How can I make face recognition faster if I have more than 1M known images?
I tried to use your amazing project for face recognition (for example single unknown image) with my big number of known images (1 Million) but its really slow, this slow because its will load all known images (load_image_file -> then face_encodings) in order to compare with ONE unknown image file.
Any ideas how to speed this process? I was thinking to do face_encodings for all known images then save the 128 as string into apache solr but with no luck as I still need to do compare_faces with all known images:) ... Any suggestions?
You could create a database table (postgres, mysql, etc) with 128 columns and store the pre-calculated 1M encodings in that table. Then you could do the compare_faces math using sql against that table to check one face.
Thank you for quick reply, How can I translate your compare_faces since its using np.linalg.norm to any other sql? Do you have any real example?
The formula for euclidean distance is just:
So assuming you had one column for each of the 128 feature values, you could do something like:
SELECT * from my_stored_encodings
ORDER BY
sqrt(
power(e1 - TEST_ENCODING_VALUE_0_HERE, 2) +
power(e2 - TEST_ENCODING_VALUE_1_HERE, 2) +
power(..... etc....
)
If you are using Postgresql, you can do more complex things like using it's built-in list data types to store the 128-number encoding in one column and do the comparison using a custom stored function. Just google around for "euclidean distance in sql".
Many thanks :)
you can use numpy logic to caluclate Euclidian distance between two vectors. dist = numpy.linalg.norm(vecor(a)-vector(b))
Thank you All. I've indexed all images encoding into apache solr and then I managed euclidean distance using solr build-in function dist i.e. http://localhost:8983/solr/mycore/select?q=:&fl=dist(2,v_0,v_1,v_3,...,v_127,-0.0621345,0.048437204,0.0839613,...)
So fare, I indexed around 40K images and the query speed very good (17ms without any solr cache)
@khaledabbad : Could you please elaborate on how you managed to do this? I'm working on something very similar.
I used Apache solr filter query with dist function.
@khaledabbad & @ageitgey : How much accuracy can we expect if we go for euclidean distance calculation rather than knn classifier in case of large set of image data ? ----> https://github.com/ageitgey/face_recognition/blob/master/examples/face_recognition_knn.py As i tested Knn_classifier is much promising in case of accuracy , can we expect that much accuracy. or is this euclidean distance concept is used in knn classifier
hello @ageitgey
thanks for the great work.
I am saving the face_encodings into database in one column. But I am not sure what data type should I select for the row. bigint ? bigserial ?
can you help please ?
I don't think its will be a good idea to save all face features (encodings) in one column, try to add 128 columns with float data type i.e. Column0 float(22,20),Column1 float(22,20), ... Column127 float(22,20).
@khaledabbad but I am using postgresql database. @ageitgey mentioned that we can store encodings in 1 column if we use postgres.
@xenc0d3r In postges, you can optionally store them in a single column using the CUBE extension. However postgres has a 100-dimension limit on cube fields by default and you have to edit a file and recompile postgres yourself if you want to do it that way since the face encodings are 128 dimensions.
See the other thread: https://github.com/ageitgey/face_recognition/issues/403#issuecomment-373437405
@ageitgey thank you for your response. So I decided to save them as multiple columns. I know you are busy with other works but can you give a sql query example how to compare a uploaded image with the saved encodings in python.
Thanks in advance.
@xenc0d3r There's an example higher in this thread: https://github.com/ageitgey/face_recognition/issues/238#issuecomment-345847465
You'll just have to write out all 128 terms in the sql statement instead of only the 2 I put in the short example.
Hello @ageitgey @khaledabbad When I try to save the encoded list to postgresql with python psycopg2 I receive the following error. TypeError: 'numpy.float64' object does not support indexing
I have 128 rows for encoded list elements in my database table and their datatypes are float. Can you help me please ?
Best Regards
@ageitgey @khaledabbad my code is as :
test = list(encoded-photo) for row in test: c.execute("""INSERT INTO photos VALUES (DEFAULT,%s,%s,%s ---128 times,);""", row)
I had success inserting with this: (python code) INSERT INTO test (face_encoding) VALUES ('"+face_encoding_string+"') just changing values to string and inserting into postgres 128 cube encoding. Python: face_encoding_string = "(0.1,0.1,0.1 .... 0.1,0.1)"
For me I don't need to optimize when filling database so this works in my case. I just need optimized querying for when I move towards analyzing video streams for face recognition.
@mmelatti can you help me about this. can we discuss instantly on twitter etc. thank you
Don't have twitter atm sorry, I'll post my code and see if it can help you? I'm using psycopg2 as well. This isn't a final product but you can see if it works for you:
` image = face_recognition.load_image_file(self.filename) face_encoding = face_recognition.face_encodings(image)[0] #only 1 face expected when entering 'mugshot' face_encoding_string = "("
for distance in face_encoding:
face_encoding_string+=str(distance)
face_encoding_string+=str(",")
face_encoding_string = face_encoding_string[:-1]
face_encoding_string += str(")")
print(face_encoding_string) # demo what a face encoding data looks like in the output
conn = psycopg2.connect(host="localhost",database="postgres", user="postgres", password="password")
cur = conn.cursor()
cur.execute("INSERT INTO wanted (first_name, last_name, face_encoding) VALUES ('"+self.entry_1.get()+"', '"+self.entry_2.get()+"', '"+face_encoding_string+"')");
conn.commit()
conn.close()`