-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoustache.py
52 lines (35 loc) · 1.47 KB
/
moustache.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
## this script adds a brown moustache to all detected faces in a photograph!
## Using Haar Cascade Classifier
## @author :- Kunal Gupta ( cite as kg777)
import cv2
import numpy as np
cascPath = "haarcascade_frontalface_default.xml" # for face detection
faceCascade = cv2.CascadeClassifier(cascPath)
mst = cv2.imread('moustache.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
source_image_path = '' ## Add the image name here
SRC = cv2.imread(source_image_path)
gray = cv2.cvtColor(SRC, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
for (x, y, w, h) in faces:
cv2.rectangle(SRC, (x, y), (x+w, y+h), (0, 255, 0), 2)
SRC = put_moustache(mst,SRC,x,y,w,h)
cv2.imshow('Moustache Face!!',SRC)
cv2.waitKey(0)
cv2.destroyAllWindows()