-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamera.py
31 lines (24 loc) · 1.11 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
from keras.models import model_from_json
from keras.preprocessing import image as im
import cv2
import classifier
class VideoCamera(object):
def __init__(self):
# Using OpenCV to capture from device 0.
self.video = cv2.VideoCapture(0)
# Load Haarcascade File
self.face_detector = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_default.xml")
# Load the Model and Weights
self.model = model_from_json(open("ml_folder/facial_expression_model_structure.json", "r").read())
self.model.load_weights('ml_folder/facial_expression_model_weights.h5')
def __del__(self):
self.video.release()
def get_frame(self):
success, frame = self.video.read()
if success:
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
frame = classifier.classify(frame, self.face_detector, self.model)
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()