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

identify_and_draw_boxes_on_faces.py 2.8 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  1. import face_recognition
  2. from PIL import Image, ImageDraw
  3. import numpy as np
  4. # This is an example of running face recognition on a single image
  5. # and drawing a box around each person that was identified.
  6. # Load a sample picture and learn how to recognize it.
  7. obama_image = face_recognition.load_image_file("obama.jpg")
  8. obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
  9. # Load a second sample picture and learn how to recognize it.
  10. biden_image = face_recognition.load_image_file("biden.jpg")
  11. biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
  12. # Create arrays of known face encodings and their names
  13. known_face_encodings = [
  14. obama_face_encoding,
  15. biden_face_encoding
  16. ]
  17. known_face_names = [
  18. "Barack Obama",
  19. "Joe Biden"
  20. ]
  21. # Load an image with an unknown face
  22. unknown_image = face_recognition.load_image_file("two_people.jpg")
  23. # Find all the faces and face encodings in the unknown image
  24. face_locations = face_recognition.face_locations(unknown_image)
  25. face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
  26. # Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library
  27. # See http://pillow.readthedocs.io/ for more about PIL/Pillow
  28. pil_image = Image.fromarray(unknown_image)
  29. # Create a Pillow ImageDraw Draw instance to draw with
  30. draw = ImageDraw.Draw(pil_image)
  31. # Loop through each face found in the unknown image
  32. for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
  33. # See if the face is a match for the known face(s)
  34. matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
  35. name = "Unknown"
  36. # If a match was found in known_face_encodings, just use the first one.
  37. # if True in matches:
  38. # first_match_index = matches.index(True)
  39. # name = known_face_names[first_match_index]
  40. # Or instead, use the known face with the smallest distance to the new face
  41. face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
  42. best_match_index = np.argmin(face_distances)
  43. if matches[best_match_index]:
  44. name = known_face_names[best_match_index]
  45. # Draw a box around the face using the Pillow module
  46. draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))
  47. # Draw a label with a name below the face
  48. text_width, text_height = draw.textsize(name)
  49. draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
  50. draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))
  51. # Remove the drawing library from memory as per the Pillow docs
  52. del draw
  53. # Display the resulting image
  54. pil_image.show()
  55. # You can also save a copy of the new image to disk if you want by uncommenting this line
  56. # pil_image.save("image_with_boxes.jpg")
Tip!

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

Comments

Loading...