-
Notifications
You must be signed in to change notification settings - Fork 657
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
Visualizer improvements pt1 #293
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bcabc3c
add methods for adding labels to image
djdameln 97fad83
refactor visualizer
djdameln bfbf6d1
move add_label to post processing helpers
djdameln 1981238
fix tests
djdameln 2b654a5
support single image in visualizer
djdameln f51be3b
call generate
djdameln 6e19e8e
revert default task type
djdameln 6d6054d
typing
djdameln 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 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 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 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 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 |
---|---|---|
|
@@ -15,9 +15,9 @@ | |
# and limitations under the License. | ||
|
||
from pathlib import Path | ||
from typing import Optional, Tuple | ||
from typing import Optional | ||
|
||
import cv2 | ||
import matplotlib.figure | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
|
@@ -29,65 +29,46 @@ class Visualizer: | |
either be logged by accessing the `figure` attribute or can be saved directly by calling `save()` method. | ||
|
||
Example: | ||
>>> visualizer = Visualizer(num_rows=1, num_cols=5, figure_size=(12, 3)) | ||
>>> visualizer = Visualizer() | ||
>>> visualizer.add_image(image=image, title="Image") | ||
>>> visualizer.close() | ||
|
||
Args: | ||
num_rows (int): Number of rows of images in the figure. | ||
num_cols (int): Number of columns/images in each row. | ||
figure_size (Tuple[int, int]): Size of output figure | ||
""" | ||
|
||
def __init__(self, num_rows: int, num_cols: int, figure_size: Tuple[int, int]): | ||
self.figure_index: int = 0 | ||
def __init__(self): | ||
|
||
self.figure, self.axis = plt.subplots(num_rows, num_cols, figsize=figure_size) | ||
self.figure.subplots_adjust(right=0.9) | ||
self.images = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add annotation to |
||
|
||
for axis in self.axis: | ||
axis.axes.xaxis.set_visible(False) | ||
axis.axes.yaxis.set_visible(False) | ||
self.figure: matplotlib.figure.Figure | ||
self.axis: np.ndarray | ||
|
||
def add_image(self, image: np.ndarray, title: str, color_map: Optional[str] = None, index: Optional[int] = None): | ||
def add_image(self, image: np.ndarray, title: str, color_map: Optional[str] = None): | ||
"""Add image to figure. | ||
|
||
Args: | ||
image (np.ndarray): Image which should be added to the figure. | ||
title (str): Image title shown on the plot. | ||
color_map (Optional[str]): Name of matplotlib color map used to map scalar data to colours. Defaults to None. | ||
index (Optional[int]): Figure index. Defaults to None. | ||
""" | ||
if index is None: | ||
index = self.figure_index | ||
self.figure_index += 1 | ||
|
||
self.axis[index].imshow(image, color_map, vmin=0, vmax=255) | ||
self.axis[index].title.set_text(title) | ||
|
||
def add_text(self, image: np.ndarray, text: str, font: int = cv2.FONT_HERSHEY_PLAIN): | ||
"""Puts text on an image. | ||
|
||
Args: | ||
image (np.ndarray): Input image. | ||
text (str): Text to add. | ||
font (Optional[int]): cv2 font type. Defaults to 0. | ||
|
||
Returns: | ||
np.ndarray: Image with text. | ||
""" | ||
image = image.copy() | ||
font_size = image.shape[1] // 256 + 1 # Text scale is calculated based on the reference size of 256 | ||
image_data = dict(image=image, title=title, color_map=color_map) | ||
self.images.append(image_data) | ||
|
||
def generate(self): | ||
"""Generate the image.""" | ||
num_cols = len(self.images) | ||
figure_size = (num_cols * 3, 3) | ||
self.figure, self.axis = plt.subplots(1, num_cols, figsize=figure_size) | ||
self.figure.subplots_adjust(right=0.9) | ||
|
||
for i, line in enumerate(text.split("\n")): | ||
(text_w, text_h), baseline = cv2.getTextSize(line.strip(), font, font_size, thickness=1) | ||
offset = i * text_h | ||
cv2.rectangle(image, (0, offset + baseline // 2), (0 + text_w, 0 + text_h + offset), (255, 255, 255), -1) | ||
cv2.putText(image, line.strip(), (0, (baseline // 2 + text_h) + offset), font, font_size, (0, 0, 255)) | ||
return image | ||
axes = self.axis if len(self.images) > 1 else [self.axis] | ||
for axis, image_dict in zip(axes, self.images): | ||
axis.axes.xaxis.set_visible(False) | ||
axis.axes.yaxis.set_visible(False) | ||
axis.imshow(image_dict["image"], image_dict["color_map"], vmin=0, vmax=255) | ||
axis.title.set_text(image_dict["title"]) | ||
|
||
def show(self): | ||
"""Show image on a matplotlib figure.""" | ||
self.generate() | ||
self.figure.show() | ||
|
||
def save(self, filename: Path): | ||
|
@@ -96,6 +77,7 @@ def save(self, filename: Path): | |
Args: | ||
filename (Path): Filename to save image | ||
""" | ||
self.generate() | ||
filename.parent.mkdir(parents=True, exist_ok=True) | ||
self.figure.savefig(filename, dpi=100) | ||
|
||
|
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was committed unintentionally. I'll revert it