-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectBanna.py
89 lines (54 loc) · 1.94 KB
/
detectBanna.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
__author__ = 'max'
import numpy as np
import cv2
classifier = "./classifier/"
img = "./img/"
lower_blue = np.array([242,26,26,255])
upper_blue = np.array([242,30,30,255])
hat = cv2.imread(img+'hat.png')
hat = cv2.resize(hat,(200,200))
# bluehat = cv2.inRange(hat , lower_blue, upper_blue)
# res = cv2.bitwise_and(hat, hat, mask=bluehat)
# hat = cv2.cvtColor(hat, cv2.COLOR_HSV2RGB)
tmp = cv2.cvtColor(hat, cv2.COLOR_BGR2GRAY)
_,alpha = cv2.threshold(tmp,0,255,cv2.THRESH_BINARY)
b, g, r = cv2.split(hat)
rgba = [b,g,r, alpha]
hat = cv2.merge(rgba,4)
# banana_cascade = cv2.CascadeClassifier(folder+'banana_classifier.xml')
eyes_cascade = cv2.CascadeClassifier(classifier + 'face.xml')
face_cascade = cv2.CascadeClassifier(classifier + 'fullface.xml')
cap = cv2.VideoCapture(0)
while(1):
# Take each frame
_, frame = cap.read()
# define range of blue color in HSV
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, alpha1 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
b1 ,g1, r1 = cv2.split(frame)
rgba1 = [b1,g1,r1,alpha1]
frame = cv2.merge(rgba1,4)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eyes_cascade.detectMultiScale(roi_gray, 1.6, 4)
for (x1, y1, w1, h1) in eyes:
cv2.rectangle(frame, (x + x1, y + y1), (x + x1 + w1, y + y1 + h1), (255, 255, 0), 2)
##let put a hat on now
xh1 = x
xh2 = x + 200
yh1 = y -200
yh2 = y
# dst = cv2.add(hat,frame)
# print xh1
# print xh1
if not (xh1 < 0 or xh2 > frame.shape[1] or yh1 < 0 or yh2 > frame.shape[0]):
frame[yh1:yh2,xh1:xh2] = hat
#cv2.imshow("sup",hat)
cv2.imshow('Smile!',frame)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()