此代码用于识别面孔。
我认为问题是关于2和3之间的python版本,因为我从py2项目中克隆了它。
我的环境是Python 3.5.1和OpenCV 3.1.0
[X,y]=read_images(sys.argv[1])
y=np.asarray(y,dtype=np.int32)
if len(sys.argv)==3:
out_dir=sys.argv[2]
model=cv2.face.createEigenFaceRecognizer()
model.train(np.asarray(X),np.asarray(y))
camera=cv2.VideoCapture(0)
face_cascade=cv2.CascadeClassifier('/home/cisco/py/data/haarcascades/cascades/haarcascade_frontalface_default.xml')
while(True):
read,img=camera.read()
faces=face_cascade.detectMultiScale(img,1.3,5)
for(x,y,w,h) in faces:
img=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
roi=gray[x:x+w,y:y+h]
try:
roi=cv2.resize(roi,(200,200),interpolation=cv2.INTER_LINEAR)
print(roi)
[La,Con]=model.predict(roi) #<<<------------------error!!
print("Label:%s , Confidence:%.2f" % (La,Con))
cv2.putText(img,names[label],(x,y-20),cv2.FONT_HERSHEY_SIMPLEX,1,255,2)
except Exception as e:
raise e
continue
cv2.imshow("camera",img)
if cv2.waitKey(0):
continue
cv2.destoryAllwindows()
我得到的错误:
Traceback (most recent call last):
File "whole.py", line 73, in <module>
face_rec()
File "whole.py", line 64, in face_rec
raise e
File "whole.py", line 60, in face_rec
[La,Con]=model.predict(roi)
TypeError: 'int' object is not iterable