-
Notifications
You must be signed in to change notification settings - Fork 19
Add HStack Layout #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sovrasov
merged 3 commits into
open-edge-platform:master
from
ashwinvaidya17:ashwin/visualization_3
Jan 8, 2025
Merged
Add HStack Layout #250
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| """Visualization Layout.""" | ||
|
|
||
| # Copyright (C) 2024-2025 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from .flatten import Flatten | ||
| from .hstack import HStack | ||
| from .layout import Layout | ||
|
|
||
| __all__ = ["Flatten", "HStack", "Layout"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Horizontal Stack Layout.""" | ||
|
|
||
| # Copyright (C) 2024-2025 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Type, Union | ||
|
|
||
| import PIL | ||
|
|
||
| from .layout import Layout | ||
|
|
||
| if TYPE_CHECKING: | ||
| from model_api.visualizer.primitive import Primitive | ||
| from model_api.visualizer.scene import Scene | ||
|
|
||
|
|
||
| class HStack(Layout): | ||
| """Horizontal Stack Layout. | ||
|
|
||
| Args: | ||
| *args (Union[Type[Primitive], Layout]): Primitives or layouts to be applied. | ||
| """ | ||
|
|
||
| def __init__(self, *args: Union[Type[Primitive], Layout]) -> None: | ||
| self.children = args | ||
|
|
||
| def _compute_on_primitive(self, primitive: Type[Primitive], image: PIL.Image, scene: Scene) -> PIL.Image | None: | ||
| if scene.has_primitives(primitive): | ||
| images = [] | ||
| for _primitive in scene.get_primitives(primitive): | ||
| _image = _primitive.compute(image.copy()) | ||
| images.append(_image) | ||
| return self._stitch(*images) | ||
| return None | ||
|
|
||
| @staticmethod | ||
| def _stitch(*images: PIL.Image) -> PIL.Image: | ||
| """Stitch images horizontally. | ||
|
|
||
| Args: | ||
| images (PIL.Image): Images to be stitched. | ||
|
|
||
| Returns: | ||
| PIL.Image: Stitched image. | ||
| """ | ||
| new_image = PIL.Image.new( | ||
| "RGB", | ||
| ( | ||
| sum(image.width for image in images), | ||
| max(image.height for image in images), | ||
| ), | ||
| ) | ||
| x_offset = 0 | ||
| for image in images: | ||
| new_image.paste(image, (x_offset, 0)) | ||
| x_offset += image.width | ||
| return new_image | ||
|
|
||
| def __call__(self, scene: Scene) -> PIL.Image: | ||
| """Stitch images horizontally. | ||
|
|
||
| Args: | ||
| scene (Scene): Scene to be stitched. | ||
|
|
||
| Returns: | ||
| PIL.Image: Stitched image. | ||
| """ | ||
| images: list[PIL.Image] = [] | ||
| for child in self.children: | ||
| if isinstance(child, Layout): | ||
| _image = child(scene) | ||
| else: | ||
| _image = self._compute_on_primitive(child, scene.base.copy(), scene) | ||
| if _image is not None: | ||
| images.append(_image) | ||
| return self._stitch(*images) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Visualization Layout.""" | ||
|
|
||
| # Copyright (C) 2024-2025 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import TYPE_CHECKING, Type | ||
|
|
||
| if TYPE_CHECKING: | ||
| import PIL | ||
|
|
||
| from model_api.visualizer.primitive import Primitive | ||
|
|
||
| from .scene import Scene | ||
|
|
||
|
|
||
| class Layout(ABC): | ||
| """Base class for layouts.""" | ||
|
|
||
| @abstractmethod | ||
| def _compute_on_primitive(self, primitive: Type[Primitive], image: PIL.Image, scene: Scene) -> PIL.Image | None: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def __call__(self, scene: Scene) -> PIL.Image: | ||
| """Compute the layout.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| """Visualization tests.""" | ||
|
|
||
| # Copyright (C) 2024 Intel Corporation | ||
| # Copyright (C) 2024-2025 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.