Python中的OpenCV人脸识别

时间:2016-01-24 23:57:28

标签: python opencv

this链接所示,人脸识别在C ++中可用,具有许多不同的算法。但是,当我尝试用recognizer = cv2.createLBPHFaceRecognizer()之类的东西在Python中创建一个识别器时,它似乎不存在。脸部识别器模块是否存在于Python中,如果存在,我该如何使用它?感谢。

2 个答案:

答案 0 :(得分:0)

你可以看到我使用python进行人脸识别的完整代码。你还必须做一件事,在你的 python 文件所在的目录中,你还必须放置训练图像。还要安装人脸识别模块。

import numpy as np
import face_recognition as fr
import cv2

video_capture = cv2.VideoCapture(0)

image = fr.load_image_file("me.jpg")
image_encoding = fr.face_encodings(image)[0]

known_face_encodings = [image_encoding]
known_face_names = ["Me"]

while True:
    ret, frame = video_capture.read()
    rgb_frame = frame[:, :, ::-1]

    face_locations = fr.face_locations(rgb_frame)
    face_encodings = fr.face_encodings(rgb_frame, face_locations)

    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        matches = fr.compare_faces(known_face_encodings, face_encoding)
        name = "Random Person"

        face_distances = fr.face_distance(known_face_encodings, face_encoding)
        best_match_index = np.argmin(face_distances)

        if matches[best_match_index]:
            name = known_face_names[best_match_index]
    
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)

    cv2.imshow("Face Recognition", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()

答案 1 :(得分:-1)

如果有人仍然遇到这个问题,诀窍是使用opencv2而不是opencv3。这是因为在新版本中,face模块已移至opencv-contrib。如果你愿意,你可以用它编译opencv3,但我无法使它工作。