Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

recognize_faces_in_pictures.py 1.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  1. import face_recognition
  2. # Load the jpg files into numpy arrays
  3. biden_image = face_recognition.load_image_file("biden.jpg")
  4. obama_image = face_recognition.load_image_file("obama.jpg")
  5. unknown_image = face_recognition.load_image_file("obama2.jpg")
  6. # Get the face encodings for each face in each image file
  7. # Since there could be more than one face in each image, it returns a list of encodings.
  8. # But since I know each image only has one face, I only care about the first encoding in each image, so I grab index 0.
  9. try:
  10. biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
  11. obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
  12. unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
  13. except IndexError:
  14. print("I wasn't able to locate any faces in at least one of the images. Check the image files. Aborting...")
  15. quit()
  16. known_faces = [
  17. biden_face_encoding,
  18. obama_face_encoding
  19. ]
  20. # results is an array of True/False telling if the unknown face matched anyone in the known_faces array
  21. results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
  22. print("Is the unknown face a picture of Biden? {}".format(results[0]))
  23. print("Is the unknown face a picture of Obama? {}".format(results[1]))
  24. print("Is the unknown face a new person that we've never seen before? {}".format(not True in results))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...