-
Notifications
You must be signed in to change notification settings - Fork 0
Home
EktaBhaskar edited this page Jul 1, 2018
·
1 revision
Welcome to the hello-world wiki! import cv2 from PIL import Image import numpy as np
maskPath = "C:/Users/laptop care/Desktop/python/Snapchat-Filters-PNG-Image-180x180.png"
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath) #for eye detection of person eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
mask = Image.open(maskPath)
def thug_mask(image): """ function to add any additional filter like snapchat to input image """
# convert input image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in grayscale image
faces = faceCascade.detectMultiScale(gray, 1.15)
# convert cv2 image to PIL image
background = Image.fromarray(image)
for (x,y,w,h) in faces:
# resize filter image
resized_mask = mask.resize((w,h), Image.ANTIALIAS)
# define offset for filter image
offset = (x-10,y-10)
# paste filter image on background
background.paste(resized_mask, offset, mask=resized_mask)
# return background as cv2 image
return np.asarray(background)
cap = cv2.VideoCapture(cv2.CAP_ANY)
while True: # read return value and frame ret, frame = cap.read()
if ret == True:
# show frame with snapchat filter
cv2.imshow('Live', thug_mask(frame))
# check if esc key is pressed
if cv2.waitKey(1) == 'q':
break
cap.release()
cv2.destroyAllWindows()