Upload anime_object_detection/detection/person.py with huggingface_hub
Browse files
anime_object_detection/detection/person.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os.path
|
| 2 |
+
import re
|
| 3 |
+
from typing import List, Tuple
|
| 4 |
+
|
| 5 |
+
from hfutils.operate import get_hf_fs
|
| 6 |
+
from hfutils.utils import hf_fs_path, parse_hf_fs_path
|
| 7 |
+
from imgutils.data import ImageTyping
|
| 8 |
+
from imgutils.detect import detect_person
|
| 9 |
+
|
| 10 |
+
from .base import ObjectDetection
|
| 11 |
+
|
| 12 |
+
_VERSIONS = {
|
| 13 |
+
'': 'v0',
|
| 14 |
+
'plus_': 'v1',
|
| 15 |
+
'plus_v1.1_': 'v1.1',
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _parse_model_name(model_name: str):
|
| 20 |
+
matching = re.fullmatch(r'^person_detect_(?P<content>[\s\S]+?)best_(?P<level>[\s\S]+?)$', model_name)
|
| 21 |
+
return _VERSIONS[matching.group('content')], matching.group('level')
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class PersonDetection(ObjectDetection):
|
| 25 |
+
def __init__(self):
|
| 26 |
+
self.repo_id = 'deepghs/imgutils-models'
|
| 27 |
+
|
| 28 |
+
def _get_default_model(self) -> str:
|
| 29 |
+
return 'person_detect_plus_v1.1_best_m'
|
| 30 |
+
|
| 31 |
+
def _list_models(self) -> List[str]:
|
| 32 |
+
hf_fs = get_hf_fs()
|
| 33 |
+
return [
|
| 34 |
+
os.path.splitext(os.path.basename(parse_hf_fs_path(path).filename))[0]
|
| 35 |
+
for path in hf_fs.glob(hf_fs_path(
|
| 36 |
+
repo_id=self.repo_id,
|
| 37 |
+
repo_type='model',
|
| 38 |
+
filename='person_detect/*.onnx',
|
| 39 |
+
))
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
def _get_default_iou_and_score(self, model_name: str) -> Tuple[float, float]:
|
| 43 |
+
return 0.5, 0.3
|
| 44 |
+
|
| 45 |
+
def _get_labels(self, model_name: str) -> List[str]:
|
| 46 |
+
return ['person']
|
| 47 |
+
|
| 48 |
+
def detect(self, image: ImageTyping, model_name: str,
|
| 49 |
+
iou_threshold: float = 0.7, score_threshold: float = 0.25) -> \
|
| 50 |
+
List[Tuple[Tuple[float, float, float, float], str, float]]:
|
| 51 |
+
version, level = _parse_model_name(model_name)
|
| 52 |
+
return detect_person(image=image, level=level, version=version,
|
| 53 |
+
iou_threshold=iou_threshold, conf_threshold=score_threshold)
|