【问题标题】:OpenCV Face Recognition in Python [closed]Python中的OpenCV人脸识别[关闭]
【发布时间】:2016-01-24 23:57:27
【问题描述】:

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

【问题讨论】:

标签: python opencv


【解决方案1】:

你可以看到我使用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()

【讨论】:

    【解决方案2】:

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

    【讨论】: