ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
53.28k stars 13.48k forks source link

Example Code (Detecting people with webcam) does not function #1497

Open KinetekEnergy opened 1 year ago

KinetekEnergy commented 1 year ago

Description

I ran the example code given to recognize faces (live recognition through a webcam). I used my own photo, modifying the code so that it only detects one person (so the second encoding was removed).

What I Did

I clicked the run button in VSCode and the moment my face was detected, it crashed. Things I've tested:

Invoked with: <_dlib_pybind11.face_recognition_model_v1 object at 0x0000020C64F4EE70>, array([[[124, 130, 130], [124, 130, 130], [123, 129, 129], ..., [134, 150, 163], [115, 129, 140], [104, 118, 126]],

   [[140, 146, 146],
    [140, 146, 146],
    [142, 148, 148],
    ...,
    [138, 147, 161],
    [118, 126, 136],
    [105, 113, 120]],

   [[150, 156, 156],
    [151, 157, 157],
    [150, 156, 156],
    ...,
    [137, 144, 157],
    [116, 123, 131],
    [105, 111, 118]],

   ...,

   [[ 70,  47,  48],
    [122, 114, 117],
    [108, 109, 111],
    ...,
    [ 71,  45,  52],
    [ 66,  40,  51],
    [ 60,  37,  50]],

   [[ 63,  44,  48],
    [105, 101, 104],
    [ 98, 100, 103],
    ...,
    [ 72,  47,  52],
    [ 70,  45,  53],
    [ 62,  39,  49]],

   [[ 69,  54,  60],
    [105, 102, 108],
    [110, 112, 116],
    ...,
    [ 71,  47,  45],
    [ 69,  45,  47],
    [ 61,  39,  43]]], dtype=uint8), <_dlib_pybind11.full_object_detection object at 0x0000020C4C0AEEB0>, 1
BirenMer commented 1 year ago

Hello there I am having the same issue, Please help me solve it ` /home/x/workspace/face-recognition-2/frc/bin/python /home/x/workspace/face-recognition-2/main.py Traceback (most recent call last): File "/home/x/workspace/face-recognition-2/main.py", line 54, in face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) File "/home/x/workspace/face-recognition-2/frc/lib/python3.10/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/x/workspace/face-recognition-2/frc/lib/python3.10/site-packages/face_recognition/api.py", line 214, in 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 0x7fdc4cbd59b0>, array([[[166, 182, 179], [167, 182, 180], [168, 184, 181], ..., [197, 206, 221], [197, 209, 221], [196, 208, 223]],

   [[170, 182, 180],
    [172, 181, 178],
    [172, 183, 179],
    ...,
    [193, 212, 221],
    [192, 212, 221],
    [192, 211, 218]],

   [[157, 180, 177],
    [163, 183, 181],
    [170, 183, 182],
    ...,
    [193, 213, 222],
    [191, 213, 218],
    [193, 211, 221]],

   ...,

   [[ 49,  49,  49],
    [ 45,  46,  44],
    [ 50,  51,  48],
    ...,
    [ 27,  27,  27],
    [ 26,  28,  28],
    [ 27,  29,  28]],

   [[ 48,  48,  47],
    [ 45,  46,  43],
    [ 50,  51,  48],
    ...,
    [ 27,  27,  27],
    [ 26,  30,  28],
    [ 25,  30,  28]],

   [[ 46,  46,  45],
    [ 48,  49,  43],
    [ 50,  51,  46],
    ...,
    [ 25,  28,  27],
    [ 27,  28,  28],
    [ 26,  29,  28]]], dtype=uint8), <_dlib_pybind11.full_object_detection object at 0x7fdc51ffde30>, 1`
BirenMer commented 1 year ago

Hey, this helped me in solving my issue,

Try rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) instead of rgb_frame = frame[:, :, ::-1]

Credits:-original issue

KinetekEnergy commented 1 year ago

So I tried your fix and it seems to work. However, the code still doesn't do anything because when showing it face, it doesn't draw a box or label anything. Here is the code:


import face_recognition
import cv2
import numpy as np

# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "Barack Obama",
    "Joe Biden"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

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

    # Only process every other frame of video to save time
    if process_this_frame:
        # 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_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_frame)
        face_encodings = face_recognition.face_encodings(
            rgb_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]

            # Or instead, use the known face with the smallest distance to the new face
            face_distances = face_recognition.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]

            face_names.append(name)

    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), (0, 0, 255), 2)

        # Draw a label with a name below the face
        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)

    # Display the resulting image
    cv2.imshow('CCP Facial Recognition System', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
BirenMer commented 1 year ago

Try replacing This: small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) with this: small_frame=cv2.resize(frame,(0,0),fx=1,fy=1)

Also, remove this line: process_this_frame = not process_this_frame

KinetekEnergy commented 1 year ago

the changes haven't done anything. it still isn't showing any sign of detection

BirenMer commented 1 year ago

Yup, I tried to debug, it is able to provide name but it is not able to draw on the top of image. Try this example insted example This one is working for me

KinetekEnergy commented 1 year ago

I just tested. It works. However, is there anyway to speed it up since it is quite slow. Also, I have a pretty good PC so it shouldn't be lagging. I don't think it's using all of the processing power.

riverallzero commented 1 year ago

Hi @KinetekEnergy, you should remove this code

# 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

I also updated issue with #1506 , it can be work

langhaoabcd commented 1 year ago

Yup, I tried to debug, it is able to provide name but it is not able to draw on the top of image. Try this example insted example This one is working for me

and Try rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) instead of rgb_frame = frame[:, :, ::-1]

shadowasphodel2919 commented 1 year ago

So I tried your fix and it seems to work. However, the code still doesn't do anything because when showing it face, it doesn't draw a box or label anything. Here is the code:


import face_recognition
import cv2
import numpy as np

# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "Barack Obama",
    "Joe Biden"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

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

    # Only process every other frame of video to save time
    if process_this_frame:
        # 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_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_frame)
        face_encodings = face_recognition.face_encodings(
            rgb_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]

            # Or instead, use the known face with the smallest distance to the new face
            face_distances = face_recognition.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]

            face_names.append(name)

    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), (0, 0, 255), 2)

        # Draw a label with a name below the face
        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)

    # Display the resulting image
    cv2.imshow('CCP Facial Recognition System', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

It's because of small_frame variable the example code that you are working on is different, so try to change rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) to rgb_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)

This should display the graphics as well.

riverallzero commented 1 year ago

Sorry @shadowasphodel2919 , try this code

import face_recognition
import cv2
import numpy as np

# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "Barack Obama",
    "Joe Biden"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

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

    # Only process every other frame of video to save time
    if process_this_frame:
        # Resize frame of video to 1/4 size for faster face recognition processing
        small_frame = cv2.resize(frame, (0, 0), fx=1, fy=1)

        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)

        # 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]

            # Or instead, use the known face with the smallest distance to the new face
            face_distances = face_recognition.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]

            face_names.append(name)

    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        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)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
shadowasphodel2919 commented 1 year ago

@riverallzero yes I know it works that is what I was telling you