TypeError: cannot concatenate 'str' and 'int' objects in face detection using haarcascade

TypeError: cannot concatenate 'str' and 'int' objects in face detection using haarcascade

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 当我 运行 上面的代码时,它抛出一个错误说明...:[=​​12=]

回溯(最近调用最后):

文件 "e:/py_projects/detector.py",第 15 行,在

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

类型错误:无法连接 'str' 和 'int' 对象

[WARN:0] 终止异步回调

我该如何解决这个错误? 提前致谢

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

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