-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_prediction.py
75 lines (58 loc) · 2.69 KB
/
model_prediction.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
import cv2
import os
from get_data import *
from model_generation import *
#Image captured from a live video source. Reshaped and used to predict
#A timer array keeps count of the emotions and returns the most shown emotion after 10 seconds
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def timer(emotion, timer_array):
count = timer_array.get(emotion)
count += 1
timer_array[emotion] = count
return timer_array
def reshape_image(image):
requiredsize = (48, 48)
image = cv2.resize(image, requiredsize, interpolation=cv2.INTER_AREA)
image = image.reshape(-1, image.shape[0], image.shape[1], 1)
image = image.astype('float32')/32
return image
def predict(image, model, timer_array):
output = model.predict(image)
index = np.argmax(output)
emotion = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Suprise", "Neutral"]
color = [(0, 0, 255), (255, 0, 255), (0, 0, 255), (0, 255, 0), (0, 255, 255), (0, 255, 255), (0, 255, 0)]
timer_array = timer(emotion[index], timer_array)
return emotion[index], color[index], timer_array
def get_face(model):
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# cv2_base_dir = os.path.dirname(resource_path(cv2.__file__))
# haar_model = os.path.join(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cv2.ocl.setUseOpenCL(False)
timer_array = {"Angry":0, "Disgust":0, "Fear":0, "Happy":0, "Sad":0, "Suprise":0, "Neutral":0}
live_video = cv2.VideoCapture(0)
while True:
_, frame = live_video.read()
frame = cv2.flip(frame, flipCode=1)
face_cascade = cv2.CascadeClassifier(resource_path("Extras/haarcascade_frontalface_default.xml"))
faces = face_cascade.detectMultiScale(frame, scaleFactor=1.2, minNeighbors=5, minSize=(20, 20))
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 0)
face = frame[y:y+h, x:x+w]
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
face = reshape_image(face)
text, color, timer_array = predict(face, model, timer_array)
cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
cv2.rectangle(frame, (x+w-150, y+h), (x+w, y+h+30), color, -1)
cv2.putText(frame, text, (x+w-100, y+h+20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.imshow("Feed", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
live_video.release()
cv2.destroyAllWindows()
average_emotion = max(timer_array, key=timer_array.get)
return average_emotion