FaceDetector-tiny / README.md
vladlinv's picture
Upload folder using huggingface_hub
ef30681 verified
|
Raw History Blame Contribute Delete
6 kB
metadata
license: apache-2.0
pipeline_tag: object-detection
tags:
  - face-detection
  - edge
  - yunet
  - onnx
  - tflite
  - opencv
  - libfacedetection
Face Detector
Hugging Face GitHub

Fast and accurate Open-Source face detection models

benchmark

Model Architecture Target Use Case mAP50‑95 AP50 Latency
FaceDetector Large closed SCRFD-10G Maximum precision, rare & hard cases (extreme angles, occlusions, distant/tiny faces) 78.2% 92.7% 3.64 ms
FaceDetector Medium SCRFD-1G High-precision box localization, recognition cropping & everyday complex scenes 72.6% 91.5% 2.45 ms
FaceDetector Small YuNet-N Real-time video streams, mobile apps & multi-stream tracking 62.2% 90.1% 1.35 ms
FaceDetector Tiny YuNet-S CPU-only inference, low-power edge devices & close-range tasks 56.4% 87.6% 1.11 ms

Usage

ONNX (OpenCV)

pip install opencv-python huggingface_hub
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

pip install ai-edge-litert opencv-python huggingface_hub
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, replace src/facedetectcnn-data.cpp and src/facedetectcnn-model.cpp with the files from cpp/, and build with CMake:

#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