Python - 人脸识别TypeError:' int'对象不可迭代

时间:2017-04-14 23:37:09

标签: python opencv opencv3.0 magic-mirror

我正在尝试为项目实施魔镜面部识别模块。当我在文件上运行测试程序时出现错误,我似乎无法解决它。

这是终端显示的内容:

Loading training data...

ALGORITHM: LBPH

Training data loaded!

Picam selected...

Traceback (most recent call last):

File "facerecognition.py", line 61, in <module>

label, confidence = model.predict(crop)

TypeError: 'int' object is not iterable

我的picamera保持活跃,但我可以看到另一张图片,这是我脸上的背景,下面是我的代码

import cv2  # OpenCV Library
import lib.face as face
import lib.config as config
import time
import os

# Load training data into model
print('Loading training data...')

if config.RECOGNITION_ALGORITHM == 1:
   print "ALGORITHM: LBPH"
   model = cv2.face.createLBPHFaceRecognizer(threshold=config.POSITIVE_THRESHOLD)
elif config.RECOGNITION_ALGORITHM == 2:
   print "ALGORITHM: Fisher"
   model = cv2.createFisherFaceRecognizer(threshold=config.POSITIVE_THRESHOLD)
else:
   print "ALGORITHM: Eigen"
   model =     cv2.createEigenFaceRecognizer(threshold=config.POSITIVE_THRESHOLD)

model.load("training.xml")
print('Training data loaded!')

camera = config.get_camera()

time.sleep(1)   # give the camera a second to warm up
while True:
    # camera video feed
    frame = camera.read()

    image = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

    faces = face.detect_face(image)
    if faces is not None:
        for i in range(0, len(faces)):
            x, y, w, h = faces[i]
            x_face = x
            y_face = y
            if config.RECOGNITION_ALGORITHM == 1:
            crop = face.crop(image, x, y, w, h)
    else:
            crop = face.resize(face.crop(image, x, y, w, h))


    label, confidence = model.predict(crop)
    cv2.rectangle(frame, (x, y), (x + w, y + h), 255)
    cv2.putText(frame, str(h), (x + w, y + h + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
    if (label != -1 and label != 0):
        # If person is close to the camera use smaller POSITIVE_THRESHOLD
        if h > 190 and confidence < config.POSITIVE_THRESHOLD:
            cv2.putText(frame, config.users[label - 1], (x - 3, y - 8), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 1)
            cv2.putText(frame, str(confidence), (x - 2, y + h + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
            print('User:' + config.users[label - 1])    
        # If person is further away from the camera but POSITIVE_THRESHOLD is still under 40 assume it is the person
        elif h <= 190 and confidence < config.POSITIVE_THRESHOLD:
            cv2.putText(frame, config.users[label - 1], (x - 3, y - 8), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 1)
            cv2.putText(frame, str(confidence), (x - 2, y + h + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
            print('User:' + config.users[label - 1])
        # If person is further away from the camera be a bit more generous with the POSITIVE_THRESHOLD and add a not sure statement
        elif h < 190:
            cv2.putText(frame, "Guess: " + config.users[label - 1], (x - 3, y - 8), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1)
            cv2.putText(frame, str(confidence), (x - 2, y + h + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
            print('Guess:' + config.users[label - 1])
        else:
            cv2.putText(frame, "Unknown", (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 1)
            print('Unknown face')
    else:
        cv2.putText(frame, "Unknown", (x, y - 5),     cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 1)
        print('Unknown face')

     # If person is close enough
     if h > 250:
         eyes = face.detect_eyes(face.crop(image, x, y, w, h))
         for i in range(0, len(eyes)):
             x, y, w, h = eyes[i]
             cv2.rectangle(frame, (x + x_face, y + y_face - 30), (x +  x_face + w + 10, y + y_face + h - 40), (94, 255, 0))
             cv2.putText(frame, "Eye " + str(i), (x + x_face, y +         y_face - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)

     if ('DISPLAY' in os.environ):
         # Display Image
         cv2.imshow('Facial recognition', frame)
         k = cv2.waitKey(1) & 0xFF
         if k == ord('q'):
             break
     else:
         print('writing face.jpg image')
         cv2.imwrite('face.jpg',frame)
         camera.stop()
         break

# Release camera and close windows
camera.stop()
cv2.destroyAllWindows()

任何帮助都将不胜感激,任何人都可以帮忙解决这个问题,我真的被卡住了。

0 个答案:

没有答案