-
Notifications
You must be signed in to change notification settings - Fork 1
/
working_interface.py
57 lines (35 loc) · 1.47 KB
/
working_interface.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
import cv2
import tensorflow as tf
class_names = ["Angry", "Happy", "Sad", "Surprise"]
model = tf.keras.models.load_model("model.h5")
def detect_emotion(frame):
emotion = list(model.predict(tf.expand_dims(frame, axis=0)))
num = max(emotion[0])
idx = list(emotion[0]).index(num)
return idx, num
def preprocess(frame):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame, (48, 48))
frame = frame / 255.
return frame
def detect_face(frame):
cascade = cv2.CascadeClassifier('C:\\Users\\SOFI\\Desktop\\project_end2022\\haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
return gray, frame, faces
video = cv2.VideoCapture(0)
while (True):
_, frame = video.read()
if _:
gray, frame, coordinates = detect_face(frame)
if coordinates is not None:
process_img = preprocess(gray)
idx, conf = detect_emotion(process_img)
class_name = class_names[idx]
if conf > 0.3 and coordinates is not None:
cv2.putText(frame, class_name, (coordinates[0][0], coordinates[0][1]), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 255, 0), 2)
cv2.imshow("Window", frame)
cv2.waitKey(0)