ageitgey / face_recognition

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

return np.linalg.norm(face_encodings - face_to_compare, axis=1) #1364

Open TanishqRajawat12 opened 3 years ago

TanishqRajawat12 commented 3 years ago

Description

i was trying this code by sentdex in my compiler but i was getting a error, sentdex in his code have assigned [0] in this line encoding = face_recognition.face_encodings(image)[0] while my system is giving error like 'list index out of range' when i copied code from sentdex. but when i tried to remove [0] and made the line like encoding = face_recognition.face_encodings(image) i now am getting another errors what should i do

What I Did

import face_recognition import os import cv2

KNOWN_FACES_DIR = "C:/Users/Admin/Desktop/KnownFaces" TOLERANCE = 0.5 FRAME_THICKNESS = 3 FONT_THICKNESS = 2 MODEL = "cnn" video = cv2.VideoCapture(0) print('Loading known faces...') known_faces = [] known_names = [] for name in os.listdir(KNOWN_FACES_DIR): for filename in os.listdir(f'{KNOWN_FACES_DIR}/{name}'): image = face_recognition.load_image_file(f'{KNOWN_FACES_DIR}/{name}/{filename}') encoding = face_recognition.face_encodings(image)[0] known_faces.append(encoding) known_names.append(name)

print('Processing unknown faces...') while True:

ret, image = video.read()
locations = face_recognition.face_locations(image, model=MODEL)
encodings = face_recognition.face_encodings(image, locations)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
print(f', found {len(encodings)} face(s)')

for face_encoding, face_location in zip(encodings, locations):
    results = face_recognition.compare_faces(known_faces, face_encoding, TOLERANCE)

    match = None
    if True in results:
        match = known_names[results.index(True)]
        print(f' - {match} from {results}')

        top_left = (face_location[3], face_location[0])
        bottom_right = (face_location[1], face_location[2])

        color = [0, 255, 0]

        cv2.rectangle(image, top_left, bottom_right, color, FRAME_THICKNESS)

        top_left = (face_location[3], face_location[2])
        bottom_right = (face_location[1], face_location[2] + 22)

        cv2.rectangle(image, top_left, bottom_right, color, cv2.FILLED)

        cv2.putText(image, match, (face_location[3] + 10, face_location[2] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), FONT_THICKNESS)

cv2.imshow(video, image)
if cv2.waitKey(1) & 0xFF == ord("q"):
    break
cv2.destroyWindow(filename)
Traceback (most recent call last):
  File "C:\Users\Admin\PycharmProjects\pythonProject1\venv\FT.py", line 18, in <module>
    encoding = face_recognition.face_encodings(image)[0]
IndexError: list index out of range
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback 

this happend when i didnt remove [0]

[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\videoio\src\cap_msmf.cpp (1022) CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147483638
Traceback (most recent call last):
  File "C:\Users\Admin\PycharmProjects\pythonProject1\venv\FT.py", line 27, in <module>
    locations = face_recognition.face_locations(image, model=MODEL)
  File "C:\Users\Admin\PycharmProjects\pythonProject1\venv\lib\site-packages\face_recognition\api.py", line 119, in face_locations
    return [_trim_css_to_bounds(_rect_to_css(face.rect), img.shape) for face in _raw_face_locations(img, number_of_times_to_upsample, "cnn")]
  File "C:\Users\Admin\PycharmProjects\pythonProject1\venv\lib\site-packages\face_recognition\api.py", line 103, in _raw_face_locations
    return cnn_face_detector(img, number_of_times_to_upsample)
TypeError: __call__(): incompatible function arguments. The following argument types are supported:
    1. (self: _dlib_pybind11.cnn_face_detection_model_v1, imgs: list, upsample_num_times: int=0, batch_size: int=128) -> std::vector<std::vector<dlib::mmod_rect,std::allocator<dlib::mmod_rect> >,std::allocator<std::vector<dlib::mmod_rect,std::allocator<dlib::mmod_rect> > > >
    2. (self: _dlib_pybind11.cnn_face_detection_model_v1, img: array, upsample_num_times: int=0) -> std::vector<dlib::mmod_rect,std::allocator<dlib::mmod_rect> >

Invoked with: <_dlib_pybind11.cnn_face_detection_model_v1 object at 0x0000019063CB1BB0>, None, 1

Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,
<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic
conversions are optional and require extra headers to be included
when compiling your pybind11 module.
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

this happend when i tried to remove [0]
wildporg-ubuntu commented 2 years ago

The problem is the way the file handling works is it loads the image, it find the faces in it, and then gets the distance. However if there are no faces in the image, it cannot reference the first face. You can fix it by adding an if statement that only gets the distance if there is a face in the image, so it would look something like if len(face_recognition.face_encodings(face_recognition.load_image_file(myFile)) > 0: print(face_recognition.face_distance(knownPeople, face_recognition.face_encodings(face_recognition.load_image_file(myFile))))