narugo1992
dev(narugo): add person detection
ebc32f0
raw
history blame
1.42 kB
from functools import lru_cache
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 plot_detection
from yolo_ import _image_preprocess, _data_simple_postprocess
_PERSON_MODELS = [
'person_detect_best_s.onnx',
]
_DEFAULT_PERSON_MODEL = _PERSON_MODELS[0]
@lru_cache()
def _open_person_detect_model(model_name):
return _open_onnx_model(hf_hub_download(
'deepghs/imgutils-models',
f'person_detect/{model_name}'
))
def detect_person(image: ImageTyping, model_name: str, max_infer_size=1216,
conf_threshold: float = 0.25, iou_threshold: float = 0.7):
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_person_detect_model(model_name).run(['output0'], {'images': data})
return _data_simple_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size)
def _gr_detect_person(image: ImageTyping, model_name: str, max_infer_size=1216,
conf_threshold: float = 0.25, iou_threshold: float = 0.7):
ret = detect_person(image, model_name, max_infer_size, conf_threshold, iou_threshold)
detections = [(box, 0, score) for box, score in ret]
return plot_detection(image, detections, ['person'])