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

Alobugdays - 12 black square title #275

Merged
merged 3 commits into from
Nov 18, 2022
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: 1 addition & 1 deletion aloscene/points_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def get_view(self, frame=None, size: Union[tuple, None] = None, labels_set: Unio
str(int(label)),
pos_x=int(x1) + 10,
pos_y=int(y1) + 10,
color=color,
text_color=color,
square_background=False,
)

Expand Down
63 changes: 44 additions & 19 deletions aloscene/renderer/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ def adapt_text_size_to_frame(size, frame_size):


def put_adapative_cv2_text(
frame: np.array, frame_size: tuple, text: str, pos_x: float, pos_y: float, color=None, square_background=True
frame: np.array,
frame_size: tuple,
text: str,
pos_x: float,
pos_y: float,
text_color=None,
background_color=None,
square_background=True,
views_counter=None
):
"""Put Text on the given frame with adaptive size.

Expand All @@ -31,27 +39,35 @@ def put_adapative_cv2_text(
pos_y: int
"""
size_h, size_w = adapt_text_size_to_frame(1.0, frame_size)
c_size_h = int(size_w * 20)
w_size_w = int(size_w * 20)

(w, h), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, (size_h + size_w) / 2, -1)

# Decrease text size if too long
if w > (frame_size[1] / (views_counter / 2)) and views_counter:
size_h /= 2
size_w /= 2

(w, h), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, (size_h + size_w) / 2, -1)

pos_x = int(pos_x)
pos_y = int(pos_y)

if square_background:
cv2.rectangle(
frame,
(pos_x - 3, pos_y - c_size_h - c_size_h),
(pos_x + (w_size_w * (len(text) + 1)), pos_y + c_size_h),
(1, 1, 1),
(pos_x, int(pos_y - 2 * h)),
(pos_x + w + 3, int(pos_y + 3 * h) ),
(1, 1, 1) if background_color is None else background_color,
-1,
)

cv2.putText(
frame,
text,
(int(pos_x), int(pos_y)),
(int(pos_x), int(pos_y + 2 * h)),
cv2.FONT_HERSHEY_SIMPLEX,
(size_h + size_w) / 2,
(0, 0, 0) if color is None else color,
(0, 0, 0) if text_color is None else text_color,
1,
cv2.LINE_AA,
)
Expand Down Expand Up @@ -143,7 +159,7 @@ def _matplotlib_render(self, view: np.ndarray):
plt.show()

@classmethod
def get_grid_view(cls, views: list, cell_grid_size=None, grid_size=None, **kwargs):
def get_grid_view(cls, views: list, cell_grid_size=None, grid_size=None, add_title=True, **kwargs):
"""Get a grid of view from multiple view"""

smallest_view = None
Expand All @@ -167,7 +183,9 @@ def get_grid_view(cls, views: list, cell_grid_size=None, grid_size=None, **kwarg
while v < len(views):
start = int(v % grid_size) * target_display_shape[1]
line[:, start : start + target_display_shape[1], :] = views[v].image
cls.add_title(line, (start, 0), views[v].title)

if add_title:
cls.add_title(line, (start, 0), views[v].title, len(views))

v += 1
if v % grid_size == 0 or v == len(views):
Expand All @@ -177,7 +195,7 @@ def get_grid_view(cls, views: list, cell_grid_size=None, grid_size=None, **kwarg
return np.concatenate(lines, axis=0)

@staticmethod
def add_title(array, start, title):
def add_title(array, start, title, views_counter):
"""
Add a box with view title

Expand All @@ -193,18 +211,22 @@ def add_title(array, start, title):
if title is None:
return
else:
x, y = start
cv2.rectangle(array, (x, y), (x + 10 + (len(title) * 6), y + 10), (0, 0, 0), -1)
cv2.putText(array, title, (x + 10, y + 8), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0.8, 0.8, 0.8), 1, cv2.LINE_AA)
put_adapative_cv2_text(array,
array.shape,
title,
start[0],
start[1],
text_color=(1, 1, 1),
background_color=(0, 0, 0),
views_counter=views_counter)

@classmethod
def get_user_defined_grid_view(cls, views):
def get_user_defined_grid_view(cls, views, add_title):
"""
Create grid_view from list of list, without resizing views

view : list of list of views with view[i][j] = view for line i, colomun j
"""

# create blank image
Hf, Wf = 0, 0
for line in views:
Expand All @@ -222,7 +244,9 @@ def get_user_defined_grid_view(cls, views):
for v in line:
h, w = v.image.shape[:2]
final_view[y : y + h, x : x + w, :] = v.image
cls.add_title(final_view, (x, y), v.title)

if add_title:
cls.add_title(final_view, (x, y), v.title)
x += w
hmax = max(hmax, h)
y += hmax
Expand All @@ -237,6 +261,7 @@ def render(
fps=30,
grid_size=None,
skip_views=False,
add_title=True
):
"""Render a list of view using the given renderer.

Expand All @@ -260,9 +285,9 @@ def render(
if skip_views and record_file is None:
raise ValueError("When skip_views is desired, a record_file must be provided.")
if isinstance(views[0], list):
view = self.get_user_defined_grid_view(views)
view = self.get_user_defined_grid_view(views, add_title)
else:
view = self.get_grid_view(views, cell_grid_size=cell_grid_size, grid_size=grid_size)
view = self.get_grid_view(views, cell_grid_size=cell_grid_size, grid_size=grid_size, add_title=add_title)
if not skip_views:
self.renderer_to_fn[renderer](view)
if record_file is not None and self.out is None:
Expand Down
6 changes: 3 additions & 3 deletions aloscene/tensors/spatial_augmented_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def append_cam_intrinsic(self, cam_intrinsic: CameraIntrinsic):
def append_cam_extrinsic(self, cam_extrinsic: CameraExtrinsic):
self._append_child("cam_extrinsic", cam_extrinsic)

def get_view(self, views: list = [], exclude=[], size=None, grid_size=None, **kwargs):
def get_view(self, views: list = [], exclude=[], size=None, grid_size=None, add_title=True, **kwargs):
"""Render the spatial augmented tensor.

Parameters
Expand All @@ -130,7 +130,7 @@ def get_view(self, views: list = [], exclude=[], size=None, grid_size=None, **kw
"""
_views = [v for v in views if isinstance(v, View)]
if len(_views) > 0:
return View(Renderer.get_grid_view(_views, grid_size=None, cell_grid_size=size, **kwargs))
return View(Renderer.get_grid_view(_views, grid_size=None, cell_grid_size=size, add_title=add_title, **kwargs))

# Include type
include_type = [
Expand Down Expand Up @@ -193,7 +193,7 @@ def __get_view(sa_tensor: SpatialAugmentedTensor, info={}):
else:
grid_size = None

view = Renderer.get_grid_view(n_views, grid_size=grid_size, cell_grid_size=size, **kwargs)
view = Renderer.get_grid_view(n_views, grid_size=grid_size, cell_grid_size=size, add_title=add_title, **kwargs)
return View(view)

def relative_to_absolute(self, x, dim, assert_integer=False):
Expand Down