-
Notifications
You must be signed in to change notification settings - Fork 158
/
testing_webcam_flask.py
125 lines (94 loc) · 4.02 KB
/
testing_webcam_flask.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import sys
import argparse
import cv2
from libfaceid.detector import FaceDetectorModels, FaceDetector
from libfaceid.encoder import FaceEncoderModels, FaceEncoder
# Use flask for web app
from flask import Flask, render_template, Response
app = Flask(__name__)
# Set the input directories
INPUT_DIR_DATASET = "datasets"
INPUT_DIR_MODEL_DETECTION = "models/detection/"
INPUT_DIR_MODEL_ENCODING = "models/encoding/"
INPUT_DIR_MODEL_TRAINING = "models/training/"
INPUT_DIR_MODEL_ESTIMATION = "models/estimation/"
# Set width and height
RESOLUTION_QVGA = (320, 240)
RESOLUTION_VGA = (640, 480)
RESOLUTION_HD = (1280, 720)
RESOLUTION_FULLHD = (1920, 1080)
def cam_init(cam_index, width, height):
cap = cv2.VideoCapture(cam_index)
if sys.version_info < (3, 0):
cap.set(cv2.cv.CV_CAP_PROP_FPS, 30)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, height)
else:
cap.set(cv2.CAP_PROP_FPS, 30)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
return cap
def label_face(frame, face_rect, face_id, confidence):
(x, y, w, h) = face_rect
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255), 1)
if face_id is not None:
cv2.putText(frame, "{} {:.2f}%".format(face_id, confidence),
(x+5,y+h-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
def process_facerecognition():
cam_index = 0
cam_resolution = RESOLUTION_QVGA
model_detector=FaceDetectorModels.HAARCASCADE
# model_detector=FaceDetectorModels.DLIBHOG
# model_detector=FaceDetectorModels.DLIBCNN
# model_detector=FaceDetectorModels.SSDRESNET
# model_detector=FaceDetectorModels.MTCNN
# model_detector=FaceDetectorModels.FACENET
model_recognizer=FaceEncoderModels.LBPH
# model_recognizer=FaceEncoderModels.OPENFACE
# model_recognizer=FaceEncoderModels.DLIBRESNET
# model_recognizer=FaceEncoderModels.FACENET
# Initialize the camera
camera = cam_init(cam_index, cam_resolution[0], cam_resolution[1])
try:
# Initialize face detection
face_detector = FaceDetector(model=model_detector, path=INPUT_DIR_MODEL_DETECTION)
# Initialize face recognizer
face_encoder = FaceEncoder(model=model_recognizer, path=INPUT_DIR_MODEL_ENCODING, path_training=INPUT_DIR_MODEL_TRAINING, training=False)
except:
face_encoder = None
print("Warning, check if models and trained dataset models exists!")
face_id, confidence = (None, 0)
while (True):
# Capture frame from webcam
ret, frame = camera.read()
if frame is None:
print("Error, check if camera is connected!")
break
# Detect and identify faces in the frame
faces = face_detector.detect(frame)
for (index, face) in enumerate(faces):
(x, y, w, h) = face
# Indentify face based on trained dataset (note: should run facial_recognition_training.py)
if face_encoder is not None:
face_id, confidence = face_encoder.identify(frame, (x, y, w, h))
# Set text and bounding box on face
label_face(frame, (x, y, w, h), face_id, confidence)
# Process 1 face only
break
# Display updated frame to web app
yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', frame)[1].tobytes() + b'\r\n\r\n')
# Release the camera
camera.release()
cv2.destroyAllWindows()
# Initialize for web app
@app.route('/')
def index():
return render_template('web_app_flask.html')
# Entry point for web app
@app.route('/video_viewer')
def video_viewer():
return Response(process_facerecognition(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
print("\n\nNote: Open browser and type http://127.0.0.1:5000/ or http://ip_address:5000/ \n\n")
# Run flask for web app
app.run(host='0.0.0.0', threaded=True, debug=True)