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

facerec_from_video_file.py 3.0 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
74
75
76
77
78
79
80
81
82
83
84
85
86
  1. import face_recognition
  2. import cv2
  3. # This is a demo of running face recognition on a video file and saving the results to a new video file.
  4. #
  5. # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
  6. # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
  7. # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
  8. # Open the input movie file
  9. input_movie = cv2.VideoCapture("hamilton_clip.mp4")
  10. length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))
  11. # Create an output movie file (make sure resolution/frame rate matches input video!)
  12. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  13. output_movie = cv2.VideoWriter('output.avi', fourcc, 29.97, (640, 360))
  14. # Load some sample pictures and learn how to recognize them.
  15. lmm_image = face_recognition.load_image_file("lin-manuel-miranda.png")
  16. lmm_face_encoding = face_recognition.face_encodings(lmm_image)[0]
  17. al_image = face_recognition.load_image_file("alex-lacamoire.png")
  18. al_face_encoding = face_recognition.face_encodings(al_image)[0]
  19. known_faces = [
  20. lmm_face_encoding,
  21. al_face_encoding
  22. ]
  23. # Initialize some variables
  24. face_locations = []
  25. face_encodings = []
  26. face_names = []
  27. frame_number = 0
  28. while True:
  29. # Grab a single frame of video
  30. ret, frame = input_movie.read()
  31. frame_number += 1
  32. # Quit when the input video file ends
  33. if not ret:
  34. break
  35. # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
  36. rgb_frame = frame[:, :, ::-1]
  37. # Find all the faces and face encodings in the current frame of video
  38. face_locations = face_recognition.face_locations(rgb_frame)
  39. face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
  40. face_names = []
  41. for face_encoding in face_encodings:
  42. # See if the face is a match for the known face(s)
  43. match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.50)
  44. # If you had more than 2 faces, you could make this logic a lot prettier
  45. # but I kept it simple for the demo
  46. name = None
  47. if match[0]:
  48. name = "Lin-Manuel Miranda"
  49. elif match[1]:
  50. name = "Alex Lacamoire"
  51. face_names.append(name)
  52. # Label the results
  53. for (top, right, bottom, left), name in zip(face_locations, face_names):
  54. if not name:
  55. continue
  56. # Draw a box around the face
  57. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  58. # Draw a label with a name below the face
  59. cv2.rectangle(frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED)
  60. font = cv2.FONT_HERSHEY_DUPLEX
  61. cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
  62. # Write the resulting image to the output video file
  63. print("Writing frame {} / {}".format(frame_number, length))
  64. output_movie.write(frame)
  65. # All done!
  66. input_movie.release()
  67. cv2.destroyAllWindows()
Tip!

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

Comments

Loading...