-
-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1790 from gratusrichard/master
added face detection implementation using yunet
- Loading branch information
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
face_detection_yunet_2023mar.onnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# YuNet Face Detection Model | ||
|
||
YuNet is a highly efficient and accurate face detection model developed by the Intel OpenVINO team. It is designed for real-time face detection and can detect multiple faces in an image, along with their facial landmarks. | ||
|
||
## Key Features | ||
|
||
- **High Accuracy**: YuNet is known for its high detection accuracy and reliability in various environments and conditions. | ||
- **Real-time Performance**: Optimized for real-time performance, making it suitable for applications requiring fast processing. | ||
- **Facial Landmarks**: In addition to detecting faces, YuNet also provides precise facial landmarks, such as eyes, nose, and mouth corners. | ||
|
||
# Face Detection using YuNet | ||
|
||
This repository contains a simple face detection script using the YuNet model for detecting faces in images. The script is written in Python and utilizes OpenCV for image processing and face detection. | ||
|
||
## Prerequisites | ||
|
||
Ensure you have the following installed on your system: | ||
- Python 3.6+ | ||
- OpenCV (including the `opencv-contrib-python` package) | ||
- NumPy | ||
|
||
## Installation | ||
|
||
|
||
1. Install the required Python packages. | ||
```sh | ||
pip install opencv-python opencv-contrib-python numpy | ||
``` | ||
|
||
2. Download the pre-trained YuNet model. | ||
If the model is not available locally, the script will automatically download it. | ||
|
||
## Usage | ||
|
||
Run the face detection script with the following command: | ||
```sh | ||
python face_detection.py -p /path/to/your/image.jpg | ||
``` | ||
|
||
Replace `/path/to/your/image.jpg` with the actual path to the image you want to process. | ||
|
||
## Script Overview | ||
|
||
### face_detection.py | ||
|
||
This is the main script for face detection. It includes the following key functionalities: | ||
|
||
- **Model Loading:** Downloads the YuNet model if not already available. | ||
- **Image Reading:** Reads the input image specified by the user. | ||
- **Face Detection:** Detects faces and facial landmarks in the image using the YuNet model. | ||
- **Visualization:** Draws bounding boxes and landmarks on detected faces and displays the results. | ||
|
||
### Key Functions | ||
|
||
- `visualize_face_detections(image_path, detections)`: Visualizes the face detections on the image. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from utils import get_dataset | ||
import os | ||
import sys | ||
import cv2 | ||
import argparse | ||
import time | ||
import numpy as np | ||
|
||
|
||
|
||
parser = argparse.ArgumentParser('-p','--path','image path for processing') | ||
|
||
|
||
args = parser.parse_args() | ||
|
||
if not os.path.exists('./face_detection_yunet_2023mar.onnx'): | ||
|
||
print("pretrained model not available, downloading from repository") | ||
suc = get_dataset() | ||
|
||
if suc: | ||
pass | ||
else: | ||
print("model could not be downloaded.") | ||
|
||
print(sys.exis()) | ||
|
||
|
||
detector = cv2.FaceDetectorYN.create("/content/face_detection_yunet_2023mar_int8.onnx", "", (320, 320),score_threshold = 0.8) | ||
img = cv2.imread() | ||
img_W = int(img.shape[1]) | ||
img_H = int(img.shape[0]) | ||
detector.setInputSize((img_W, img_H)) | ||
start_time = time.time() | ||
detections = detector.detect(img)[1] | ||
end_time = time.time() | ||
elapsed_time_ms = ( end_time - start_time ) * 1000 | ||
print(f"Processing time: {elapsed_time_ms:.2f} milliseconds") | ||
|
||
|
||
|
||
def visualize_face_detections(image_path, detections): | ||
image = cv2.imread(image_path) | ||
|
||
for detection in detections: | ||
x, y, width, height = map(int, detection[:4]) | ||
right_eye = tuple(map(int, detection[4:6])) | ||
left_eye = tuple(map(int, detection[6:8])) | ||
nose_tip = tuple(map(int, detection[8:10])) | ||
right_mouth_corner = tuple(map(int, detection[10:12])) | ||
left_mouth_corner = tuple(map(int, detection[12:14])) | ||
face_score = detection[14] | ||
|
||
cv2.rectangle(image, (x, y), (x + width, y + height), (0, 255, 0), 2) | ||
|
||
cv2.circle(image, right_eye, 3, (255, 0, 0), -1) | ||
cv2.circle(image, left_eye, 3, (0, 0, 255), -1) | ||
cv2.circle(image, nose_tip, 3, (0, 255, 0), -1) | ||
cv2.circle(image, right_mouth_corner, 3, (255, 0, 255), -1) | ||
cv2.circle(image, left_mouth_corner, 3, (0, 255, 255), -1) | ||
|
||
cv2.putText(image, f"fs: {face_score:.2f}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) | ||
|
||
cv2.imshow(image) | ||
|
||
image_path = args.path | ||
visualize_face_detections(image_path, detections) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import requests | ||
|
||
|
||
|
||
|
||
|
||
def get_dataset(): | ||
pretrained_dataset_url= "https://github.com/opencv/opencv_zoo/raw/main/models/face_detection_yunet/face_detection_yunet_2023mar.onnx?download=" | ||
|
||
file_name = "face_detection_yunet_2023mar.onnx" | ||
|
||
|
||
response = requests.get(url=pretrained_dataset_url) | ||
|
||
if response.status_code == 200: | ||
with open(file_name, 'wb') as file: | ||
file.write(response.content) | ||
print("File downloaded successfully.") | ||
return 1 | ||
else: | ||
print("Failed to download file. Status code:", response.status_code) | ||
return 0 |