Skip to content

Commit 6eeb7f6

Browse files
authored
🐞 Defer OpenVINO import to avoid unnecessary warnings (#2385)
* Fix openvino import issue Signed-off-by: Samet Akcay <samet.akcay@intel.com> * Fix pre-commit issues Signed-off-by: Samet Akcay <samet.akcay@intel.com> --------- Signed-off-by: Samet Akcay <samet.akcay@intel.com>
1 parent 1465b05 commit 6eeb7f6

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/anomalib/deploy/inferencers/openvino_inferencer.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
# SPDX-License-Identifier: Apache-2.0
55

66
import logging
7-
from importlib.util import find_spec
87
from pathlib import Path
9-
from typing import TYPE_CHECKING, Any
8+
from typing import Any
109

1110
import cv2
1211
import numpy as np
12+
from lightning_utilities.core.imports import package_available
1313
from omegaconf import DictConfig
1414
from PIL import Image
1515

@@ -21,14 +21,6 @@
2121

2222
logger = logging.getLogger("anomalib")
2323

24-
if find_spec("openvino") is not None:
25-
import openvino as ov
26-
27-
if TYPE_CHECKING:
28-
from openvino import CompiledModel
29-
else:
30-
logger.warning("OpenVINO is not installed. Please install OpenVINO to use OpenVINOInferencer.")
31-
3224

3325
class OpenVINOInferencer(Inferencer):
3426
"""OpenVINO implementation for the inference.
@@ -102,6 +94,10 @@ def __init__(
10294
task: str | None = None,
10395
config: dict | None = None,
10496
) -> None:
97+
if not package_available("openvino"):
98+
msg = "OpenVINO is not installed. Please install OpenVINO to use OpenVINOInferencer."
99+
raise ImportError(msg)
100+
105101
self.device = device
106102

107103
self.config = config
@@ -110,7 +106,7 @@ def __init__(
110106

111107
self.task = TaskType(task) if task else TaskType(self.metadata["task"])
112108

113-
def load_model(self, path: str | Path | tuple[bytes, bytes]) -> tuple[Any, Any, "CompiledModel"]:
109+
def load_model(self, path: str | Path | tuple[bytes, bytes]) -> tuple[Any, Any, Any]:
114110
"""Load the OpenVINO model.
115111
116112
Args:
@@ -121,6 +117,8 @@ def load_model(self, path: str | Path | tuple[bytes, bytes]) -> tuple[Any, Any,
121117
[tuple[str, str, ExecutableNetwork]]: Input and Output blob names
122118
together with the Executable network.
123119
"""
120+
import openvino as ov
121+
124122
core = ov.Core()
125123
# If tuple of bytes is passed
126124
if isinstance(path, tuple):

0 commit comments

Comments
 (0)