Skip to content

Commit 9ec4d8d

Browse files
committed
Visualizer can accept np images
1 parent f6126ba commit 9ec4d8d

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/python/model_api/visualizer/visualizer.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
# Copyright (C) 2024 Intel Corporation
44
# SPDX-License-Identifier: Apache-2.0
55

6-
from pathlib import Path
7-
from typing import Union
6+
from __future__ import annotations # TODO: remove when Python3.9 support is dropped
87

8+
from typing import TYPE_CHECKING
9+
10+
import numpy as np
911
from PIL import Image
1012

1113
from model_api.models.result import (
@@ -18,7 +20,6 @@
1820
Result,
1921
)
2022

21-
from .layout import Layout
2223
from .scene import (
2324
AnomalyScene,
2425
ClassificationScene,
@@ -29,18 +30,27 @@
2930
SegmentationScene,
3031
)
3132

33+
if TYPE_CHECKING:
34+
from pathlib import Path
35+
36+
from .layout import Layout
37+
3238

3339
class Visualizer:
3440
"""Utility class to automatically select the correct scene and render/show it."""
3541

36-
def __init__(self, layout: Union[Layout, None] = None) -> None:
42+
def __init__(self, layout: Layout | None = None) -> None:
3743
self.layout = layout
3844

39-
def show(self, image: Image, result: Result) -> Image:
45+
def show(self, image: Image | np.ndarray, result: Result) -> None:
46+
if isinstance(image, np.ndarray):
47+
image = Image.fromarray(image)
4048
scene = self._scene_from_result(image, result)
4149
return scene.show()
4250

43-
def save(self, image: Image, result: Result, path: Path) -> None:
51+
def save(self, image: Image | np.ndarray, result: Result, path: Path) -> None:
52+
if isinstance(image, np.ndarray):
53+
image = Image.fromarray(image)
4454
scene = self._scene_from_result(image, result)
4555
scene.save(path)
4656

0 commit comments

Comments
 (0)