Skip to content

Commit e8bb8d3

Browse files
Added webcam demo for Face Recognition (#247)
* Added webcam demo for Face Recognition * Update webcam_demo.py * Renamed README.md and added webcam_demo instructions. * Renamed README.md and added webcam_demo instructions. * Renamed README.md and added webcam_demo instructions. * Renamed README.md and added webcam_demo instructions. * Renamed README.md and added webcam_demo instructions. * Renamed README.md and added webcam_demo instructions. Co-authored-by: ad-daniel <[email protected]>
1 parent 5f6778b commit e8bb8d3

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Diff for: projects/perception/face_recognition/REAMDE.md renamed to projects/perception/face_recognition/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ More specifically, the following applications are provided:
88
2. demos/eval_demo.py: A tool that demonstrates how to perform evaluation using FaceRecognition
99
3. demos/inference_demo.py: A tool that demonstrates how to perform inference on a single image
1010
4. demos/benchmarking_demo.py: A simple benchmarking tool for measuring the performance of FaceRecognition in various platforms
11+
5. demos/webcam_demo.py: A tool that demonstrates how to perform face detection and recognition with the use of a webcam.
12+
1. To use this tool you have to first create a database containing the faces to be recognised. To do this, you will have to prepare the face images using the [align](https://github.com/opendr-eu/opendr/blob/master/docs/reference/face-recognition.md#facerecognitionlearneralign) method of the tool and place them in a folder named `'cropped_images_path'` inside the `'demos'` directory.
1113

1214
Please use the --device cpu flag for the demos if you are running them on a machine without a CUDA-enabled GPU.
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2020-2022 OpenDR European Project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import cv2
17+
18+
from opendr.perception.object_detection_2d import RetinaFaceLearner
19+
from opendr.perception.object_detection_2d.datasets.transforms import\
20+
BoundingBoxListToNumpyArray
21+
from opendr.perception.face_recognition import FaceRecognitionLearner
22+
23+
facedetector = RetinaFaceLearner(backbone='mnet', device='cuda')
24+
facedetector.download(".", mode="pretrained")
25+
facedetector.load("./retinaface_mnet")
26+
27+
recognizer = FaceRecognitionLearner(device='cuda', backbone='mobilefacenet', mode='backbone_only')
28+
recognizer.download(path=".")
29+
recognizer.load(".")
30+
recognizer.fit_reference('./cropped_images_path', save_path="./save_path", create_new=True)
31+
32+
cam = cv2.VideoCapture(0)
33+
cv2.namedWindow("face recognition")
34+
font = cv2.FONT_HERSHEY_SIMPLEX
35+
fontScale = 1
36+
color = (255, 0, 0)
37+
thickness = 2
38+
while True:
39+
ret, frame = cam.read()
40+
if not ret:
41+
print("Failed to grab frame")
42+
break
43+
bounding_boxes = facedetector.infer(frame)
44+
if bounding_boxes:
45+
bounding_boxes_ = BoundingBoxListToNumpyArray()(bounding_boxes)
46+
boxes = bounding_boxes_[:, :4]
47+
for idx, box in enumerate(boxes):
48+
(startX, startY, endX, endY) = int(box[0]), int(box[1]), int(box[2]), int(box[3])
49+
img = frame[startY:endY, startX:endX]
50+
result = recognizer.infer(img)
51+
if result.description != 'Not found':
52+
color = (0, 255, 0)
53+
else:
54+
color = (0, 0, 255)
55+
img = cv2.rectangle(frame, (startX, startY), (endX, endY), color, thickness)
56+
img = cv2.putText(img, result.description, (startX, endY - 10), font,
57+
fontScale, color, thickness, cv2.LINE_AA)
58+
else:
59+
img = frame
60+
cv2.imshow("face recognition", img)
61+
cv2.waitKey(1)
62+
63+
cam.release()

0 commit comments

Comments
 (0)