Skip to content

Commit

Permalink
Added webcam demo for Face Recognition (#247)
Browse files Browse the repository at this point in the history
* 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 <44834743+ad-daniel@users.noreply.github.com>
  • Loading branch information
Pavlos-Tosidis and ad-daniel authored May 2, 2022
1 parent 5f6778b commit e8bb8d3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
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()

0 comments on commit e8bb8d3

Please # to comment.