-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask.py
57 lines (42 loc) · 1.24 KB
/
mask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import cv2
from PIL import Image
import numpy as np
# thug life meme mask image path
maskPath = "mask.png"
# haarcascade path
cascPath = "face.xml"
# cascade classifier object
faceCascade = cv2.CascadeClassifier(cascPath)
# Open mask as PIL image
mask = Image.open(maskPath)
def thug_mask(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 imageto PIL image
background = Image.fromarray(image)
for (x,y,w,h) in faces:
# resize mask
resized_mask = mask.resize((w,h), Image.ANTIALIAS)
# define offset for mask
offset = (x,y)
# pask mask on background
background.paste(resized_mask, offset, mask=resized_mask)
# return background as cv2 image
return np.asarray(background)
# VideoCapture object
cap = cv2.VideoCapture(cv2.CAP_ANY)
while True:
# read return value and frame
ret, frame = cap.read()
if ret == True:
# show frame with thug life mask
cv2.imshow('Live', thug_mask(frame))
# chck if esc key is pressed
if cv2.waitKey(1) == 27:
break
# release cam
cap.release()
# destroy all open opencv windows
cv2.destroyAllWindows()