Commit 0ba4b531 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add auto censor system

parent 9e05ed38
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2,4 +2,4 @@ include README.md
include MANIFEST.in
include requirements.txt
include requirements-*.txt
recursive-include imgutils *.json *.yml *.yaml
recursive-include imgutils *.json *.yml *.yaml *.png
+2 −2
Original line number Diff line number Diff line
@@ -35,11 +35,11 @@ def load_image(image: ImageTyping, mode=None, force_background: Optional[str] =
    return image


def load_images(images: MultiImagesTyping, mode=None) -> List[Image.Image]:
def load_images(images: MultiImagesTyping, mode=None, force_background: Optional[str] = 'white') -> List[Image.Image]:
    if not isinstance(images, (list, tuple)):
        images = [images]

    return [load_image(item, mode) for item in images]
    return [load_image(item, mode, force_background) for item in images]


def add_background_for_rgba(image: ImageTyping, background: str = 'white'):
+1 −0
Original line number Diff line number Diff line
@@ -9,5 +9,6 @@ Overview:
        :align: center
"""
from .head import detect_heads
from .manbits import detect_manbits
from .person import detect_person
from .visual import detection_visualize
+82 −0
Original line number Diff line number Diff line
"""
Overview:
    Detect human manbits in anime images.

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

    .. image:: manbit_detect.dat.svg
        :align: center

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

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

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

from huggingface_hub import hf_hub_download

from ._yolo import _image_preprocess, _data_postprocess
from ..data import ImageTyping, load_image, rgb_encode
from ..utils import open_onnx_model


@lru_cache()
def _open_manbit_detect_model(level: str = 'm'):
    return open_onnx_model(hf_hub_download(
        'deepghs/imgutils-models',
        f'manbits_detect/manbits_detect_best_{level}.onnx'
    ))


_LABELS = [
    'EXPOSED_BELLY', 'EXPOSED_BREAST_F', 'EXPOSED_BREAST_M',
    'EXPOSED_BUTTOCKS', 'EXPOSED_GENITALIA_F', 'EXPOSED_GENITALIA_M'
]


def detect_manbits(image: ImageTyping, level: str = 'm', max_infer_size=640,
                   conf_threshold: float = 0.25, iou_threshold: float = 0.7) \
        -> List[Tuple[Tuple[int, int, int, int], str, float]]:
    """
    Overview:
        Detect human manbits 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 default value is `s`.
    :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`.
    :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()
    """
    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)
+1 −0
Original line number Diff line number Diff line
from .censor_ import register_censor_method, censor
Loading