Face_recognition

Hey, everyone! I am working with face recognition and i want the system to separate my face from others on the webcam image. Here is the code:

import cv2 as cv
import face_recognition as fr
face_known = fr.load_image_file(path to known face on my desktop)
encoding_F = fr.face_encodings(face_known)[0]
cap = cv.VideoCapture(1, cv.CAP_DSHOW)
cap.set(3, 1000)
cap.set(4, 700)

while True:
    tr, image = cap.read()
    encoding_S = fr.face_encodings(image)
    try:
        locat = fr.face_locations(image)
        for location in locat:
            for encoding in encoding_S:
                result = fr.compare_faces([encoding_F], encoding)
                face_dis = fr.face_distance([encoding_F], encoding)
                if result[0]:
                    cv.rectangle(image, (locat[0], locat[1]), (locat[2], locat[3]), (0,255,0), 2)
                    cv.putText(image, f'known {100*round(face_dis[0], 2)} %', (locat[0]+2, locat[1]-2), 
    cv.FONT_HERSHEY_TRIPLEX, 1, (0,255,0), 1)
                else:
                    cv.rectangle(image, (locat[0], locat[1]), (locat[2], locat[3]), (0, 0, 255), 2)
                    cv.putText(image, 'unknown', (locat[0] + 2, locat[1] - 2), 
    cv.FONT_HERSHEY_TRIPLEX, 1, (0,0,255), 1)
        cv.imshow('win', image)
        cv.waitKey(1)
    except IndexError:
        cv.imshow('win', image)
        cv.waitKey(1)

However, this code returns the following error:
Traceback (most recent call last):
File “C:\Users\mikha\PycharmProjects\pythonProject1\face_rec.py”, line 34, in
encoding_S = fr.face_encodings(image)
File “C:\Users\mikha\PycharmProjects\pythonProject\venv\lib\site-packages\face_recognition\api.py”, line 213, in face_encodings
raw_landmarks = _raw_face_landmarks(face_image, known_face_locations, model)
File “C:\Users\mikha\PycharmProjects\pythonProject\venv\lib\site-packages\face_recognition\api.py”, line 156, in _raw_face_landmarks
face_locations = _raw_face_locations(face_image)
File “C:\Users\mikha\PycharmProjects\pythonProject\venv\lib\site-packages\face_recognition\api.py”, line 105, in _raw_face_locations
return face_detector(img, number_of_times_to_upsample)
TypeError: call(): incompatible function arguments. The following argument types are supported:
1. (self: _dlib_pybind11.fhog_object_detector, image: array, upsample_num_times: int=0) → _dlib_pybind11.rectangles
Invoked with: <_dlib_pybind11.fhog_object_detector object at
0x0000012B20AEB070>, None, 1Traceback (most recent call last):
File “C:\Users\mikha\PycharmProjects\pythonProject1\face_rec.py”, line 34, in

encoding_S = fr.face_encodings(image)
File “C:\Users\mikha\PycharmProjects\pythonProject\venv\lib\site-packages\face_recognition\api.py”, line 213, in face_encodings
raw_landmarks = _raw_face_landmarks(face_image, known_face_locations, model)
File “C:\Users\mikha\PycharmProjects\pythonProject\venv\lib\site-packages\face_recognition\api.py”, line 156, in _raw_face_landmarks
face_locations = _raw_face_locations(face_image)
File “C:\Users\mikha\PycharmProjects\pythonProject\venv\lib\site-
packages\face_recognition\api.py”, line 105, in _raw_face_locations
return face_detector(img, number_of_times_to_upsample)
TypeError: call(): incompatible function arguments. The following argument types are supported:
1. (self: _dlib_pybind11.fhog_object_detector, image: array, upsample_num_times: int=0) → _dlib_pybind11.rectangles

Invoked with: <_dlib_pybind11.fhog_object_detector object at 0x0000012B20AEB070>, None, 1

I wonder if anyone can help…

your image is invalid/empty/None, nothing came out of the capture
you have to add a check here:

tr, image = cap.read()
if tr==False:
    print("no image")
    break
    

also please check (before the loop):

if not cap.isOpened():
     # bail out

and try another camera id, like 0

1 Like

Yeah, you need to specify the input image

Thanks a million!!!