Advertisement
Guest User

Untitled

a guest
May 26th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. import face_recognition
  2. import cv2
  3. from multiprocessing import Process, Manager, cpu_count
  4. import time
  5. import numpy
  6.  
  7.  
  8. # This is a little bit complicated (but fast) example of running face recognition on live video from your webcam.
  9. # This example is using multiprocess.
  10.  
  11. # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
  12. # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
  13. # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
  14.  
  15.  
  16. # Get next worker's id
  17. def next_id(current_id, Global):
  18. if current_id == Global.worker_num:
  19. return 1
  20. else:
  21. return current_id + 1
  22.  
  23.  
  24. # Get previous worker's id
  25. def prev_id(current_id, Global):
  26. if current_id == 1:
  27. return Global.worker_num
  28. else:
  29. return current_id - 1
  30.  
  31.  
  32. # A subprocess use to capture frames.
  33. def capture(read_frame_list, Global):
  34. # Get a reference to webcam #0 (the default one)
  35. video_capture = cv2.VideoCapture(0)
  36. # video_capture.set(3, 640) # Width of the frames in the video stream.
  37. # video_capture.set(4, 480) # Height of the frames in the video stream.
  38. # video_capture.set(5, 30) # Frame rate.
  39. print("Width: %d, Height: %d, FPS: %d" % (video_capture.get(3), video_capture.get(4), video_capture.get(5)))
  40.  
  41. while not Global.is_exit:
  42. # If it's time to read a frame
  43. if Global.buff_num != next_id(Global.read_num, Global):
  44. # Grab a single frame of video
  45. ret, frame = video_capture.read()
  46. read_frame_list[Global.buff_num] = frame
  47. Global.buff_num = next_id(Global.buff_num, Global)
  48. else:
  49. time.sleep(0.01)
  50.  
  51. # Release webcam
  52. video_capture.release()
  53.  
  54.  
  55. # Many subprocess use to process frames.
  56. def process(worker_id, read_frame_list, write_frame_list, Global):
  57. known_face_encodings = Global.known_face_encodings
  58. known_face_names = Global.known_face_names
  59. while not Global.is_exit:
  60.  
  61. # Wait to read
  62. while Global.read_num != worker_id or Global.read_num != prev_id(Global.buff_num, Global):
  63. time.sleep(0.01)
  64.  
  65. # Delay to make the video look smoother
  66. time.sleep(Global.frame_delay)
  67.  
  68. # Read a single frame from frame list
  69. frame_process = read_frame_list[worker_id]
  70.  
  71. # Expect next worker to read frame
  72. Global.read_num = next_id(Global.read_num, Global)
  73.  
  74. # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
  75. rgb_frame = frame_process[:, :, ::-1]
  76.  
  77. # Find all the faces and face encodings in the frame of video, cost most time
  78. face_locations = face_recognition.face_locations(rgb_frame)
  79. face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
  80.  
  81. # Loop through each face in this frame of video
  82. for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
  83. # See if the face is a match for the known face(s)
  84. matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
  85.  
  86. name = "Unknown"
  87.  
  88. # If a match was found in known_face_encodings, just use the first one.
  89. if True in matches:
  90. first_match_index = matches.index(True)
  91. name = known_face_names[first_match_index]
  92.  
  93. # Draw a box around the face
  94. cv2.rectangle(frame_process, (left, top), (right, bottom), (0, 0, 255), 2)
  95.  
  96. # Draw a label with a name below the face
  97. cv2.rectangle(frame_process, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  98. font = cv2.FONT_HERSHEY_DUPLEX
  99. cv2.putText(frame_process, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  100.  
  101. # Wait to write
  102. while Global.write_num != worker_id:
  103. time.sleep(0.01)
  104.  
  105. # Send frame to global
  106. write_frame_list[worker_id] = frame_process
  107.  
  108. # Expect next worker to write frame
  109. Global.write_num = next_id(Global.write_num, Global)
  110.  
  111.  
  112. if __name__ == '__main__':
  113.  
  114. # Global variables
  115. Global = Manager().Namespace()
  116. Global.buff_num = 1
  117. Global.read_num = 1
  118. Global.write_num = 1
  119. Global.frame_delay = 0
  120. Global.is_exit = False
  121. read_frame_list = Manager().dict()
  122. write_frame_list = Manager().dict()
  123.  
  124. # Number of workers (subprocess use to process frames)
  125. worker_num = cpu_count()
  126. Global.worker_num = worker_num
  127.  
  128. # Subprocess list
  129. p = []
  130.  
  131. # Create a subprocess to capture frames
  132. p.append(Process(target=capture, args=(read_frame_list, Global)))
  133. p[0].start()
  134.  
  135. # Load a sample picture and learn how to recognize it.
  136. obama_image = face_recognition.load_image_file("obama.jpg")
  137. obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
  138.  
  139. # Load a second sample picture and learn how to recognize it.
  140. biden_image = face_recognition.load_image_file("biden.jpg")
  141. biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
  142.  
  143. # Create arrays of known face encodings and their names
  144. Global.known_face_encodings = [
  145. obama_face_encoding,
  146. biden_face_encoding
  147. ]
  148. Global.known_face_names = [
  149. "Barack Obama",
  150. "Joe Biden"
  151. ]
  152.  
  153. # Create workers
  154. for worker_id in range(1, worker_num + 1):
  155. p.append(Process(target=process, args=(worker_id, read_frame_list, write_frame_list, Global)))
  156. p[worker_id].start()
  157.  
  158. # Start to show video
  159. last_num = 1
  160. fps_list = []
  161. tmp_time = time.time()
  162. while not Global.is_exit:
  163. while Global.write_num != last_num:
  164. last_num = int(Global.write_num)
  165.  
  166. # Calculate fps
  167. delay = time.time() - tmp_time
  168. tmp_time = time.time()
  169. fps_list.append(delay)
  170. if len(fps_list) > 5 * worker_num:
  171. fps_list.pop(0)
  172. fps = len(fps_list) / numpy.sum(fps_list)
  173. print("fps: %.2f" % fps)
  174.  
  175. # Calculate frame delay, in order to make the video look smoother.
  176. # When fps is higher, should use a smaller ratio, or fps will be limited in a lower value.
  177. # Larger ratio can make the video look smoother, but fps will hard to become higher.
  178. # Smaller ratio can make fps higher, but the video looks not too smoother.
  179. # The ratios below are tested many times.
  180. if fps < 6:
  181. Global.frame_delay = (1 / fps) * 0.75
  182. elif fps < 20:
  183. Global.frame_delay = (1 / fps) * 0.5
  184. elif fps < 30:
  185. Global.frame_delay = (1 / fps) * 0.25
  186. else:
  187. Global.frame_delay = 0
  188.  
  189. # Display the resulting image
  190. cv2.imshow('Video', write_frame_list[prev_id(Global.write_num, Global)])
  191.  
  192. # Hit 'q' on the keyboard to quit!
  193. if cv2.waitKey(1) & 0xFF == ord('q'):
  194. Global.is_exit = True
  195. break
  196.  
  197. time.sleep(0.01)
  198.  
  199. # Quit
  200. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement