-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
73 lines (50 loc) · 2.18 KB
/
inference.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
## Code for real time prediction ##
import cv2
import tensorflow as tf
import numpy as np
import time
import argparse
import mlflow
parser = argparse.ArgumentParser()
parser.add_argument('--video_name', type=str, required=True)
parser.add_argument('--model_dirname', type=str, required=True)
args = parser.parse_args()
class_names = ["Angry", "Disgust", "Fear", "Happy", "Neutral", "Sad", "Surprise"]
model = mlflow.keras.load_model(f"mlruns/Fer2013-Experiment/{args.model_dirname}/artifacts/model")
cap = cv2.VideoCapture(f'testing/{args.video_name}')
faceDetect = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
# used to record the time when we processed last frame
prev_frame_time = 0
# used to record the time at which we processed current frame
new_frame_time = 0
while True:
ret, frame = cap.read()
if not ret:
break
new_frame_time = time.time()
# Calculating the fps
# fps will be number of frame processed in given time frame
# since their will be most of time error of 0.001 second
# we will be subtracting it to get more accurate result
fps = str(int(1/(new_frame_time-prev_frame_time)))
cv2.putText(frame, fps, (7, 70), cv2.FONT_HERSHEY_SIMPLEX , 3, (100, 255, 0), 3, cv2.LINE_AA)
prev_frame_time = new_frame_time
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3, 3)
for x, y, w, h in faces:
sub_face_img = frame[y : y + h, x : x + w]
resized = cv2.resize(sub_face_img, (48, 48))
normalize = resized / 255.0
reshaped = np.reshape(normalize, (1, 48, 48, 3))
result = model.predict(reshaped)
label = np.argmax(result, axis=1)[0]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 1)
cv2.rectangle(frame, (x, y), (x + w, y + h), (50, 50, 255), 2)
cv2.rectangle(frame, (x, y - 40), (x + w, y), (50, 50, 255), -1)
cv2.putText(frame, class_names[label], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
cv2.imshow("Frame", frame)
k = cv2.waitKey(1)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()