i am trying to store the face encodings in the database and file but after it when i fetch it and pass it to the face_recoggnition.facecomparison()
it's not accepting it; it gives error:
Traceback (most recent call last):
File "C:\Projects\FACE RECOGNITION\fetch.py", line 19, in <module>
com = face_recognition.compare_faces(enc,x)
File "C:\Projects\FACE RECOGNITION\venv\lib\site-packages\face_recognition\api.py", line 226, in compare_faces
return list(face_distance(known_face_encodings, face_encoding_to_check) <= tolerance)
File "C:\Projects\FACE RECOGNITION\venv\lib\site-packages\face_recognition\api.py", line 75, in face_distance
return np.linalg.norm(face_encodings - face_to_compare, axis=1)
numpy.core._exceptions._UFuncNoLoopError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('float64'), dtype('<U1558')) -> None
Process finished with exit code 1
my code for fetching the encodings:
import pymongo
import pickle
import cv2,face_recognition
import numpy as np
enc=[]
with (open("Encodings/encodings.pickle", "rb")) as openfile:
x = pickle.load(openfile)
x = x["enc"][0]
x = np.asarray(x)
print(x)
image = cv2.imread("IMAGES/UTKARSH RAI.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(image)[0]
enc.append(encode)
com = face_recognition.compare_faces(enc,x)
dis = face_recognition.face_distance(enc,x)
print(dis)
i have also tried MongoDB and the case is again the same i tried without x = np.asarray(x) but it also gives error.
CodePudding user response:
I think you have type mismatch issue here. Better check the data type of both enc and x variable e.g. type(enc).
The solution is to use the same data type of variabes. You can use typecast to cast all variables in the same type.E.g.
x = np.asarray(x)
enc = np.asarray(enc)
then run
com = face_recognition.compare_faces(enc,x)
CodePudding user response:
before I finaly moved to Elasticsearch as a db server, I've been using the following structure to store face encodings. It is extebded "classical" way: I append a list of face locations (face box coordinates on an image) for the cases when many faces on the same image are present:
class Face_Dictionary():
"""[The class contains all data structures to store in memory as
well as load and save to file
face encodings data, including picture file full path and
face location for each face on every picture]
"""
def __init__(self, dicfilename="", mode="load"): #Create all data objects
self.mode = mode
self.dicfilename = dicfilename
self.fl_Loaded = False
self.fl_Saved = False
self.Encodings = []
self.Names = []
self.facelocs = []
self.fd = {"encodings": self.Encodings, "names": self.Names,
"locations": self.facelocs}
if bool(self.dicfilename) and self.mode == "load":
self.fl_Loaded = self.load()
def __del__(self): #redefine method del()
del(self.Encodings)
del(self.Names)
del(self.facelocs)
del(self.fd)
def load(self):
"""
[Loads a dictionary with face encodings from Pickle-type file into self.fd]
Args:
dicfilename ([str]): [Pickle-type file *.pkl]
Returns:
[bool]: [True if loaded. Also sets self.fl_Loaded as True]
"""
if bool(self.dicfilename) and self.mode == "load":
try:
f = open(self.dicfilename, "rb")
except (IOError, EOFError) as e:
print("[ERR] Не можу знайти/прочитати файл кодувань обличь: %s" % e)
return False
else:
if os.path.getsize(self.dicfilename) > 15:
self.fd = load(f)
self.fl_Loaded = True
else:
f.close()
return False
f.close()
return True
else:
return False
def save(self):
"""
[Saves dictionary with face encodings to Pickle-type file]
Args:
None
Returns:
[bool]: [True if self.fd has been saved to the original <self.dicfilename.pkl>.
Also sets self.fl_Saved as True]
"""
if bool(self.dicfilename) and self.mode == "save":
try:
f = open(self.dicfilename, "wb")
except OSError:
print("[ERR] Не можу створити файл бази даних %s" % self.dicfilename)
return False
dump(self.fd, f, protocol=HIGHEST_PROTOCOL)
f.close()
self.fl_Saved = True
return True
def save_as(self, filename):
"""
[Saves dictionary with face encodings to Pickle-type file with specified name]
Args:
filename ([str]): [Pickle-type file *.pkl]
Returns:
[bool]: [True if self.fd has been saved to the original <self.dicfilename.pkl>.
Also sets self.fl_Saved as True]
"""
if bool(filename):
try:
f = open(filename, "wb")
except OSError:
print("[ERR] Не можу створити файл бази даних %s" % filename)
return False
dump(self.fd, f, protocol=HIGHEST_PROTOCOL)
f.close()
self.fl_Saved = True
return True