-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smile_Detector.py
129 lines (88 loc) · 2.53 KB
/
Smile_Detector.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
#!/usr/bin/env python
# coding: utf-8
# In[12]:
import pygame,dlib,time,cv2,os
from imutils.video import VideoStream
from imutils import face_utils
# In[13]:
shape_predictor="shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(shape_predictor)
# In[23]:
vs = VideoStream(src=0).start()
time.sleep(2.0)
# In[24]:
count=0
p1=[(0,0)]*68
p2=[(0,0)]*68
d=[(0,0)]*68
dist_smilo=0
diff_chx,diff_chy=0,0
pid=0
count_smile,count_eact,count_be=0,0,0
# In[22]:
while True:
frame = vs.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects=detector(gray,0)
diff_smile=0
diff_ang=0
diff_leye=0
diff_eye=0
diff_reye=0
diff_up=0
diff_change=0
if(count%2==0):
p1=p2
p2=[(0,0)]*68
d=[(0,0)]*68
cv2.imshow("frame",frame)
x49=0
y49=0
x55=0
y55=0
#print("Count smile ",count_smile)
s=0
for rect in rects:
shape=predictor(gray,rect)
shape = face_utils.shape_to_np(shape)
i=1
x1,y1,w,h = 0,0,0,0
count=count+1
for (x,y) in shape:
cv2.circle(frame,(x,y),1,(0,255,0),-1)
if(i):
cv2.putText(frame, str(i), (x, y),cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 1)
if (i==49):
x49=x
y49=y
elif(i==55):
x55=x
y55=y
dist_smile=((x49-x55)**2+(y49-y55)**2)**0.5
#print('dist_smile',dist_smile)
diff_smile = (dist_smile)-dist_smilo
if diff_smile<0:
diff_smile*=-1
#print('diff_smile',diff_smile)
#print('dist_smilo',dist_smilo)
if count==1 or diff_smile>15:
dist_smilo=dist_smile
if diff_smile<6:
dist_smilo = (dist_smilo+dist_smile)//2
if (diff_smile>10 and dist_smile>60 and dist_smilo>55 and count!=1):
#cv2.putText(frame,'Smile', (50, 50),cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 1)
s=1
#cv2.imshow("selfie1", frame)
i=i+1
if (s==1):
count_smile = count_smile+1
print("Smile ",count_smile)
s=0
cv2.imshow("Frame",frame)
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
VideoStream(src=0).stop()
cv2.destroyAllWindows()
# In[ ]: