Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. import face_recognition
  2. import cv2
  3.  
  4. # This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
  5. # other example, but it includes some basic performance tweaks to make things run a lot faster:
  6. #   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
  7. #   2. Only detect faces in every other frame of video.
  8.  
  9. # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
  10. # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
  11. # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
  12.  
  13. # Get a reference to webcam #0 (the default one)
  14. video_capture = cv2.VideoCapture(0)
  15.  
  16. # Load a sample picture and learn how to recognize it.
  17. omar_image = face_recognition.load_image_file("omar.jpg")
  18. omar_face_encoding = face_recognition.face_encodings(omar_image)[0]
  19.  
  20. # Load a second sample picture and learn how to recognize it.
  21. ibrahim_image = face_recognition.load_image_file("ibra.jpg")
  22. ibrahim_face_encoding = face_recognition.face_encodings(ibrahim_image)[0]
  23.  
  24. # Create arrays of known face encodings and their names
  25. known_face_encodings = [
  26.     omar_face_encoding,
  27.     ibrahim_face_encoding
  28. ]
  29. known_face_names = [
  30.     "Ezzat",
  31.     "Ibrahim"
  32. ]
  33.  
  34. # Initialize some variables
  35. face_locations = []
  36. face_encodings = []
  37. face_names = []
  38. process_this_frame = True
  39.  
  40. while True:
  41.     # Grab a single frame of video
  42.     ret, frame = video_capture.read()
  43.  
  44.     # Resize frame of video to 1/4 size for faster face recognition processing
  45.     small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
  46.  
  47.     # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
  48.     rgb_small_frame = small_frame[:, :, ::-1]
  49.  
  50.     # Only process every other frame of video to save time
  51.     if process_this_frame:
  52.         # Find all the faces and face encodings in the current frame of video
  53.         face_locations = face_recognition.face_locations(rgb_small_frame)
  54.         face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
  55.  
  56.         face_names = []
  57.         for face_encoding in face_encodings:
  58.             # See if the face is a match for the known face(s)
  59.             matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
  60.             name = "Unknown"
  61.  
  62.             # If a match was found in known_face_encodings, just use the first one.
  63.             if True in matches:
  64.                 first_match_index = matches.index(True)
  65.                 name = known_face_names[first_match_index]
  66.  
  67.             face_names.append(name)
  68.  
  69.     process_this_frame = not process_this_frame
  70.  
  71.  
  72.     # Display the results
  73.     for (top, right, bottom, left), name in zip(face_locations, face_names):
  74.         # Scale back up face locations since the frame we detected in was scaled to 1/4 size
  75.         top *= 4
  76.         right *= 4
  77.         bottom *= 4
  78.         left *= 4
  79.  
  80.         # Draw a box around the face
  81.         cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  82.  
  83.         # Draw a label with a name below the face
  84.         cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  85.         font = cv2.FONT_HERSHEY_DUPLEX
  86.         cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  87.  
  88.     # Display the resulting image
  89.     cv2.imshow('Video', frame)
  90.  
  91.     # Hit 'q' on the keyboard to quit!
  92.     if cv2.waitKey(1) & 0xFF == ord('q'):
  93.         break
  94.  
  95. # Release handle to the webcam
  96. video_capture.release()
  97. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement