Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import face_recognition
- import imutils
- input_video = cv2.VideoCapture('clowns.mp4')
- clown = cv2.imread("clown-face-emoji-by-twitter.png")
- fps = int(input_video.get(5))
- out = cv2.VideoWriter('outpy.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (int(input_video.get(4)), int(input_video.get(3))))
- def clownify():
- while True:
- # Grab a single frame of video
- ret, frame = input_video.read()
- # Quit when the input video file ends
- if not ret:
- break
- frame = imutils.rotate_bound(frame, 90)
- frame = cv2.flip(frame, 1)
- # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
- rgb_frame = frame[:, :, ::-1]
- # Find all the faces and face encodings in the current frame of video
- face_locations = face_recognition.face_locations(rgb_frame)
- # Label the results
- for (top, right, bottom, left) in face_locations:
- resized_clown = cv2.resize(clown, (right - left, bottom - top), interpolation=cv2.INTER_AREA)
- # Draw a box around the face
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
- # put a clown over the face
- frame[top:bottom, left:right] = resized_clown
- # write frame out to video
- out.write(frame)
- # quit if q keypress is detected
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # cleanup stuff
- input_video.release()
- cv2.destroyAllWindows()
- # function call because I'm a dumbass
- clownify()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement