-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.py
65 lines (48 loc) · 1.99 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
import argparse
import cv2
import torch
from emotion import EmotionRecognizer
from detect_face import FaceDetector
parser = argparse.ArgumentParser(description='FaceAnalizer')
parser.add_argument('--cpu', action="store_true", default=False, help='Use cpu inference')
parser.add_argument('--confidence_threshold', default=0.05, type=float, help='confidence_threshold')
parser.add_argument('--top_k', default=5000, type=int, help='top_k')
parser.add_argument('--nms_threshold', default=0.3, type=float, help='nms_threshold')
parser.add_argument('--keep_top_k', default=750, type=int, help='keep_top_k')
parser.add_argument('--vis_thres', default=0.5, type=float, help='visualization_threshold')
args = parser.parse_args()
face_detector = FaceDetector("traced_models/face.pt", args)
emotion_detector = EmotionRecognizer("traced_models/emotion.pt", args)
device = torch.cuda.current_device()
class VideoCamera(object):
def __init__(self):
# capturing video
self.video = cv2.VideoCapture(0)
def __del__(self):
# releasing camera
self.video.release()
def get_frame(self):
# extracting frames
ret, frame = self.video.read()
list_of_detections = face_detector.detect_face(frame)
frame = emotion_detector.recognize_faces(frame, list_of_detections)
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
from flask import Flask, render_template, Response
app = Flask(__name__)
@app.route('/')
def index():
# rendering webpage
return render_template('index.html')
def gen(camera):
while True:
# get camera frame
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n') \
@app.route('/video_vid')
def video_feed():
return Response(gen(VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
# defining server ip address and port
app.run(host='0.0.0.0', port='5000', debug=True)