FaceDetector-tiny / README.md
vladlinv's picture
Upload folder using huggingface_hub
ef30681 verified
|
Raw History Blame Contribute Delete
6 kB
---
license: apache-2.0
pipeline_tag: object-detection
tags:
- face-detection
- edge
- yunet
- onnx
- tflite
- opencv
- libfacedetection
---
<div align="center">
<img src="https://raw.githubusercontent.com/metrika-labs/FaceDetector/master/media/banner.webp" width="100%" alt="Face Detector">
<br>
<a href="https://huggingface.co/collections/metrika-labs/face-detector"><img src="https://img.shields.io/badge/🤗_Hugging_Face-Face_Detector-FFD21E" alt="Hugging Face"></a>
<a href="https://github.com/metrika-labs/FaceDetector"><img src="https://img.shields.io/badge/GitHub-FaceDetector-181717?logo=github" alt="GitHub"></a>
</div>
# Fast and accurate Open-Source face detection models
<p><img src="https://raw.githubusercontent.com/metrika-labs/FaceDetector/master/media/bechmark.png" width="100%" alt="benchmark"></p>
<table width="100%">
<thead>
<tr>
<th align="left">Model</th>
<th align="center">Architecture</th>
<th align="left">Target Use Case</th>
<th align="center">mAP50&#8209;95</th>
<th align="center">AP50</th>
<th align="center">Latency</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>FaceDetector&nbsp;Large</strong>&nbsp;<sup><code>closed</code></sup></td>
<td align="center"><code>SCRFD-10G</code></td>
<td>Maximum precision, rare &amp; hard cases (extreme angles, occlusions, distant/tiny faces)</td>
<td align="center">78.2%</td>
<td align="center">92.7%</td>
<td align="center">3.64&nbsp;ms</td>
</tr>
<tr>
<td><strong><a href="https://huggingface.co/metrika-labs/FaceDetector-medium">FaceDetector&nbsp;Medium</a></strong></td>
<td align="center"><code>SCRFD-1G</code></td>
<td>High-precision box localization, recognition cropping &amp; everyday complex scenes</td>
<td align="center">72.6%</td>
<td align="center">91.5%</td>
<td align="center">2.45&nbsp;ms</td>
</tr>
<tr>
<td><strong><a href="https://huggingface.co/metrika-labs/FaceDetector-small">FaceDetector&nbsp;Small</a></strong></td>
<td align="center"><code>YuNet-N</code></td>
<td>Real-time video streams, mobile apps &amp; multi-stream tracking</td>
<td align="center">62.2%</td>
<td align="center">90.1%</td>
<td align="center">1.35&nbsp;ms</td>
</tr>
<tr>
<td><strong><a href="https://huggingface.co/metrika-labs/FaceDetector-tiny">FaceDetector&nbsp;Tiny</a></strong></td>
<td align="center"><code>YuNet-S</code></td>
<td>CPU-only inference, low-power edge devices &amp; close-range tasks</td>
<td align="center">56.4%</td>
<td align="center">87.6%</td>
<td align="center">1.11&nbsp;ms</td>
</tr>
</tbody>
</table>
## Usage
### ONNX (OpenCV)
```python
pip install opencv-python huggingface_hub
```
```python
import cv2
from huggingface_hub import snapshot_download
model_dir = snapshot_download("metrika-labs/FaceDetector-tiny")
img = cv2.imread("image.jpg")
h, w = img.shape[:2]
detector = cv2.FaceDetectorYN.create(
f"{model_dir}/onnx/model.onnx", "", (w, h),
score_threshold=0.5, nms_threshold=0.45,
)
_, faces = detector.detect(img)
```
### TFLite
```python
pip install ai-edge-litert opencv-python huggingface_hub
```
```python
from pathlib import Path
import cv2
import numpy as np
from ai_edge_litert.interpreter import Interpreter
from huggingface_hub import snapshot_download
model_dir = snapshot_download("metrika-labs/FaceDetector-tiny")
runner = Interpreter(
model_content=Path(f"{model_dir}/tflite/model_int8.tflite").read_bytes()
).get_signature_runner("serving_default")
img = cv2.resize(cv2.imread("image.jpg"), (640, 640))
outs = runner(input=img[None].astype(np.float32))
cls = np.concatenate([outs[f"output_{i}"] for i in (0, 1, 2)], axis=1)[0, :, 0]
obj = np.concatenate([outs[f"output_{i}"] for i in (3, 4, 5)], axis=1)[0, :, 0]
reg = np.concatenate([outs[f"output_{i}"] for i in (6, 7, 8)], axis=1)[0]
priors = np.concatenate([
np.stack([*np.meshgrid(np.arange(640 // s) * s, np.arange(640 // s) * s, indexing="ij")[::-1],
np.full((640 // s, 640 // s), s), np.full((640 // s, 640 // s), s)], axis=-1).reshape(-1, 4)
for s in (8, 16, 32)
], axis=0).astype(np.float32)
scores = np.sqrt(np.clip(cls * obj, 0.0, 1.0))
keep = scores >= 0.5
s, r, p = scores[keep], reg[keep], priors[keep]
cx, cy = r[:, 0] * p[:, 2] + p[:, 0], r[:, 1] * p[:, 3] + p[:, 1]
bw, bh = np.exp(r[:, 2]) * p[:, 2], np.exp(r[:, 3]) * p[:, 3]
boxes = np.stack([cx - bw * 0.5, cy - bh * 0.5, bw, bh], axis=-1)
idxs = cv2.dnn.NMSBoxes(boxes.tolist(), s.tolist(), 0.5, 0.45)
```
### C++ (`libfacedetection`)
Clone [`ShiqiYu/libfacedetection`](https://github.com/ShiqiYu/libfacedetection), replace `src/facedetectcnn-data.cpp` and `src/facedetectcnn-model.cpp` with the files from `cpp/`, and build with CMake:
```cpp
#include <cstdlib>
#include <opencv2/opencv.hpp>
#include "facedetectcnn.h"
int main() {
cv::Mat image = cv::imread("image.jpg");
unsigned char * pBuffer = (unsigned char *)std::malloc(FACEDETECTION_RESULT_BUFFER_SIZE);
int * pResults = facedetect_cnn(pBuffer, (unsigned char *)(image.ptr(0)), image.cols, image.rows, (int)image.step);
for (int i = 0; i < (pResults ? *pResults : 0); i++) {
short * p = ((short *)(pResults + 1)) + FACEDETECTION_RESULT_STRIDE_SHORTS * i;
int confidence = p[0], x = p[1], y = p[2], w = p[3], h = p[4];
}
std::free(pBuffer);
}
```
## Details
The face detection model family includes three models based on the SCRFD and YuNet architectures, trained by Metrika Labs on 160K images from the Google Open Images dataset with our own carefully prepared annotations. The models combine high speed and accuracy, making them suitable for real-time applications, including mobile and edge devices. Available in ONNX (OpenCV), TFLite, and libfacedetection (C++) formats.
Models are released under the Apache 2.0 license.
contact@metrika.cc