Repository navigation
Expand file tree
/
Copy pathvisualization.py
More file actions
154 lines (136 loc) · 5.07 KB
/
Copy pathvisualization.py
File metadata and controls
154 lines (136 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import multiprocessing
from queue import Queue, Empty
import atexit
import numpy as np
import cv2
process_queue_max_len = min(8, os.cpu_count())
process_queue = Queue(process_queue_max_len)
def exit_handler():
while not process_queue.empty():
try:
process = process_queue.get(block=False)
process.join()
except Empty:
pass
atexit.register(exit_handler)
# prevents memory leaks by matplotlib
def visualize_farthest_calibration_frame(*args, **kwargs):
if process_queue.full():
oldest_process = process_queue.get()
oldest_process.join()
process = multiprocessing.Process(target=visualize_farthest_calibration_frame_impl, args=args, kwargs=kwargs)
process.start()
process_queue.put(process)
# prevents memory leaks by matplotlib
def visualize_detection(*args, **kwargs):
if process_queue.full():
oldest_process = process_queue.get()
oldest_process.join()
process = multiprocessing.Process(target=visualize_detection_impl, args=args, kwargs=kwargs)
process.start()
process_queue.put(process)
def visualize_farthest_calibration_frame_impl(data_dir, transect_id, farthest_calibration_frame_disp, min_depth, max_depth):
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("pdf") # required for PyInstaller detection
plt.imshow(
np.clip(
farthest_calibration_frame_disp.data, max_depth ** -1, min_depth ** -1
)
** -1,
vmin=min_depth,
vmax=max_depth,
cmap="turbo",
)
plt.colorbar()
plt.title("Final Calibration Frame Depth")
os.makedirs(os.path.join(data_dir, "results", "calibration"), exist_ok=True)
plt.savefig(
os.path.join(data_dir, "results", "calibration", f"{transect_id}_calibration.pdf"),
bbox_inches="tight",
dpi=300,
transparent=True,
)
def visualize_detection_impl(data_dir, transect_id, detection_id, detection_frame, calibrated_depth_midas, farthest_calibration_frame_disp, boxes, masks, world_positions, sample_locations, draw_detection_ids, draw_world_position, min_depth, max_depth):
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches
matplotlib.use("pdf") # required for PyInstaller detection
scale = 2
if farthest_calibration_frame_disp is not None:
fig, (ax1, ax2, ax3) = plt.subplots(
1, 3, figsize=(scale * 6.202, scale * 1.5)
)
else:
fig, (ax1, ax2) = plt.subplots(
1, 2, figsize=(scale * 6.202, scale * 1.5)
)
ax1.imshow(detection_frame[..., ::-1])
ax1.set_title("Observation")
for i, (box, mask, world_pos) in enumerate(zip(boxes, masks, world_positions)):
if mask is not None:
mask_rgb = np.zeros((*detection_frame.shape[0:2], 4))
mask_rgb[:, :, [0, 3]] = mask[..., None]
mask_rgb[..., 3] *= 0.25
ax1.imshow((mask_rgb * 255).astype(np.uint8))
rect = matplotlib.patches.Rectangle(
(box[0], box[1]),
box[2] - box[0],
box[3] - box[1],
linewidth=1,
edgecolor="r",
facecolor="none",
)
ax1.add_patch(rect)
label = ""
if draw_detection_ids:
label += f"#{i:03d}"
if draw_world_position:
label += "@" + ",".join([f"{e:.2f}m" for e in world_pos])
rx, ry = rect.get_xy()
ax1.annotate(label, (rx, ry - 5), color="red", fontsize=6)
ax1.get_xaxis().set_visible(False)
ax1.get_yaxis().set_visible(False)
im = ax2.imshow(calibrated_depth_midas, vmin=min_depth, vmax=max_depth, cmap="turbo")
ax2.set_title("Depth")
ax2.get_xaxis().set_visible(False)
ax2.get_yaxis().set_visible(False)
sample_locations = np.array(sample_locations)
if len(sample_locations) > 0:
for ax in [ax1, ax2]:
ax.scatter(
sample_locations[:, 1],
sample_locations[:, 0],
color="r",
marker="x",
)
if farthest_calibration_frame_disp is not None:
im = ax3.imshow(
np.clip(
farthest_calibration_frame_disp.data,
max_depth ** -1,
min_depth ** -1,
)
** -1,
vmin=min_depth,
vmax=max_depth,
cmap="turbo",
)
ax3.set_title("Reference Depth")
ax3.get_xaxis().set_visible(False)
ax3.get_yaxis().set_visible(False)
fig.subplots_adjust(
bottom=0.1, top=0.9, left=0.1, right=0.8, wspace=0.02, hspace=0.02
)
a = 0.50
cbar_ax = fig.add_axes([0.805, (1 - a) / 2, 0.02, a])
cbar = fig.colorbar(im, cax=cbar_ax, ax=[ax2, ax3] if farthest_calibration_frame_disp is not None else [ax2])
cbar.set_label("Depth [m]")
os.makedirs(os.path.join(data_dir, "results", "sampling"), exist_ok=True)
plt.savefig(
os.path.join(data_dir, "results", "sampling", transect_id + "_" + detection_id + ".pdf"),
bbox_inches="tight",
dpi=300,
transparent=True,
)