Commit 79db604c authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): migrate eye

parent 9dcce06d
Loading
Loading
Loading
Loading
+13 −13
Original line number Diff line number Diff line
import random

from benchmark import BaseBenchmark, create_plot_cli
from imgutils.detect import detect_eyes
from imgutils.detect.eye import detect_eyes, _REPO_ID
from imgutils.generic.yolo import _open_models_for_repo_id

_MODELS = _open_models_for_repo_id(_REPO_ID).model_names


class EyeDetectBenchmark(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.eye import _open_eye_detect_model
        _ = _open_eye_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.eye import _open_eye_detect_model
        _open_eye_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_eyes(image_file, level=self.level, version=self.version)
        _ = detect_eyes(image_file, model_name=self.model_name)


if __name__ == '__main__':
    create_plot_cli(
        [
            ('eye v1.0 (yolov8s)', EyeDetectBenchmark('s', 'v1.0')),
            ('eye v1.0 (yolov8n)', EyeDetectBenchmark('n', 'v1.0')),
            ('eye v0.8 (yolov8s)', EyeDetectBenchmark('s', 'v0.8')),
            ('eye v0.7 (yolov8s)', EyeDetectBenchmark('s', 'v0.7')),
            (model_name, EyeDetectBenchmark(model_name))
            for model_name in _MODELS
        ],
        title='Benchmark for Anime Eyes Detections',
        run_times=10,
+0 −2546

File deleted.

Preview size limit exceeded, changes collapsed.

+45 −39
Original line number Diff line number Diff line
@@ -13,47 +13,52 @@ 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_eye_detection'


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


_LABELS = ["eye"]


def detect_eyes(image: ImageTyping, level: str = 's', version: str = 'v1.0', max_infer_size=640,
def detect_eyes(image: ImageTyping, level: str = 's', version: str = 'v1.0', model_name: Optional[str] = None,
                conf_threshold: float = 0.3, iou_threshold: float = 0.3) \
        -> List[Tuple[Tuple[int, int, int, int], str, float]]:
    """
    Overview:
    Detect human eyes 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.3`.
    :return: The detection results list, each item includes the detected area `(x0, y0, x1, y1)`,
        the target type (always `eye`) and the target confidence score.
    This function uses a YOLOv8 model to detect eyes in the given anime image. It supports
    different model levels and versions, allowing for a trade-off between speed and accuracy.

    :param image: The input image for eye detection. Can be various image types supported by ImageTyping.
    :type image: ImageTyping

    :param level: The model level to use. Can be either 's' (for higher accuracy) or 'n' (for faster processing).
                  Default is 's'.
    :type level: str

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

    :param model_name: Optional custom model name. If not provided, it's constructed using version and level.
    :type model_name: Optional[str]

    :param conf_threshold: Confidence threshold for detections. Only detections with confidence above this
                           threshold are returned. Default is 0.3.
    :type conf_threshold: float

    :param iou_threshold: Intersection over Union (IoU) threshold for non-maximum suppression.
                          Detections with IoU above this threshold are considered overlapping and merged.
                          Default is 0.3.
    :type iou_threshold: float

    :return: A list of detected eyes. Each detection is represented by a tuple containing:
             - Bounding box coordinates as (x0, y0, x1, y1)
             - Detection class (always 'eye' for this function)
             - Confidence score of the detection
    :rtype: List[Tuple[Tuple[int, int, int, int], str, float]]

    :raises: May raise exceptions related to image loading or model prediction (from yolo_predict function).

    Examples::
        >>> from imgutils.detect import detect_eyes, detection_visualize
@@ -68,9 +73,10 @@ def detect_eyes(image: ImageTyping, level: str = 's', version: str = 'v1.0', max
        >>> 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_eye_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'eye_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.eye import _open_eye_detect_model, detect_eyes
from imgutils.detect.eye import detect_eyes
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_eye_detect_model.cache_clear()
        _open_models_for_repo_id.cache_clear()


@pytest.mark.unittest