diff --git a/src/anomalib/deploy/inferencers/openvino_inferencer.py b/src/anomalib/deploy/inferencers/openvino_inferencer.py index bb57a8d65a..8dea77b92e 100644 --- a/src/anomalib/deploy/inferencers/openvino_inferencer.py +++ b/src/anomalib/deploy/inferencers/openvino_inferencer.py @@ -4,12 +4,12 @@ # SPDX-License-Identifier: Apache-2.0 import logging -from importlib.util import find_spec from pathlib import Path -from typing import TYPE_CHECKING, Any +from typing import Any import cv2 import numpy as np +from lightning_utilities.core.imports import package_available from omegaconf import DictConfig from PIL import Image @@ -21,14 +21,6 @@ logger = logging.getLogger("anomalib") -if find_spec("openvino") is not None: - import openvino as ov - - if TYPE_CHECKING: - from openvino import CompiledModel -else: - logger.warning("OpenVINO is not installed. Please install OpenVINO to use OpenVINOInferencer.") - class OpenVINOInferencer(Inferencer): """OpenVINO implementation for the inference. @@ -102,6 +94,10 @@ def __init__( task: str | None = None, config: dict | None = None, ) -> None: + if not package_available("openvino"): + msg = "OpenVINO is not installed. Please install OpenVINO to use OpenVINOInferencer." + raise ImportError(msg) + self.device = device self.config = config @@ -110,7 +106,7 @@ def __init__( self.task = TaskType(task) if task else TaskType(self.metadata["task"]) - def load_model(self, path: str | Path | tuple[bytes, bytes]) -> tuple[Any, Any, "CompiledModel"]: + def load_model(self, path: str | Path | tuple[bytes, bytes]) -> tuple[Any, Any, Any]: """Load the OpenVINO model. Args: @@ -121,6 +117,8 @@ def load_model(self, path: str | Path | tuple[bytes, bytes]) -> tuple[Any, Any, [tuple[str, str, ExecutableNetwork]]: Input and Output blob names together with the Executable network. """ + import openvino as ov + core = ov.Core() # If tuple of bytes is passed if isinstance(path, tuple):