Spaces:
Running
Running
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 | |
_CENSOR_MODELS = [ | |
'censor_detect_v0.9_s', | |
'censor_detect_v0.8_s', | |
'censor_detect_v0.7_s', | |
] | |
_DEFAULT_CENSOR_MODEL = _CENSOR_MODELS[0] | |
def _open_censor_detect_model(model_name): | |
return _open_onnx_model(hf_hub_download( | |
f'deepghs/anime_censor_detection', | |
f'{model_name}/model.onnx' | |
)) | |
_LABELS = ['nipple_f', 'penis', 'pussy'] | |
def detect_censors(image: ImageTyping, model_name: str, max_infer_size=640, | |
conf_threshold: float = 0.25, iou_threshold: float = 0.5) \ | |
-> 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_censor_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_censors(image: ImageTyping, model_name: str, max_infer_size=640, | |
conf_threshold: float = 0.25, iou_threshold: float = 0.5): | |
ret = detect_censors(image, model_name, max_infer_size, conf_threshold, iou_threshold) | |
return detection_visualize(image, ret, _LABELS) | |