Skip to content

Commit

Permalink
Merge pull request #1790 from gratusrichard/master
Browse files Browse the repository at this point in the history
added face detection implementation using yunet
  • Loading branch information
akshitagupta15june committed Jun 1, 2024
2 parents c5251ff + 695496c commit bd9edc6
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions Face-Detection/Face Detection using Yunet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
face_detection_yunet_2023mar.onnx
56 changes: 56 additions & 0 deletions Face-Detection/Face Detection using Yunet/README.md
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.

67 changes: 67 additions & 0 deletions Face-Detection/Face Detection using Yunet/app.py
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)
22 changes: 22 additions & 0 deletions Face-Detection/Face Detection using Yunet/utils.py
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

0 comments on commit bd9edc6

Please sign in to comment.