File size: 7,007 Bytes
663494c |
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
import cv2
import numpy as np
from shapely import affinity
from shapely.geometry import LineString, box
def get_patch_coord(patch_box, patch_angle=0.0):
patch_x, patch_y, patch_h, patch_w = patch_box
x_min = patch_x - patch_w / 2.0
y_min = patch_y - patch_h / 2.0
x_max = patch_x + patch_w / 2.0
y_max = patch_y + patch_h / 2.0
patch = box(x_min, y_min, x_max, y_max)
patch = affinity.rotate(
patch, patch_angle, origin=(patch_x, patch_y), use_radians=False
)
return patch
def get_discrete_degree(vec, angle_class=36):
deg = np.mod(np.degrees(np.arctan2(vec[1], vec[0])), 360)
deg = (int(deg / (360 / angle_class) + 0.5) % angle_class) + 1
return deg
def mask_for_lines(lines, mask, thickness, idx, type="index", angle_class=36):
# draw mask for rasterization of a single line
coords = np.asarray(list(lines.coords), np.int32)
coords = coords.reshape((-1, 2))
if len(coords) < 2:
return mask, idx
if type == "backward":
coords = np.flip(coords, 0)
if type == "index":
cv2.polylines(mask, [coords], False, color=idx, thickness=thickness)
idx += 1
else:
for i in range(len(coords) - 1):
cv2.polylines(
mask,
[coords[i:]],
False,
color=get_discrete_degree(
coords[i + 1] - coords[i], angle_class=angle_class
),
thickness=thickness,
)
return mask, idx
def line_geom_to_mask(
layer_geom,
confidence_levels,
local_box,
canvas_size,
thickness,
idx,
type="index",
angle_class=36,
):
patch_x, patch_y, patch_h, patch_w = local_box
patch = get_patch_coord(local_box)
canvas_h = canvas_size[0]
canvas_w = canvas_size[1]
scale_height = canvas_h / patch_h
scale_width = canvas_w / patch_w
trans_x = -patch_x + patch_w / 2.0
trans_y = -patch_y + patch_h / 2.0
map_mask = np.zeros(canvas_size, np.uint8)
# loop through different line instances for a class
for line in layer_geom:
if isinstance(line, tuple):
line, confidence = line
else:
confidence = None
new_line = line.intersection(patch)
if not new_line.is_empty:
new_line = affinity.affine_transform(
new_line, [1.0, 0.0, 0.0, 1.0, trans_x, trans_y]
)
new_line = affinity.scale(
new_line, xfact=scale_width, yfact=scale_height, origin=(0, 0)
)
confidence_levels.append(confidence)
# draw line and add idx by 1 every time, use it as color for instances
if new_line.geom_type == "MultiLineString":
for new_single_line in new_line:
map_mask, idx = mask_for_lines(
new_single_line, map_mask, thickness, idx, type, angle_class
)
else:
map_mask, idx = mask_for_lines(
new_line, map_mask, thickness, idx, type, angle_class
)
return map_mask, idx
def overlap_filter(mask, filter_mask):
C, _, _ = mask.shape
for c in range(C - 1, -1, -1):
filter = np.repeat((filter_mask[c] != 0)[None, :], c, axis=0)
mask[:c][filter] = 0
return mask
def preprocess_map(
vectors, patch_size, canvas_size, num_classes, thickness, angle_class
):
confidence_levels = [-1]
vector_num_list = {}
for i in range(num_classes):
vector_num_list[i] = []
# cluster the lines based on their classes, easier to retrieve
for vector in vectors:
if vector["pts_num"] >= 2:
vector_num_list[vector["type"]].append(
LineString(vector["pts"][: vector["pts_num"]])
)
# print(vector_num_list)
local_box = (0.0, 0.0, patch_size[0], patch_size[1])
idx = 1
filter_masks = []
instance_masks = []
forward_masks = []
backward_masks = []
# go through different classes of lines
for i in range(num_classes):
# print('\n')
# print(idx)
map_mask, idx = line_geom_to_mask(
vector_num_list[i],
confidence_levels,
local_box,
canvas_size,
thickness,
idx,
)
# print(map_mask.shape)
# print(np.unique(map_mask))
instance_masks.append(map_mask)
# print(idx)
filter_mask, _ = line_geom_to_mask(
vector_num_list[i],
confidence_levels,
local_box,
canvas_size,
thickness + 4,
1,
)
filter_masks.append(filter_mask)
forward_mask, _ = line_geom_to_mask(
vector_num_list[i],
confidence_levels,
local_box,
canvas_size,
thickness,
1,
type="forward",
angle_class=angle_class,
)
forward_masks.append(forward_mask)
backward_mask, _ = line_geom_to_mask(
vector_num_list[i],
confidence_levels,
local_box,
canvas_size,
thickness,
1,
type="backward",
angle_class=angle_class,
)
backward_masks.append(backward_mask)
# zxc
filter_masks = np.stack(filter_masks)
instance_masks = np.stack(instance_masks)
forward_masks = np.stack(forward_masks)
backward_masks = np.stack(backward_masks)
# filter overlapping area, some lane divider / road divider / ped crossing will be gone
# print(len(instance_masks))
instance_masks = overlap_filter(instance_masks, filter_masks)
forward_masks = overlap_filter(forward_masks, filter_masks).sum(0).astype("int32")
backward_masks = overlap_filter(backward_masks, filter_masks).sum(0).astype("int32") # 2 x H x W
# print(len(instance_masks))
# zxc
semantic_masks = instance_masks != 0
return semantic_masks, instance_masks, forward_masks, backward_masks
def rasterize_map(vectors, patch_size, canvas_size, num_classes, thickness):
confidence_levels = [-1]
vector_num_list = {}
for i in range(num_classes):
vector_num_list[i] = []
for vector in vectors:
if vector["pts_num"] >= 2:
vector_num_list[vector["type"]].append(
(
LineString(vector["pts"][: vector["pts_num"]]),
vector.get("confidence_level", 1),
)
)
local_box = (0.0, 0.0, patch_size[0], patch_size[1])
idx = 1
masks = []
for i in range(num_classes):
map_mask, idx = line_geom_to_mask(
vector_num_list[i],
confidence_levels,
local_box,
canvas_size,
thickness,
idx,
)
masks.append(map_mask)
return np.stack(masks), confidence_levels
|