Skip to content

Commit

Permalink
🐞 Defer OpenVINO import to avoid unnecessary warnings (#2385)
Browse files Browse the repository at this point in the history
* Fix openvino import issue

Signed-off-by: Samet Akcay <[email protected]>

* Fix pre-commit issues

Signed-off-by: Samet Akcay <[email protected]>

---------

Signed-off-by: Samet Akcay <[email protected]>
  • Loading branch information
samet-akcay authored Oct 22, 2024
1 parent 1465b05 commit 6eeb7f6
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/anomalib/deploy/inferencers/openvino_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand Down

0 comments on commit 6eeb7f6

Please sign in to comment.