-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLive_FER.py
81 lines (57 loc) · 2.9 KB
/
Live_FER.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
from tensorflow.keras.models import model_from_json
import cv2
import numpy as np
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load weights into new model
model.load_weights("model_weights.h5")
face_haar_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
emotions = ('Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral')
def video_detect(video_link = 0):
cap=cv2.VideoCapture(video_link)
while True:
ret,test_img=cap.read()# captures frame and returns boolean value and captured image
if not ret:
continue
gray_img= cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY)
try:
faces_detected = face_haar_cascade.detectMultiScale(gray_img, 1.32, 5)
for (x,y,w,h) in faces_detected:
cv2.rectangle(test_img,(x,y),(x+w,y+h),(255,0,0),thickness=3)
roi_gray=gray_img[y:y+w,x:x+h]#cropping region of interest i.e. face area from image
roi_gray=cv2.resize(roi_gray,(48,48))
img = roi_gray.reshape((1,48,48,1))
img = img /255.0
max_index = np.argmax(model.predict(img.reshape((1,48,48,1))), axis=-1)[0]
predicted_emotion = emotions[max_index]
cv2.putText(test_img, predicted_emotion, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
except:
pass
resized_img = cv2.resize(test_img, (1000, 700))
cv2.imshow('Facial emotion analysis ',resized_img)
if cv2.waitKey(10) == ord('s'):#wait until 's' key is pressed
break
cap.release()
cv2.destroyAllWindows()
def image_detect(img_link):
c_img = cv2.imread(img_link)
gray_img = cv2.cvtColor(c_img, cv2.COLOR_BGR2GRAY)
faces_detected = face_haar_cascade.detectMultiScale(gray_img, 1.32, 5)
for (x,y,w,h) in faces_detected:
cv2.rectangle(c_img,(x,y),(x+w,y+h),(255,0,0),thickness=3)
roi_gray=gray_img[y:y+w,x:x+h]#cropping region of interest i.e. face area from image
roi_gray=cv2.resize(roi_gray,(48,48))
img = roi_gray.reshape((1,48,48,1))
img = img /255.0
max_index = np.argmax(model.predict(img.reshape((1,48,48,1))), axis=-1)[0]
predicted_emotion = emotions[max_index]
cv2.putText(c_img, predicted_emotion, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
resized_img = cv2.resize(c_img, (1000, 700))
cv2.imshow('Facial emotion analysis ',resized_img)
cv2.imwrite("All_Emotions_Detection.jpg", resized_img)
if cv2.waitKey(0) == ord('s'):
cv2.destroyAllWindows()
image_detect("all_emotions1.jpg")
# video_detect()