Commit 17bb1dcd authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): use censor detection

parent 72cda264
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
imgutils.detect.censor
==========================

.. currentmodule:: imgutils.detect.censor

.. automodule:: imgutils.detect.censor


detect_censors
------------------------------

.. autofunction:: detect_censors

+39 −0
Original line number Diff line number Diff line
import random

from benchmark import BaseBenchmark, create_plot_cli
from imgutils.detect import detect_censors


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

    def load(self):
        from imgutils.detect.censor import _open_censor_detect_model
        _ = _open_censor_detect_model(level=self.level, version=self.version)

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

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


if __name__ == '__main__':
    create_plot_cli(
        [
            ('censor v1 (yolov8s)', CensorDetectBenchmark('s', 'v1')),
            ('censor v1 (yolov8n)', CensorDetectBenchmark('n', 'v1')),
            ('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')),
        ],
        title='Benchmark for Anime Censor Detections',
        run_times=10,
        try_times=20,
    )()
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ imgutils.detect
.. toctree::
    :maxdepth: 3

    censor
    face
    head
    person
+1 −1
Original line number Diff line number Diff line
@@ -8,8 +8,8 @@ Overview:
    .. image:: head_detect_demo.plot.py.svg
        :align: center
"""
from .censor import detect_censors
from .face import detect_faces
from .head import detect_heads
from .manbits import detect_manbits
from .person import detect_person
from .visual import detection_visualize
+25 −36
Original line number Diff line number Diff line
"""
Overview:
    Detect human manbits in anime images.
    Detect human censor points (including female's nipples and genitals) in anime images.

    Trained on dataset `ani_face_detection <https://universe.roboflow.com/linog/ani_face_detection>`_ with YOLOv8.

    .. image:: manbit_detect.dat.svg
    .. image:: censor_detect_demo.plot.py.svg
        :align: center

    This is an overall benchmark of all the manbit detect models:
    This is an overall benchmark of all the censor detect models:

    .. image:: manbit_detect.benchmark.py.svg
    .. image:: censor_detect_benchmark.plot.py.svg
        :align: center

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

@@ -24,59 +25,47 @@ from ..utils import open_onnx_model


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


_LABELS = [
    'EXPOSED_BELLY', 'EXPOSED_BREAST_F', 'EXPOSED_BREAST_M',
    'EXPOSED_BUTTOCKS', 'EXPOSED_GENITALIA_F', 'EXPOSED_GENITALIA_M'
]
@lru_cache()
def _open_censor_detect_labels(level: str = 's', version: str = 'v1.0'):
    with open(hf_hub_download(
            f'deepghs/anime_censor_detection',
            f'censor_detect_{version}_{level}/model_artifacts.json'
    ), 'r') as f:
        return json.load(f)['names']


def detect_manbits(image: ImageTyping, level: str = 'm', max_infer_size=640,
                   conf_threshold: float = 0.25, iou_threshold: float = 0.7) \
def detect_censors(image: ImageTyping, level: str = 's', version: str = 'v1.0', max_infer_size=640,
                   conf_threshold: float = 0.3, iou_threshold: float = 0.7) \
        -> List[Tuple[Tuple[int, int, int, int], str, float]]:
    """
    Overview:
        Detect human manbits in anime images.
        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 overmanbit, while the `s` model achieves higher accuracy.
        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.25`.
        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 (always `manbit`) and the target confidence score.

    Examples::
        >>> from imgutils.detect import detect_manbits, detection_visualize
        >>>
        >>> image = 'mostima_post.jpg'
        >>> result = detect_manbits(image)  # detect it
        >>> result
        [
            ((29, 441, 204, 584), 'manbit', 0.7874319553375244),
            ((346, 59, 529, 275), 'manbit', 0.7510495185852051),
            ((606, 51, 895, 336), 'manbit', 0.6986488103866577)
        ]
        >>>
        >>> # visualize it
        >>> from matplotlib import pyplot as plt
        >>> plt.imshow(detection_visualize(image, result))
        >>> plt.show()
        the target type (always `censor`) and the target confidence score.
    """
    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_manbit_detect_model(level).run(['output0'], {'images': data})
    return _data_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size, _LABELS)
    output, = _open_censor_detect_model(level).run(['output0'], {'images': data})
    labels = _open_censor_detect_labels(level, version)
    return _data_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size, labels)
Loading