creating snapchat filter in python code example
Example: python build a snapchat filter
import argparse
import cv2
from imutils.video import VideoStream
from imutils import face_utils, translate, resize
import time
import dlib
import numpy as np
parser = argparse.ArgumentParser()
parser.add_argument("-predictor", required=True, help="path to predictor")
args = parser.parse_args()
print("starting program.")
print("'s' starts drawing eyes.")
print("'r' to toggle recording image, and 'q' to quit")
vs = VideoStream().start()
time.sleep(1.5)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args.predictor)
recording = False
counter = 0
class EyeList(object):
def __init__(self, length):
self.length = length
self.eyes = []
def push(self, newcoords):
if len(self.eyes) < self.length:
self.eyes.append(newcoords)
else:
self.eyes.pop(0)
self.eyes.append(newcoords)
def clear(self):
self.eyes = []
eyelist = EyeList(10)
eyeSnake = False
frame = vs.read()
frame = resize(frame, width=800)
eyelayer = np.zeros(frame.shape, dtype='uint8')
eyemask = eyelayer.copy()
eyemask = cv2.cvtColor(eyemask, cv2.COLOR_BGR2GRAY)
translated = np.zeros(frame.shape, dtype='uint8')
translated_mask = eyemask.copy()
while True:
frame = vs.read()
frame = resize(frame, width=800)
eyelayer.fill(0)
eyemask.fill(0)
translated.fill(0)
translated_mask.fill(0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
if eyeSnake:
for rect in rects:
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
leftEye = shape[36:42]
rightEye = shape[42:48]
cv2.fillPoly(eyemask, [leftEye], 255)
cv2.fillPoly(eyemask, [rightEye], 255)
eyelayer = cv2.bitwise_and(frame, frame, mask=eyemask)
x, y, w, h = cv2.boundingRect(eyemask)
eyelist.push([x, y])
for i in reversed(eyelist.eyes):
translated1 = translate(eyelayer, i[0] - x, i[1] - y)
translated1_mask = translate(eyemask, i[0] - x, i[1] - y)
translated_mask = np.maximum(translated_mask, translated1_mask)
translated = cv2.bitwise_and(translated, translated, mask=255 - translated1_mask)
translated += translated1
frame = cv2.bitwise_and(frame, frame, mask=255 - translated_mask)
frame += translated
cv2.imshow("eye glitch", frame)
key = cv2.waitKey(1) & 0xFF
if recording:
cv2.imwrite("image_seq/%05d.png" % counter, frame)
counter += 1
if key == ord("q"):
break
if key == ord("s"):
eyeSnake = not eyeSnake
eyelist.clear()
if key == ord("r"):
recording = not recording
cv2.destroyAllWindows()
vs.stop()