Skip to content

Commit f8a55c9

Browse files
Refactor primitives
Signed-off-by: Ashwin Vaidya <[email protected]>
1 parent e97b12c commit f8a55c9

File tree

4 files changed

+68
-37
lines changed

4 files changed

+68
-37
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Primitive classes."""
2+
3+
# Copyright (C) 2025 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
from .bounding_box import BoundingBox
7+
from .overlay import Overlay
8+
from .primitive import Primitive
9+
10+
__all__ = ["Primitive", "BoundingBox", "Overlay"]

model_api/python/model_api/visualizer/primitive.py renamed to model_api/python/model_api/visualizer/primitive/bounding_box.py

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
"""Base class for primitives."""
1+
"""Bounding box primitive."""
22

3-
# Copyright (C) 2024 Intel Corporation
3+
# Copyright (C) 2025 Intel Corporation
44
# SPDX-License-Identifier: Apache-2.0
55

66
from __future__ import annotations
77

8-
from abc import ABC, abstractmethod
9-
10-
import numpy as np
11-
import PIL
128
from PIL import Image, ImageDraw
139

14-
15-
class Primitive(ABC):
16-
"""Primitive class."""
17-
18-
@abstractmethod
19-
def compute(self, image: Image) -> Image:
20-
pass
10+
from .primitive import Primitive
2111

2212

2313
class BoundingBox(Primitive):
@@ -71,27 +61,3 @@ def compute(self, image: Image) -> Image:
7161
draw.text((0, 0), self.label, fill="white")
7262
image.paste(label_image, (self.x1, self.y1))
7363
return image
74-
75-
76-
class Overlay(Primitive):
77-
"""Overlay primitive.
78-
79-
Useful for XAI and Anomaly Maps.
80-
81-
Args:
82-
image (PIL.Image | np.ndarray): Image to be overlaid.
83-
opacity (float): Opacity of the overlay.
84-
"""
85-
86-
def __init__(self, image: PIL.Image | np.ndarray, opacity: float = 0.4) -> None:
87-
self.image = self._to_pil(image)
88-
self.opacity = opacity
89-
90-
def _to_pil(self, image: PIL.Image | np.ndarray) -> PIL.Image:
91-
if isinstance(image, np.ndarray):
92-
return PIL.Image.fromarray(image)
93-
return image
94-
95-
def compute(self, image: PIL.Image) -> PIL.Image:
96-
_image = self.image.resize(image.size)
97-
return PIL.Image.blend(image, _image, self.opacity)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Overlay primitive."""
2+
3+
# Copyright (C) 2025 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
from __future__ import annotations
7+
8+
import numpy as np
9+
import PIL
10+
11+
from .primitive import Primitive
12+
13+
14+
class Overlay(Primitive):
15+
"""Overlay primitive.
16+
17+
Useful for XAI and Anomaly Maps.
18+
19+
Args:
20+
image (PIL.Image | np.ndarray): Image to be overlaid.
21+
opacity (float): Opacity of the overlay.
22+
"""
23+
24+
def __init__(self, image: PIL.Image | np.ndarray, opacity: float = 0.4) -> None:
25+
self.image = self._to_pil(image)
26+
self.opacity = opacity
27+
28+
def _to_pil(self, image: PIL.Image | np.ndarray) -> PIL.Image:
29+
if isinstance(image, np.ndarray):
30+
return PIL.Image.fromarray(image)
31+
return image
32+
33+
def compute(self, image: PIL.Image) -> PIL.Image:
34+
image_ = self.image.resize(image.size)
35+
return PIL.Image.blend(image, image_, self.opacity)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Base class for primitives."""
2+
3+
# Copyright (C) 2025 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
from __future__ import annotations
7+
8+
from abc import ABC, abstractmethod
9+
from typing import TYPE_CHECKING
10+
11+
if TYPE_CHECKING:
12+
import PIL
13+
14+
15+
class Primitive(ABC):
16+
"""Base class for primitives."""
17+
18+
@abstractmethod
19+
def compute(self, image: PIL.Image) -> PIL.Image:
20+
"""Compute the primitive."""

0 commit comments

Comments
 (0)