File size: 1,706 Bytes
90b4364
3903f4f
90b4364
 
 
 
 
3903f4f
 
90b4364
 
e400f05
9d84e8d
459f3e7
 
 
 
 
 
 
 
 
90b4364
 
 
 
 
 
 
17d3ab1
89047b5
90b4364
 
 
522af51
3903f4f
 
4818b14
89047b5
3903f4f
90b4364
 
 
 
4818b14
3903f4f
90b4364
 
4818b14
89047b5
90b4364
3903f4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from functools import lru_cache
from typing import List, Tuple

from huggingface_hub import hf_hub_download
from imgutils.data import ImageTyping, load_image, rgb_encode

from onnx_ import _open_onnx_model
from plot import detection_visualize
from yolo_ import _image_preprocess, _data_postprocess

_FACE_MODELS = [
    'face_detect_v1.4_s',
    'face_detect_v1.4_n',
    'face_detect_v1.3_s',
    'face_detect_v1.3_n',
    'face_detect_v1.2_s',
    'face_detect_v1.1_s',
    'face_detect_v1.1_n',
    'face_detect_v1_s',
    'face_detect_v1_n',
    'face_detect_v0_s',
    'face_detect_v0_n',
]
_DEFAULT_FACE_MODEL = _FACE_MODELS[0]


@lru_cache()
def _open_face_detect_model(model_name):
    return _open_onnx_model(hf_hub_download(
        f'deepghs/anime_face_detection',
        f'{model_name}/model.onnx',
    ))


_LABELS = ['face']


def detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
                 conf_threshold: float = 0.25, iou_threshold: float = 0.7) \
        -> List[Tuple[Tuple[int, int, int, int], str, float]]:
    image = load_image(image, mode='RGB')
    new_image, old_size, new_size = _image_preprocess(image, max_infer_size)

    data = rgb_encode(new_image)[None, ...]
    output, = _open_face_detect_model(model_name).run(['output0'], {'images': data})
    return _data_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size, _LABELS)


def _gr_detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
                     conf_threshold: float = 0.25, iou_threshold: float = 0.7):
    ret = detect_faces(image, model_name, max_infer_size, conf_threshold, iou_threshold)
    return detection_visualize(image, ret, _LABELS)