-
Notifications
You must be signed in to change notification settings - Fork 24
/
filters.py
138 lines (96 loc) · 3.56 KB
/
filters.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Face filters (Snapchat like) using OpenCV
# @author:- Kunal Gupta (cite as kg777)
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
import numpy as np
import os
import subprocess
cascPath = "haarcascade_frontalface_default.xml" # for face detection
if not os.path.exists(cascPath):
subprocess.call(['./download_filters.sh'])
else:
print('Filters already exist!')
faceCascade = cv2.CascadeClassifier(cascPath)
log.basicConfig(filename='webcam.log',level=log.INFO)
video_capture = cv2.VideoCapture(0)
anterior = 0
mst = cv2.imread('moustache.png')
hat = cv2.imread('cowboy_hat.png')
dog = cv2.imread('dog_filter.png')
def put_moustache(mst,fc,x,y,w,h):
face_width = w
face_height = h
mst_width = int(face_width*0.4166666)+1
mst_height = int(face_height*0.142857)+1
mst = cv2.resize(mst,(mst_width,mst_height))
for i in range(int(0.62857142857*face_height),int(0.62857142857*face_height)+mst_height):
for j in range(int(0.29166666666*face_width),int(0.29166666666*face_width)+mst_width):
for k in range(3):
if mst[i-int(0.62857142857*face_height)][j-int(0.29166666666*face_width)][k] <235:
fc[y+i][x+j][k] = mst[i-int(0.62857142857*face_height)][j-int(0.29166666666*face_width)][k]
return fc
def put_hat(hat,fc,x,y,w,h):
face_width = w
face_height = h
hat_width = face_width+1
hat_height = int(0.35*face_height)+1
hat = cv2.resize(hat,(hat_width,hat_height))
for i in range(hat_height):
for j in range(hat_width):
for k in range(3):
if hat[i][j][k]<235:
fc[y+i-int(0.25*face_height)][x+j][k] = hat[i][j][k]
return fc
def put_dog_filter(dog,fc,x,y,w,h):
face_width = w
face_height = h
dog = cv2.resize(dog,(int(face_width*1.5),int(face_height*1.75)))
for i in range(int(face_height*1.75)):
for j in range(int(face_width*1.5)):
for k in range(3):
if dog[i][j][k]<235:
fc[y+i-int(0.375*h)-1][x+j-int(0.25*w)][k] = dog[i][j][k]
return fc
ch = 0
print "Select Filter:1.) Hat 2.) Moustache 3.) Hat and Moustache 4.) Dog Filter"
ch = int(raw_input())
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(40,40)
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
#cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
#cv2.putText(frame,"Person Detected",(x,y),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2)
if ch==2:
frame = put_moustache(mst,frame,x,y,w,h)
elif ch==1:
frame = put_hat(hat,frame,x,y,w,h)
elif ch==3:
frame = put_moustache(mst,frame,x,y,w,h)
frame = put_hat(hat,frame,x,y,w,h)
else:
frame = put_dog_filter(dog,frame,x,y,w,h)
if anterior != len(faces):
anterior = len(faces)
log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()