ageitgey / face_recognition

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

__call__(): incompatible function arguments #853

Closed niltonmalves closed 5 years ago

niltonmalves commented 5 years ago

Description

I'm trying to catalog all the faces that go to a store. then try to find out if a particular person frequents that place. first I removed all of the faces and saved separately. Now I'm trying to compare these saved files, but the second error happens.

What I Did


import os
import cv2
import face_recognition
from PIL import Image, ImageDraw
import numpy as np
import shutil

#pick up the faces and compare if there is any equal from this list
#having repeated face, create and put in separate folder.
#path = r'C:\Users\Matriz\Documents\rostos_vouga\ss\210520193'
path = r'C:\Users\Matriz\Documents\teste_verif_rosto_em_pasta\posicao_inicial'
files = []
#r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
        if '.jpg' in file:
            files.append(os.path.join(r, file))
        print(file)

for indice ,f   in enumerate(files):
    print(f)

    encodings = face_recognition.face_encodings(f)
    if len(encodings) > 0:
        biden_encoding = encodings[0]
        print("x1")
else:
   print("No faces found in the image!")

   pasta = "/Users/Matriz/Documents/teste_verif_rosto_em_pasta/posicao_final/xx%d" % indice
   try:
       os.makedirs(pasta)
   except OSError:
       print ("Creation of the directory %s failed" % pasta)
   else:
       print ("Successfully created the directory %s" % pasta)

   print("sdsd")
   obama_image = face_recognition.load_image_file(f)
   obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
   known_face_encodings = [obama_face_encoding]
   known_face_names = ["cliente%d" % indice]
   print(indice)
   for un in reversed(files):
       print('loop procurando rosto', un)
                # Load an image with an unknown face
       unknown_image = face_recognition.load_image_file(un)
                # Find all the faces and face encodings in the unknown image
       face_locations = face_recognition.face_locations(unknown_image)
       face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
       for (top, right, bottom, left), face_encoding in zip(face_locations, 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]
               #cut the file from the unknown folder and paste it into the folder of known

               shutil.move(un,pasta)

Paste the command(s) you ran and the output. If there was a crash, please include the traceback here.

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:\Users\Matriz\Documents\frames_vouga\verify_faces_in_path_asked_in_github.py 
frame13.jpg
frame131.jpg
frame136.jpg
frame145.jpg
frame146.jpg
frame147.jpg
frame152.jpg
frame155.jpg
frame166.jpg
frame167.jpg
frame170.jpg
frame171.jpg
frame181.jpg
frame185.jpg
frame199.jpg
frame2.jpg
frame210.jpg
frame217.jpg
frame219.jpg
frame221.jpg
frame222.jpg
frame23.jpg
frame256.jpg
frame264.jpg
frame266.jpg
frame273.jpg
frame276.jpg
frame36.jpg
frame42.jpg
frame49.jpg
frame50.jpg
frame52.jpg
frame65.jpg
frame93.jpg
C:\Users\Matriz\Documents\teste_verif_rosto_em_pasta\posicao_inicial\frame13.jpg
Traceback (most recent call last):
  File "C:\Users\Matriz\Documents\frames_vouga\verify_faces_in_path_asked_in_github.py", line 24, in <module>
    encodings = face_recognition.face_encodings(f)
  File "C:\Python37\lib\site-packages\face_recognition\api.py", line 209, in face_encodings
    raw_landmarks = _raw_face_landmarks(face_image, known_face_locations, model="small")
  File "C:\Python37\lib\site-packages\face_recognition\api.py", line 153, in _raw_face_landmarks
    face_locations = _raw_face_locations(face_image)
  File "C:\Python37\lib\site-packages\face_recognition\api.py", line 102, 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.fhog_object_detector, image: array, upsample_num_times: int=0) -> dlib.rectangles

Invoked with: <dlib.fhog_object_detector object at 0x0000017FCFB1B570>, 'C:\\Users\\Matriz\\Documents\\teste_verif_rosto_em_pasta\\posicao_inicial\\frame13.jpg', 1
>>> 

frame2 frame13 frame23 frame36 frame42 frame49 frame50 frame52 frame65 frame93 frame131 frame136 frame145 frame146 frame147 frame152 frame155 frame166 frame167 frame170 frame171 frame181 frame185 frame199 frame210 frame217 frame219 frame221 frame222 frame256 frame264 frame266 frame273 frame276

ageitgey commented 5 years ago

You have to call face_recognition.load_image_file() on each file to load the image contents before you look for faces in it. You can't pass a raw filename to face_encodings() function like you are doing here.

niltonmalves commented 5 years ago

thanks!

akshayks98 commented 4 years ago

i am getting the same error. can you please tell me what you changed in this code to solve this issue.

ageitgey commented 4 years ago

@akshayks98 Read my comment right above.