Skip to content

Added webcam demo for Face Recognition #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ More specifically, the following applications are provided:
2. demos/eval_demo.py: A tool that demonstrates how to perform evaluation using FaceRecognition
3. demos/inference_demo.py: A tool that demonstrates how to perform inference on a single image
4. demos/benchmarking_demo.py: A simple benchmarking tool for measuring the performance of FaceRecognition in various platforms
5. demos/webcam_demo.py: A tool that demonstrates how to perform face detection and recognition with the use of a webcam.
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.

Please use the --device cpu flag for the demos if you are running them on a machine without a CUDA-enabled GPU.
63 changes: 63 additions & 0 deletions projects/perception/face_recognition/demos/webcam_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2020-2022 OpenDR European Project
#
# 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
#
# http://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

from opendr.perception.object_detection_2d import RetinaFaceLearner
from opendr.perception.object_detection_2d.datasets.transforms import\
BoundingBoxListToNumpyArray
from opendr.perception.face_recognition import FaceRecognitionLearner

facedetector = RetinaFaceLearner(backbone='mnet', device='cuda')
facedetector.download(".", mode="pretrained")
facedetector.load("./retinaface_mnet")

recognizer = FaceRecognitionLearner(device='cuda', backbone='mobilefacenet', mode='backbone_only')
recognizer.download(path=".")
recognizer.load(".")
recognizer.fit_reference('./cropped_images_path', save_path="./save_path", create_new=True)

cam = cv2.VideoCapture(0)
cv2.namedWindow("face recognition")
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
color = (255, 0, 0)
thickness = 2
while True:
ret, frame = cam.read()
if not ret:
print("Failed to grab frame")
break
bounding_boxes = facedetector.infer(frame)
if bounding_boxes:
bounding_boxes_ = BoundingBoxListToNumpyArray()(bounding_boxes)
boxes = bounding_boxes_[:, :4]
for idx, box in enumerate(boxes):
(startX, startY, endX, endY) = int(box[0]), int(box[1]), int(box[2]), int(box[3])
img = frame[startY:endY, startX:endX]
result = recognizer.infer(img)
if result.description != 'Not found':
color = (0, 255, 0)
else:
color = (0, 0, 255)
img = cv2.rectangle(frame, (startX, startY), (endX, endY), color, thickness)
img = cv2.putText(img, result.description, (startX, endY - 10), font,
fontScale, color, thickness, cv2.LINE_AA)
else:
img = frame
cv2.imshow("face recognition", img)
cv2.waitKey(1)

cam.release()