Skip to content
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

Enhance visualizer to toggle plot title visibility #1228

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
(<https://github.com/openvinotoolkit/datumaro/pull/1182>)
- Add ImageColorScale context manager
(<https://github.com/openvinotoolkit/datumaro/pull/1194>)
- Enhance visualizer to toggle plot title visibility
(<https://github.com/openvinotoolkit/datumaro/pull/1228>)

### Bug fixes
- Fix wrong example of Datumaro dataset creation in document
Expand Down
31 changes: 20 additions & 11 deletions src/datumaro/components/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def __init__(
bbox_linewidth: float = 1.0,
text_y_offset: float = 1.5,
alpha: float = 1.0,
show_plot_title: bool = True,
) -> None:
"""
Visualizer for Datumaro annotations
Expand All @@ -114,6 +115,9 @@ def __init__(
alpha:
Transparency value when drawing annotations. It should be in [0, 1].
If alpha=0, we do not draw any annotations.
show_plot_title:
If True, show the plot title formatted as "ID: {item_id}, Subset: {subset}".
Otherwise, hide the plot title.
"""
self.dataset = dataset
self.figsize = figsize
Expand All @@ -124,6 +128,7 @@ def __init__(

assert 0.0 <= alpha <= 1.0, "alpha should be in [0, 1]."
self.alpha = alpha
self.show_plot_title = show_plot_title

self._items = [item for item in self.dataset]

Expand Down Expand Up @@ -430,17 +435,8 @@ def _parse_inputs(
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
ax.imshow(img)

width = ax.transAxes.transform_point((1, 0))[0] - ax.transAxes.transform_point((0, 0))[0]
text = ax.set_title(f"ID: {item_id}, Subset: {subset}", loc="center", wrap=True)
text.__get_wrapped_text = text._get_wrapped_text

def _get_wrapped_text():
wrapped_text = text.__get_wrapped_text()
text._text = wrapped_text
return wrapped_text

text._get_wrapped_text = _get_wrapped_text
text._get_wrap_line_width = lambda: width
if self.show_plot_title:
self._plot_title(ax, item_id, subset)

ax.set_axis_off()

Expand Down Expand Up @@ -468,6 +464,19 @@ def _get_wrapped_text():

return fig

def _plot_title(self, ax: Axes, item_id: str, subset: str) -> None:
width = ax.transAxes.transform_point((1, 0))[0] - ax.transAxes.transform_point((0, 0))[0]
text = ax.set_title(f"ID: {item_id}, Subset: {subset}", loc="center", wrap=True)
text.__get_wrapped_text = text._get_wrapped_text

def _get_wrapped_text():
wrapped_text = text.__get_wrapped_text()
text._text = wrapped_text
return wrapped_text

text._get_wrapped_text = _get_wrapped_text
text._get_wrap_line_width = lambda: width

def _draw_label(
self,
ann: Label,
Expand Down
Loading