Opencv 在启动时崩溃

Opencv 在启动时崩溃

尝试在我的网络摄像头上实时使用人脸识别,但是 opencv 在打开摄像头后就崩溃了。这里是代码:

import face_recognition
import cv2
import os
import re


fileIndex = {
    '#png_files'
}

video_capture = cv2.VideoCapture(0)


known_face_encodings = []
known_face_names = []
for n in os.listdir('#path'):
    sp = os.path.splitext(n)
    if re.match(r'^\.(png|jpe?g|gif)$', sp[1]):
        if fileIndex.get(n):
            img = face_recognition.load_image_file("#path" + n)
            encoding = face_recognition.face_encodings(img)[0]
            known_face_encodings.append(encoding)
            known_face_names.append(n)

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

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        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:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # If a match was found in known_face_encodings, just use the first one.
            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]
                name = fileIndex.get(name, 'Unknown')

            face_names.append(os.path.splitext(name)[0])

    process_this_frame = not process_this_frame
    

    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (3, 168, 124), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (3, 168, 124), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    # Display the resulting image
    cv2.imshow('user-cam', frame)
    
    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()

这里是 Python 错误:

Traceback (most recent call last):
  File "/home/s3idani/Scripts/security/face_recognition/py/facerec_from_webcam_more_faster.py", line 60, in <module>
    face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
  File "/home/s3idani/.local/lib/python3.8/site-packages/face_recognition/api.py", line 214, in face_encodings
    return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]
  File "/home/s3idani/.local/lib/python3.8/site-packages/face_recognition/api.py", line 214, in <listcomp>
    return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]
TypeError: compute_face_descriptor(): incompatible function arguments. The following argument types are supported:
    1. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], face: _dlib_pybind11.full_object_detection, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vector
    2. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], num_jitters: int = 0) -> _dlib_pybind11.vector
    3. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], faces: _dlib_pybind11.full_object_detections, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectors
    4. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),numpy.uint8]], batch_faces: List[_dlib_pybind11.full_object_detections], num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectorss
    5. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),numpy.uint8]], num_jitters: int = 0) -> _dlib_pybind11.vectors

Invoked with: <_dlib_pybind11.face_recognition_model_v1 object at 0x7f906a226830>, array([[[252, 248, 200],
        [255, 252, 205],
        [252, 248, 203],
        ...,
        [188, 210, 189],
        [181, 201, 177],
        [163, 186, 170]],

       [[252, 251, 205],
        [253, 255, 208],
        [252, 254, 205],
        ...,
        [189, 211, 187],
        [187, 207, 183],
        [171, 187, 172]],

       [[251, 250, 206],
        [253, 255, 216],
        [253, 255, 217],
        ...,
        [184, 207, 189],
        [202, 223, 197],
        [166, 192, 166]],

       ...,

       [[182, 162, 117],
        [201, 189, 146],
        [181, 166, 125],
        ...,
        [211, 240, 227],
        [209, 237, 225],
        [203, 237, 220]],

       [[166, 150, 119],
        [174, 156, 122],
        [162, 148, 112],
        ...,
        [206, 242, 223],
        [207, 237, 219],
        [205, 232, 216]],

       [[136, 128, 110],
        [150, 143, 112],
        [155, 148, 113],
        ...,
        [208, 239, 222],
        [207, 236, 220],
        [205, 231, 211]]], dtype=uint8), <_dlib_pybind11.full_object_detection object at 0x7f906a226370>, 1

蟒蛇3.8

opencv 4.7

dlib 19.24

如何解决这个问题?

答案1

我遇到了同样的问题,我发现更换

rgb_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)

rgb_frame = small_frame[:, :, ::-1]

这个问题可能更有帮助https://github.com/ageitgey/face_recognition/issues/1497

相关内容