【问题标题】:IndexError: tuple index out of range -pythonIndexError:元组索引超出范围-python
【发布时间】:2018-02-10 16:35:39
【问题描述】:

请帮助我。我正在运行一个人脸识别检测器 python 程序,它将显示来自 sqllite studio 数据库的数据...

import cv2,os
import sqlite3
import numpy as np
from PIL import Image 
import pickle

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "Classifiers/face.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
path = 'dataSet'

def getProfile(Id):
         conn=sqlite3.connect("facebase.db")
         cmd="SELECT * FROM people WHERE ID="+str(Id)
         cursor=conn.execute(cmd)
         profile=None
         for row in cursor:
            profile=row
         conn.close()
         return profile 


cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX #Creates a font
while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, 1.2,5)
    for(x,y,w,h) in faces:
        cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        profile=getProfile(Id)
        if(profile!=None):
        #cv2.putText(im,str(profile[1]),(x,y+h+20),font, 2, 255, 3)
        cv2.putText(im, str(profile[1]), (x,y-40), font, 2, (255,255,255),3)
        #cv2.putText(im,str(profile[2]),(x,y+h+40),font, 2, 255, 3)
        cv2.putText(im, str(profile[2]), (x,y-80), font, 2, (255,255,255),3)
    else:
        Id="Unknown"
    cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
    cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)
cv2.imshow('im',im) 
if cv2.waitKey(10) & 0xFF==ord('q'):
    break
cam.release()
cv2.destroyAllWindows()

这就是程序

错误是

   Traceback (most recent call last):
   File "C:\Users\Titus John\Desktop\New folder (6)\Face-Recognition-
   master\detector.py", line 38, in <module>
   cv2.putText(im, str(profile[2]), (x,y-80), font, 2, (255,255,255), 3)
   IndexError: tuple index out of range

我正在使用 python 3.4 和 opencv 3.4 谁能帮我???我是python新手。

非常感谢……

【问题讨论】:

  • 看起来你的元组profile 包含少于三个元素。 Python 索引从 0 开始,而不是 1。
  • 您的身份已关闭。 if profile: 之后的代码应该缩进。检查其余部分。
  • 虽然解决了这个问题...

标签: python opencv computer-vision mysql-python opencv3.0


【解决方案1】:

索引在 Python 中是从零开始的。也就是说,计数从0 开始,而不是1

尝试将第 36 和 38 行更改为:

cv2.putText(im, str(profile[0]), (x,y-40), font, 2, (255,255,255),3)
cv2.putText(im, str(profile[1]), (x,y-80), font, 2, (255,255,255),3)

【讨论】:

  • 如果成功了,你可以点击对勾来接受我的回答。
猜你喜欢
  • 2013-12-16
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-28
  • 2018-11-16
  • 2017-07-12
相关资源
最近更新 更多