Commit aaba76d3 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add benchmark for hand detection

parent 64a5d4bf
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
import random

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


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

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

    def unload(self):
        from imgutils.detect.hand import _open_hand_detect_model
        _open_hand_detect_model.cache_clear()

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


if __name__ == '__main__':
    create_plot_cli(
        [
            ('hand v0.8 (yolov8s)', HandDetectBenchmark('v0.8', 's')),
            ('hand v1.0 (yolov8s)', HandDetectBenchmark('v1.0', 's')),
            ('hand v1.0 (yolov8n)', HandDetectBenchmark('v1.0', 'n')),
        ],
        title='Benchmark for Anime Hand Detections',
        run_times=10,
        try_times=20,
    )()
+16 −0
Original line number Diff line number Diff line
from imgutils.detect import detect_hands
from imgutils.detect.visual import detection_visualize
from plot import image_plot


def _detect(img, **kwargs):
    return detection_visualize(img, detect_hands(img, **kwargs))


if __name__ == '__main__':
    image_plot(
        (_detect('two_bikini_girls.png'), 'closed heads'),
        (_detect('mostima_post.jpg'), 'anime style'),
        columns=2,
        figsize=(12, 9),
    )
+1 −0
Original line number Diff line number Diff line
@@ -13,3 +13,4 @@ from .face import detect_faces
from .head import detect_heads
from .person import detect_person
from .visual import detection_visualize
from .hand import detect_hands
+63 −0
Original line number Diff line number Diff line
"""
Overview:
    Detect human hands in anime images.

    Trained on dataset `deepghs/anime_hand_detection <https://huggingface.co/datasets/deepghs/anime_hand_detection>`_ with YOLOv8.

    .. image:: hand_detect_demo.plot.py.svg
        :align: center

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

    .. image:: hand_detect_benchmark.plot.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_hand_detect_model(level: str = 's', version: str = 'v1.0'):
    return open_onnx_model(hf_hub_download(
        f'deepghs/anime_hand_detection',
        f'hand_detect_{version}_{level}/model.onnx'
    ))


_LABELS = ["hand"]


def detect_hands(image: ImageTyping, level: str = 's', version: str = 'v1.0', max_infer_size=640,
                 conf_threshold: float = 0.35, iou_threshold: float = 0.7) \
        -> List[Tuple[Tuple[int, int, int, int], str, float]]:
    """
    Overview:
        Detect human hand 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 (always `hand`) 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_hand_detect_model(level).run(['output0'], {'images': data})
    return _data_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size, _LABELS)
+19 −0
Original line number Diff line number Diff line
import pytest

from imgutils.detect.hand import _open_hand_detect_model, detect_hands
from test.testings import get_testfile


@pytest.fixture(scope='module', autouse=True)
def _release_model_after_run():
    try:
        yield
    finally:
        _open_hand_detect_model.cache_clear()


@pytest.mark.unittest
class TestDetectHead:
    def test_detect_hands(self):
        detections = detect_hands(get_testfile('genshin_post.jpg'))
        assert len(detections) >= 4