From e8bb8d34f921fc1e6aa326bd4ccad8622c408f01 Mon Sep 17 00:00:00 2001 From: Pavlos Tosidis <35866477+Pavlos-Tosidis@users.noreply.github.com> Date: Mon, 2 May 2022 16:26:21 +0300 Subject: [PATCH] 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 <44834743+ad-daniel@users.noreply.github.com> --- .../face_recognition/{REAMDE.md => README.md} | 2 + .../face_recognition/demos/webcam_demo.py | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+) rename projects/perception/face_recognition/{REAMDE.md => README.md} (61%) create mode 100644 projects/perception/face_recognition/demos/webcam_demo.py diff --git a/projects/perception/face_recognition/REAMDE.md b/projects/perception/face_recognition/README.md similarity index 61% rename from projects/perception/face_recognition/REAMDE.md rename to projects/perception/face_recognition/README.md index 10315970f0..7b6832edba 100644 --- a/projects/perception/face_recognition/REAMDE.md +++ b/projects/perception/face_recognition/README.md @@ -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. \ No newline at end of file diff --git a/projects/perception/face_recognition/demos/webcam_demo.py b/projects/perception/face_recognition/demos/webcam_demo.py new file mode 100644 index 0000000000..13f0f73f77 --- /dev/null +++ b/projects/perception/face_recognition/demos/webcam_demo.py @@ -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()