-
Notifications
You must be signed in to change notification settings - Fork 2
/
camera.py
93 lines (75 loc) · 3.87 KB
/
camera.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
import cv2
from model import FacialExpressionModel
import numpy as np
<<<<<<< HEAD
facec = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
model = FacialExpressionModel("model.json", "model_weights.h5")
font = cv2.FONT_HERSHEY_SIMPLEX
class VideoCamera(object):
def __init__(self):
#self.video = cv2.VideoCapture('/home/rhyme/Desktop/Project/videos/facial_exp.mkv')
self.video = cv2.VideoCapture(0)
=======
import pandas as pd
import datetime
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import seaborn as sns
import io
facec = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') #reading the Haar Cascade file
model = FacialExpressionModel("model.json", "model_weights.h5") #Passing the model json file and the weights to the FacialExpressionModel object
font = cv2.FONT_HERSHEY_SIMPLEX #Setting the font to the OpenCV
EMOTIONS_LIST = ["Angry", "Disgust","Fear", "Happy","Neutral", "Sad","Surprise"] #emotions encoding
def plot_preds(preds):
'''
A functions that takes in predictions and creates a file of the bar plot as a binary
'''
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
sns.barplot(x = EMOTIONS_LIST,
y = preds.reshape(7,),
ax=axis)
output = io.BytesIO() #converting the chart to binary
FigureCanvas(fig).print_png(output)
with open("charts/chart", "wb") as file:
file.write(output.getvalue())
class VideoCamera(object):
def __init__(self,logging,link=0):
# link = r'./Face-Emotion-Recognition/videos/facial_exp.mkv'
self.video = cv2.VideoCapture(link)
self.logging = logging
self.log = pd.read_pickle(f'logs/{logging}.pkl')
>>>>>>> e5151adb4 (final)
def __del__(self):
self.video.release()
# returns camera frames along with bounding boxes and predictions
def get_frame(self):
_, fr = self.video.read()
<<<<<<< HEAD
gray_fr = cv2.cvtColor(fr, cv2.COLOR_BGR2GRAY)
faces = facec.detectMultiScale(gray_fr, 1.3, 5)
for (x, y, w, h) in faces:
fc = gray_fr[y:y+h, x:x+w]
roi = cv2.resize(fc, (48, 48))
pred = model.predict_emotion(roi[np.newaxis, :, :, np.newaxis])
cv2.putText(fr, pred, (x, y), font, 1, (255, 255, 0), 2)
cv2.rectangle(fr,(x,y),(x+w,y+h),(255,0,0),2)
_, jpeg = cv2.imencode('.jpg', fr)
return jpeg.tobytes()
=======
gray_fr = cv2.cvtColor(fr, cv2.COLOR_BGR2GRAY) #Converting the frame into gray scale
faces = facec.detectMultiScale(gray_fr, 1.3, 5)
for (x, y, w, h) in faces:
fc = gray_fr[y:y+h, x:x+w]
roi = cv2.resize(fc, (48, 48)) #Resizing the face into 48*48 to match our model
preds = model.predict_emotion(roi[np.newaxis, :, :, np.newaxis]) #get the prediction
pred = EMOTIONS_LIST[np.argmax(preds)] #Getting the emotion name with the highest prediction/probability
preds = preds.reshape(1,7)
plot_preds(preds) #Plotting the prediction and saving the chart into the folder
self.log = self.log.append(pd.DataFrame(list(preds), columns=EMOTIONS_LIST, index=[datetime.datetime.now()]), ignore_index=True)
cv2.putText(fr, pred, (x, y), font, 1, (255, 255, 0), 2) #Writing the prediction on the box
cv2.rectangle(fr,(x,y),(x+w,y+h),(255,0,0),2) #Drawing the rectangle box on the frame
_, jpeg = cv2.imencode('.jpg', fr) #Encoding the frame as a jpg file
pd.to_pickle(self.log, f'logs/{self.logging}.pkl') #Adding the predictions into a PKL file for future analyses
return jpeg.tobytes() #Retruning the image as a bytes object
>>>>>>> e5151adb4 (final)