-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
53 lines (47 loc) · 1.65 KB
/
utils.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
import cv2
from face_recognition import OnWebFaceRecognition, draw_annotation
import numpy as np
from PIL import Image
from io import BytesIO
import io
from imageio import imread
import base64
def generate_frames(mjson, mdt):
camera = cv2.VideoCapture(0)
fr = OnWebFaceRecognition(json_data=mjson, face_detection_threshold=mdt)
while True:
success, frame = camera.read()
if not success:
break
else:
smaller_frame = cv2.cvtColor(cv2.resize(frame, (0, 0), fx=0.5, fy=0.5), cv2.COLOR_BGR2RGB)
try:
matches = fr.recognize_faces(
image=smaller_frame, threshold=0.6, bboxes=None
)
for face_bbox, match, _ in matches:
name = match["name"] if match is not None else "Unknown"
draw_annotation(frame, name, int(1 / 0.5) * np.array(face_bbox))
except Exception:
pass
_, buffer = cv2.imencode(".jpg", frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-type: image/jpeg\r\n\r\n' + frame + b'\r\n')
def allowed_extension(filename, allowed_extensions):
if not "." in filename:
return False
else:
ext = filename.rsplit(".", 1)[1]
if ext.upper() in allowed_extensions:
return True
else:
return False
def get_f_name(filename):
if not "." in filename:
raise Exception
else:
name = filename.split(".")[0]
return name
def base64_to_cv2_image(base64_img):
return cv2.cvtColor(imread(io.BytesIO(base64.b64decode(base64_img))), cv2.COLOR_RGB2BGR)