TypeError:无法使用haarcascade连接人脸检测中的“ str”和“ int”对象

时间:2019-04-05 13:05:49

标签: python-3.x detection face

import cv2
cam = cv2.VideoCapture(0)
detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Id=input('enter your id....')
sampleNum=0
while(True):
    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = detector.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

        sampleNum=sampleNum+1
        cv2.imwrite("dataSet/User."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
        cv2.imshow('frame',img)
    #wait for 100 miliseconds 
    if cv2.waitKey(100) & 0xFF == ord('q'):
        break
    # break if the sample number is morethan 20
    elif sampleNum>20:
        break
cam.release()
cv2.destroyAllWindows()

编辑:1 当我运行上面的代码时,它抛出一个错误,指出...:

回溯(最近通话最近一次):

文件

中的“ e:/py_projects/detector.py”,第15行
cv2.imwrite("dataSet/User."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])

TypeError:无法连接“ str”和“ int”对象

[WARN:0]终止异步回调

如何解决此错误? 预先感谢

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,尝试将Id转换为字符串对象。

cv2.imwrite("dataSet/User." + str(Id) + '.' + str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])