Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import cv2
  2. import face_recognition
  3. import imutils
  4.  
  5. input_video = cv2.VideoCapture('clowns.mp4')
  6.  
  7. clown = cv2.imread("clown-face-emoji-by-twitter.png")
  8. fps = int(input_video.get(5))
  9. out = cv2.VideoWriter('outpy.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (int(input_video.get(4)), int(input_video.get(3))))
  10.  
  11.  
  12. def clownify():
  13.  
  14. while True:
  15. # Grab a single frame of video
  16. ret, frame = input_video.read()
  17.  
  18. # Quit when the input video file ends
  19. if not ret:
  20. break
  21.  
  22. frame = imutils.rotate_bound(frame, 90)
  23. frame = cv2.flip(frame, 1)
  24. # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
  25. rgb_frame = frame[:, :, ::-1]
  26.  
  27. # Find all the faces and face encodings in the current frame of video
  28. face_locations = face_recognition.face_locations(rgb_frame)
  29.  
  30. # Label the results
  31. for (top, right, bottom, left) in face_locations:
  32.  
  33. resized_clown = cv2.resize(clown, (right - left, bottom - top), interpolation=cv2.INTER_AREA)
  34. # Draw a box around the face
  35. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  36. # put a clown over the face
  37. frame[top:bottom, left:right] = resized_clown
  38. # write frame out to video
  39. out.write(frame)
  40. # quit if q keypress is detected
  41. if cv2.waitKey(1) & 0xFF == ord('q'):
  42. break
  43. # cleanup stuff
  44. input_video.release()
  45. cv2.destroyAllWindows()
  46.  
  47.  
  48. # function call because I'm a dumbass
  49. clownify()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement