Unverified Commit 525c042e authored by narugo1992's avatar narugo1992 Committed by GitHub
Browse files

Merge pull request #121 from deepghs/dev/cache

dev(narugo): add thread-safe cache
parents c1bca124 7a8c4345
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
imgutils.utils.cache
====================================

.. currentmodule:: imgutils.utils.cache

.. automodule:: imgutils.utils.cache



ts_lru_cache
------------------------------

.. autofunction:: ts_lru_cache


+1 −0
Original line number Diff line number Diff line
@@ -9,5 +9,6 @@ imgutils.utils
.. toctree::
    :maxdepth: 3

    cache
    func
    onnxruntime
+3 −4
Original line number Diff line number Diff line
@@ -74,7 +74,6 @@ Overview:
        This module requires onnxruntime version 1.18 or higher.
"""

from functools import lru_cache
from typing import Tuple, List

import numpy as np
@@ -83,7 +82,7 @@ from hbutils.testing.requires.version import VersionInfo
from huggingface_hub import hf_hub_download

from imgutils.data import ImageTyping
from imgutils.utils import open_onnx_model
from imgutils.utils import open_onnx_model, ts_lru_cache
from ..data import load_image


@@ -104,7 +103,7 @@ def _check_compatibility() -> bool:
_REPO_ID = 'deepghs/nudenet_onnx'


@lru_cache()
@ts_lru_cache()
def _open_nudenet_yolo():
    """
    Open and cache the NudeNet YOLO ONNX model.
@@ -118,7 +117,7 @@ def _open_nudenet_yolo():
    ))


@lru_cache()
@ts_lru_cache()
def _open_nudenet_nms():
    """
    Open and cache the NudeNet NMS ONNX model.
+2 −3
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ Overview:
            :align: center

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

import cv2
@@ -33,12 +32,12 @@ from huggingface_hub import hf_hub_download

from ..config.meta import __VERSION__
from ..data import ImageTyping, load_image
from ..utils import open_onnx_model
from ..utils import open_onnx_model, ts_lru_cache

_DEFAULT_MODEL = 'dbnetpp_resnet50_fpnc_1200e_icdar2015'


@lru_cache()
@ts_lru_cache()
def _open_text_detect_model(model: str):
    """
    Get an ONNX session for the specified DBNET or DBNET++ model.
+3 −3
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ Overview:
    Having the **best effect**, closest to the drawing lines,
    but consuming a large amount of memory and computing power at runtime.
"""
from functools import lru_cache, partial
from functools import partial
from typing import Optional

import numpy as np
@@ -14,7 +14,7 @@ from huggingface_hub import hf_hub_download

from ._base import resize_image, cv2_resize, _get_image_edge
from ..data import ImageTyping, load_image
from ..utils import open_onnx_model
from ..utils import open_onnx_model, ts_lru_cache


def _preprocess(input_image: Image.Image, detect_resolution: int = 512):
@@ -23,7 +23,7 @@ def _preprocess(input_image: Image.Image, detect_resolution: int = 512):
    return (input_image / 255.0).transpose(2, 0, 1)[None, ...].astype(np.float32)


@lru_cache()
@ts_lru_cache()
def _open_la_model(coarse: bool):
    return open_onnx_model(hf_hub_download(
        'deepghs/imgutils-models',
Loading