如何修复Python错误:“ int”对象不可迭代

时间:2019-04-11 03:18:46

标签: python opencv

我正在使用opencv进行人脸识别,并且在执行代码时出现错误:

File "<ipython-input-2-b8cd1a5a0b77>", line 44, in <module>
    canvas = detect(gray, frame)

  File "<ipython-input-2-b8cd1a5a0b77>", line 23, in detect
    id_, conf = recognizer.predict(roi_gray)

TypeError: 'int' object is not iterable

然后,内核死了。

请帮助

def detect(gray, frame):
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]

        id_, conf = recognizer.predict(roi_gray)
        if conf>=45:
            font = cv2.FONT_HERSHEY_COMPLEX
            name = labels[id_]
            color = (255, 255, 0)
            cv2.putText(frame, name, (x,y), font, 1, color, 2, cv2.LINE_AA)


        eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 3)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
    return frame

video_capture = cv2.VideoCapture(0)
while True:
    ret, frame = video_capture.read()
    if ret is True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    else:
        continue
    canvas = detect(gray, frame)
    cv2.imshow('Video', canvas)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()

0 个答案:

没有答案