Commit c28ddc30 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): migrate censor

parent 79db604c
Loading
Loading
Loading
Loading
+13 −15
Original line number Diff line number Diff line
import random

from benchmark import BaseBenchmark, create_plot_cli
from imgutils.detect import detect_censors
from imgutils.detect.censor import detect_censors, _REPO_ID
from imgutils.generic.yolo import _open_models_for_repo_id

_MODELS = _open_models_for_repo_id(_REPO_ID).model_names


class CensorDetectBenchmark(BaseBenchmark):
    def __init__(self, level, version):
    def __init__(self, model_name: str):
        BaseBenchmark.__init__(self)
        self.level = level
        self.version = version
        self.model_name = model_name

    def load(self):
        from imgutils.detect.censor import _open_censor_detect_model
        _ = _open_censor_detect_model(level=self.level, version=self.version)
        from imgutils.generic.yolo import _open_models_for_repo_id
        _ = _open_models_for_repo_id(_REPO_ID)._open_model(self.model_name)

    def unload(self):
        from imgutils.detect.censor import _open_censor_detect_model
        _open_censor_detect_model.cache_clear()
        from imgutils.generic.yolo import _open_models_for_repo_id
        _open_models_for_repo_id.cache_clear()

    def run(self):
        image_file = random.choice(self.all_images)
        _ = detect_censors(image_file, level=self.level, version=self.version)
        _ = detect_censors(image_file, model_name=self.model_name)


if __name__ == '__main__':
    create_plot_cli(
        [
            ('censor v1.0 (yolov8s)', CensorDetectBenchmark('s', 'v1.0')),
            ('censor v1.0 (yolov8n)', CensorDetectBenchmark('n', 'v1.0')),
            ('censor v0.10 (yolov8s)', CensorDetectBenchmark('s', 'v0.10')),
            ('censor v0.9 (yolov8s)', CensorDetectBenchmark('s', 'v0.9')),
            # ('censor v0.8 (yolov8s)', CensorDetectBenchmark('s', 'v0.8')),
            # ('censor v0.7 (yolov8s)', CensorDetectBenchmark('s', 'v0.7')),
            (model_name, CensorDetectBenchmark(model_name))
            for model_name in _MODELS
        ],
        title='Benchmark for Anime Censor Detections',
        run_times=10,
+0 −2538

File deleted.

Preview size limit exceeded, changes collapsed.

+48 −39
Original line number Diff line number Diff line
@@ -15,47 +15,55 @@ Overview:
        :align: center

"""
from functools import lru_cache
from typing import List, Tuple
from typing import List, Tuple, Optional

from huggingface_hub import hf_hub_download
from ..data import ImageTyping
from ..generic import yolo_predict

from ._yolo import _image_preprocess, _data_postprocess
from ..data import ImageTyping, load_image, rgb_encode
from ..utils import open_onnx_model
_REPO_ID = 'deepghs/anime_censor_detection'


@lru_cache()
def _open_censor_detect_model(level: str = 's', version: str = 'v1.0'):
    return open_onnx_model(hf_hub_download(
        f'deepghs/anime_censor_detection',
        f'censor_detect_{version}_{level}/model.onnx'
    ))


_LABELS = ["nipple_f", "penis", "pussy"]


def detect_censors(image: ImageTyping, level: str = 's', version: str = 'v1.0', max_infer_size=640,
def detect_censors(image: ImageTyping, level: str = 's', version: str = 'v1.0', model_name: Optional[str] = None,
                   conf_threshold: float = 0.3, iou_threshold: float = 0.7) \
        -> List[Tuple[Tuple[int, int, int, int], str, float]]:
    """
    Overview:
    Detect human censor points in anime images.

    :param image: Image to detect.
    :param level: The model level being used can be either `s` or `n`.
        The `n` model runs faster with smaller system overhead, while the `s` model achieves higher accuracy.
        The default value is `s`.
    :param version: Version of model, default is ``v1.0``.
    :param max_infer_size: The maximum image size used for model inference, if the image size exceeds this limit,
        the image will be resized and used for inference. The default value is `640` pixels.
    :param conf_threshold: The confidence threshold, only detection results with confidence scores above
        this threshold will be returned. The default value is `0.3`.
    :param iou_threshold: The detection area coverage overlap threshold, areas with overlaps above this threshold
        will be discarded. The default value is `0.7`.
    :return: The detection results list, each item includes the detected area `(x0, y0, x1, y1)`,
        the target type (one of `nipple_f`, `penis` and `pussy`) and the target confidence score.
    This function uses pre-trained YOLOv8 models to identify and locate specific
    anatomical features that are typically censored in anime images. It can detect
    female nipples, male genitals, and female genitals.

    :param image: The input image to be analyzed. Can be a file path, URL, or image data.
    :type image: ImageTyping

    :param level: The model level to use, either 's' (standard) or 'n' (nano).
                  The 'n' model is faster but less accurate, while 's' is more accurate but slower.
    :type level: str

    :param version: The version of the model to use. Default is 'v1.0'.
    :type version: str

    :param model_name: Optional custom model name. If not provided, it will be constructed
                       from the version and level.
    :type model_name: Optional[str]

    :param conf_threshold: The confidence threshold for detections. Only detections with
                           confidence above this value will be returned. Default is 0.3.
    :type conf_threshold: float

    :param iou_threshold: The Intersection over Union (IoU) threshold for non-maximum
                          suppression. Detections with IoU above this value will be merged.
                          Default is 0.7.
    :type iou_threshold: float

    :return: A list of tuples, each containing:
             - A tuple of four integers (x0, y0, x1, y1) representing the bounding box
             - A string indicating the type of detection ('nipple_f', 'penis', or 'pussy')
             - A float representing the confidence score of the detection
    :rtype: List[Tuple[Tuple[int, int, int, int], str, float]]

    :raises ValueError: If an invalid level is provided.
    :raises RuntimeError: If the model fails to load or process the image.

    Examples::
        >>> from imgutils.detect import detect_censors, detection_visualize
@@ -74,9 +82,10 @@ def detect_censors(image: ImageTyping, level: str = 's', version: str = 'v1.0',
        >>> plt.imshow(detection_visualize(image, result))
        >>> plt.show()
    """
    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(level, version).run(['output0'], {'images': data})
    return _data_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size, _LABELS)
    return yolo_predict(
        image=image,
        repo_id=_REPO_ID,
        model_name=model_name or f'censor_detect_{version}_{level}',
        conf_threshold=conf_threshold,
        iou_threshold=iou_threshold,
    )
+3 −2
Original line number Diff line number Diff line
import pytest

from imgutils.detect.censor import _open_censor_detect_model, detect_censors
from imgutils.detect.censor import detect_censors
from imgutils.generic.yolo import _open_models_for_repo_id
from test.testings import get_testfile


@@ -9,7 +10,7 @@ def _release_model_after_run():
    try:
        yield
    finally:
        _open_censor_detect_model.cache_clear()
        _open_models_for_repo_id.cache_clear()


@pytest.mark.unittest