|
3 | 3 | # Copyright (C) 2024 Intel Corporation |
4 | 4 | # SPDX-License-Identifier: Apache-2.0 |
5 | 5 |
|
6 | | -from pathlib import Path |
7 | | -from typing import Union |
| 6 | +from __future__ import annotations # TODO: remove when Python3.9 support is dropped |
8 | 7 |
|
| 8 | +from typing import TYPE_CHECKING |
| 9 | + |
| 10 | +import numpy as np |
9 | 11 | from PIL import Image |
10 | 12 |
|
11 | 13 | from model_api.models.result import ( |
|
18 | 20 | Result, |
19 | 21 | ) |
20 | 22 |
|
21 | | -from .layout import Layout |
22 | 23 | from .scene import ( |
23 | 24 | AnomalyScene, |
24 | 25 | ClassificationScene, |
|
29 | 30 | SegmentationScene, |
30 | 31 | ) |
31 | 32 |
|
| 33 | +if TYPE_CHECKING: |
| 34 | + from pathlib import Path |
| 35 | + |
| 36 | + from .layout import Layout |
| 37 | + |
32 | 38 |
|
33 | 39 | class Visualizer: |
34 | 40 | """Utility class to automatically select the correct scene and render/show it.""" |
35 | 41 |
|
36 | | - def __init__(self, layout: Union[Layout, None] = None) -> None: |
| 42 | + def __init__(self, layout: Layout | None = None) -> None: |
37 | 43 | self.layout = layout |
38 | 44 |
|
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) |
40 | 48 | scene = self._scene_from_result(image, result) |
41 | 49 | return scene.show() |
42 | 50 |
|
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) |
44 | 54 | scene = self._scene_from_result(image, result) |
45 | 55 | scene.save(path) |
46 | 56 |
|
|
0 commit comments