当我使用“ face_recognition”和网络摄像头识别不同的面孔时,OpenCV返回错误?

时间:2018-09-18 03:58:52

标签: opencv face-recognition

我试图重复一个face_recognition示例代码(来自:https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py)。

不幸的是,我遇到了opencv错误:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-11-8bd8ed75eefa> in <module>()
      3 #     small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
      4 #     rgb_small_frame = small_frame[:, :, ::-1]
----> 5     rgb_small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)[:, :, ::-1]
      6     if process_this_frame:
      7         face_locations = face_recognition.face_locations(rgb_small_frame)

error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4044: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

关于这些问题,我注意到有人也有同样的问题,但是本文没有实际解决方案。有没有人可以帮助我?预先谢谢你。

环境:

python:3.6
Opencv:3.4.2
dlib:19.15.0
face_recognition:1.2.3

下面是我的代码:

import face_recognition
import cv2
import os
os.chdir(r'E:\1. Learning\face_recognition')

video_capture = cv2.VideoCapture(0)

linhan_image = face_recognition.load_image_file("linhan.jpg")
linhan_face_encoding = face_recognition.face_encodings(linhan_image, num_jitters=100)[0]

ketian_image = face_recognition.load_image_file("ketian.jpg")
ketian_face_encoding = face_recognition.face_encodings(ketian_image, num_jitters=100)[0]

linwanqi_image = face_recognition.load_image_file("linwanqi.jpg")
linwanqi_face_encoding = face_recognition.face_encodings(linwanqi_image, num_jitters=100)[0]

known_face_encodings = [
    linhan_face_encoding,
    ketian_face_encoding,
    linwanqi_face_encoding
]
known_face_names = [
    "linhan",
    "ketian",
    "linwanqi"
]

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    ret, frame = video_capture.read()
#     small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
#     rgb_small_frame = small_frame[:, :, ::-1]
    rgb_small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)[:, :, ::-1]
    if process_this_frame:
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.4)
            name = "Unknown"

            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]

            face_names.append(name)

    process_this_frame = not process_this_frame

    for (top, right, bottom, left), name in zip(face_locations, face_names):
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        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_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()

0 个答案:

没有答案