-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcompreface_webcam_recognition_demo.py
128 lines (104 loc) · 5.13 KB
/
compreface_webcam_recognition_demo.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
126
127
128
"""
Copyright(c) 2021 the original author or authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https: // www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing
permissions and limitations under the License.
"""
import cv2
import argparse
import time
from threading import Thread
from compreface import CompreFace
from compreface.service import RecognitionService
def parseArguments():
parser = argparse.ArgumentParser()
parser.add_argument("--api-key", help="CompreFace recognition service API key", type=str, default='00000000-0000-0000-0000-000000000002')
parser.add_argument("--host", help="CompreFace host", type=str, default='http://localhost')
parser.add_argument("--port", help="CompreFace port", type=str, default='8000')
args = parser.parse_args()
return args
class ThreadedCamera:
def __init__(self, api_key, host, port):
self.active = True
self.results = []
self.capture = cv2.VideoCapture(0)
self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 2)
compre_face: CompreFace = CompreFace(host, port, {
"limit": 0,
"det_prob_threshold": 0.8,
"prediction_count": 1,
"face_plugins": "age,gender",
"status": False
})
self.recognition: RecognitionService = compre_face.init_face_recognition(api_key)
self.FPS = 1/30
# Start frame retrieval thread
self.thread = Thread(target=self.show_frame, args=())
self.thread.daemon = True
self.thread.start()
def show_frame(self):
print("Started")
while self.capture.isOpened():
(status, frame_raw) = self.capture.read()
self.frame = cv2.flip(frame_raw, 1)
if self.results:
results = self.results
for result in results:
box = result.get('box')
age = result.get('age')
gender = result.get('gender')
mask = result.get('mask')
subjects = result.get('subjects')
if box:
cv2.rectangle(img=self.frame, pt1=(box['x_min'], box['y_min']),
pt2=(box['x_max'], box['y_max']), color=(0, 255, 0), thickness=1)
if age:
age = f"Age: {age['low']} - {age['high']}"
cv2.putText(self.frame, age, (box['x_max'], box['y_min'] + 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1)
if gender:
gender = f"Gender: {gender['value']}"
cv2.putText(self.frame, gender, (box['x_max'], box['y_min'] + 35),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1)
if mask:
mask = f"Mask: {mask['value']}"
cv2.putText(self.frame, mask, (box['x_max'], box['y_min'] + 55),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1)
if subjects:
subjects = sorted(subjects, key=lambda k: k['similarity'], reverse=True)
subject = f"Subject: {subjects[0]['subject']}"
similarity = f"Similarity: {subjects[0]['similarity']}"
cv2.putText(self.frame, subject, (box['x_max'], box['y_min'] + 75),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1)
cv2.putText(self.frame, similarity, (box['x_max'], box['y_min'] + 95),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1)
else:
subject = f"No known faces"
cv2.putText(self.frame, subject, (box['x_max'], box['y_min'] + 75),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1)
cv2.imshow('CompreFace demo', self.frame)
time.sleep(self.FPS)
if cv2.waitKey(1) & 0xFF == 27:
self.capture.release()
cv2.destroyAllWindows()
self.active=False
def is_active(self):
return self.active
def update(self):
if not hasattr(self, 'frame'):
return
_, im_buf_arr = cv2.imencode(".jpg", self.frame)
byte_im = im_buf_arr.tobytes()
data = self.recognition.recognize(byte_im)
self.results = data.get('result')
if __name__ == '__main__':
args = parseArguments()
threaded_camera = ThreadedCamera(args.api_key, args.host, args.port)
while threaded_camera.is_active():
threaded_camera.update()