-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.py
160 lines (107 loc) · 4.09 KB
/
test.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pandas as pd
import numpy as np
import cv2
from tensorflow.keras.models import load_model
from PyQt5.QtWidgets import QFileDialog
from datetime import datetime
import seaborn as sns
import easygui
df = pd.DataFrame(columns=['time', 'emotion'])
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
model = load_model('model.h5')
emotion_dict = {0: 'anger', 1: 'contempt', 2: 'disgust',
3: 'fear', 4: 'happiness',
5: 'sadness', 6: 'surprise'}
j = 0
print('\n Select your choice: ')
print('\n 1) Capture live feed using webcam')
print('\n 2) Select a video file ')
print('\n 3) Select a image file ')
print('\n Enter Your Choice :')
choice = int(input('Choice: \n'))
if choice == 1:
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
elif choice == 2:
path = easygui.fileopenbox(default='*')
cap = cv2.VideoCapture(path)
elif choice == 3:
j = 1
pass
else:
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
def convert_image(image):
image_arr = []
pic = cv2.resize(image, (48, 48))
image_arr.append(pic)
image_arr = np.array(image_arr)
image_arr = image_arr.astype('float32')
image_arr /= 255
ans = model.predict_classes(image_arr)[0]
return ans
if j == 0:
while cap.isOpened():
time_rec = datetime.now()
ret, frame = cap.read()
if ret:
gray = cv2.flip(frame, 1)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(gray, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
prediction = int(convert_image(roi_gray))
emotion = emotion_dict[prediction]
df = df.append({'time': time_rec, 'emotion': emotion}, ignore_index=True)
cv2.putText(gray, emotion, (x + 20, y - 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255),
2 , cv2.LINE_AA
)
cv2.namedWindow('Video', cv2.WINDOW_KEEPRATIO)
cv2.imshow('Video', gray)
cv2.resizeWindow('Video', 1000, 600)
if cv2.waitKey(1) == 27: # press ESC to break
cap.release()
cv2.destroyAllWindows()
break
else:
break
else:
path = easygui.fileopenbox(default='*')
gray = cv2.imread(path)
time_rec = datetime.now()
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(gray, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
prediction = int(convert_image(roi_gray))
emotion = emotion_dict[prediction]
df = df.append({'time': time_rec, 'emotion': emotion}, ignore_index=True)
cv2.putText(gray, emotion, (x + 20, y - 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255),
2 , cv2.LINE_AA
)
cv2.namedWindow('Video', cv2.WINDOW_KEEPRATIO)
cv2.imshow('Video', gray)
cv2.resizeWindow('Video', 1000, 600)
if cv2.waitKey(1) == 27: # press ESC to break
break
else:
break
print(df.head())
print(df.shape)
import matplotlib.pyplot as plt
emo_data = df.groupby('emotion').size()
print(emo_data, '\n')
emotion_dict_count = {'anger': 0, 'contempt': 0, 'disgust': 0,
'fear': 0, 'happiness': 0,
'sadness': 0, 'surprise': 0}
for i in df['emotion']:
emotion_dict_count[str(i)] += 1
emo_count = [x for x in emotion_dict_count.values()]
emo_name = [x for x in emotion_dict_count.keys()]
for i,j in zip(emo_count, emo_name):
if i == 0:
emo_count.remove(i)
emo_name.remove(j)
plt.pie(x=emo_count, labels=emo_name, autopct='%1.2f', startangle=90)
# print('\n emo_count :',emo_count)
# print('\n emo_name :',emo_name)
plt.title("Emotions Recorded ")
plt.show()