-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilter.py
More file actions
45 lines (38 loc) · 1.28 KB
/
Filter.py
File metadata and controls
45 lines (38 loc) · 1.28 KB
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
import cv2
cap = cv2.VideoCapture(0)
eye_cascade = cv2.CascadeClassifier('frontalEyes35x16.xml')
nose_cascade = cv2.CascadeClassifier('Nose18x15.xml')
mustache = cv2.imread("mustache.png",-1)
glasses = cv2.imread("glasses.png",-1)
while True:
ret,img = cap.read()
if ret == False:
continue
eyes = eye_cascade.detectMultiScale(img,1.1,5)
nose = nose_cascade.detectMultiScale(img,1.1,5)
for (x,y,w,h) in eyes:
x1 = x-10
y1 = y-15
x2 = x+h+10
y2 = y+w+w//4
glasses = cv2.resize(glasses,(x2-x1,y2-y1))
alpha_mask = glasses[:,:,3]/255.0
alpha_inv = 1.0 - alpha_mask
for c in range(3):
img[y1:y2,x1:x2,c] = alpha_mask * glasses[:,:,c] + alpha_inv * img[y1:y2,x1:x2,c]
for (x,y,w,h) in nose:
x1 = x-w//4
y1 = y+h//3+w//7
x2 = x+h+h//2
y2 = y+w+h//2
mustache = cv2.resize(mustache,(x2-x1,y2-y1))
alpha_mask = mustache[:,:,3]/255.0
alpha_inv = 1.0 - alpha_mask
for c in range(3):
img[y1:y2,x1:x2,c] = alpha_mask * mustache[:,:,c] + alpha_inv * img[y1:y2,x1:x2,c]
cv2.imshow("Video Frame",img)
key_pressed = cv2.waitKey(1) & 0xFF
if key_pressed == ord('q'):
break
cap.release()
cv2.destroyAllWindows()