-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClas_Emotions.py
126 lines (100 loc) · 3.45 KB
/
Clas_Emotions.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
# _*_ coding:utf-8 _*_
import sys
import dlib
import numpy as np
import cv2
import matplotlib.pyplot as plt
import os
import pickle
import errno
import argparse
import sys
import time
import math
import joblib
class face_dlib():
def __init__(self):
self.detector = dlib.get_frontal_face_detector()
self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
self.cap = cv2.VideoCapture(0)
self.cap.set(3, 480)
self.cnt = 0
def face(self):
test_a = 'C:/Users/jnmor/Desktop/ForSteos/face.jpg'
folder = 'C:/Users/jnmor/Desktop/ForSteos/'
try:
os.mkdir(folder)
except OSError as e:
if e.errno == errno.EEXIST:
print('Directory not created.')
else:
raise
# carga el modelo
data_filename_memmap = os.path.join(folder,'TrainedModel.sav')
print(data_filename_memmap)
#pickle.dump(data_filename_memmap, open("prot2", "w"), protocol=0)
neighb = joblib.load(data_filename_memmap)
while (self.cap.isOpened()):
flag, im_rd = self.cap.read()
k = cv2.waitKey(1)
img_gray = cv2.cvtColor(im_rd, cv2.COLOR_RGB2GRAY)
faces = self.detector(img_gray, 0)
font = cv2.FONT_HERSHEY_SIMPLEX
if (len(faces) ==1):
for k, d in enumerate(faces):
#draw red rectangle on the face
cv2.rectangle(im_rd, (d.left(), d.top()), (d.right(), d.bottom()), (0, 0, 255))
faces = img_gray[d.top():d.bottom(), d.left():d.right()]
cropped = img_gray[d.top():d.bottom(), d.left():d.right()]
cropped_cropped = cv2.resize(cropped, (48, 48))
cv2.imshow("face",cropped_cropped)
cv2.imwrite('face.jpg', cropped_cropped)
img = plt.imread(test_a)
img = np.array(img).reshape(len(img[0])**2)
img = np.array(img).reshape(1,-1)
R = neighb.predict(img)
main(R[0])
#print(R[0])
else:
print('Just one face')
continue
# enter "s" key save the image
if (k == ord('s')):
self.cnt += 1
cv2.imwrite("screenshoot" + str(self.cnt) + ".jpg", im_rd)
# enter "q" to quit
if (k == ord('q')):
break
cv2.imshow("camera", im_rd)
# free the camera
self.cap.release()
# delete the window
cv2.destroyAllWindows()
def main(R):
if R == 0:
print("Angry")
#time.sleep(3.0)
elif R == 1:
print("disgust")
#time.sleep(3.0)
elif R == 2:
print("fear")
#time.sleep(3.0)
elif R == 3:
print("happy")
#time.sleep(3.0)
elif R == 4:
print("neutral")
#time.sleep(3.0)
elif R == 5:
print("sad")
#time.sleep(3.0)
elif R == 6:
print("surprise")
#time.sleep(3.0)
else:
print("IDK")
#time.sleep(3.0)
if __name__ == "__main__":
face_d = face_dlib()
face_d.face()