Update utils.py
Browse files
utils.py
CHANGED
|
@@ -90,15 +90,20 @@ class UnionFind:
|
|
| 90 |
return labels
|
| 91 |
|
| 92 |
def visualise_single_image_prediction(image_as_np_array, predictions, filename):
|
| 93 |
-
|
| 94 |
-
if h > w:
|
| 95 |
-
figure, subplot = plt.subplots(1, 1, figsize=(10, 10 * h / w))
|
| 96 |
-
else:
|
| 97 |
-
figure, subplot = plt.subplots(1, 1, figsize=(10 * w / h, 10))
|
| 98 |
subplot.imshow(image_as_np_array)
|
| 99 |
plot_bboxes(subplot, predictions["panels"], color="green")
|
| 100 |
-
plot_bboxes(subplot, predictions["texts"], color="red",
|
| 101 |
plot_bboxes(subplot, predictions["characters"], color="blue")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
COLOURS = [
|
| 104 |
"#b7ff51", # green
|
|
@@ -141,14 +146,24 @@ def visualise_single_image_prediction(image_as_np_array, predictions, filename):
|
|
| 141 |
subplot.plot([x2], [y2], color=random_colour, marker="o", markersize=5)
|
| 142 |
|
| 143 |
for (i, j) in predictions["text_character_associations"]:
|
| 144 |
-
score = predictions["dialog_confidences"][i]
|
| 145 |
bbox_i = predictions["texts"][i]
|
| 146 |
bbox_j = predictions["characters"][j]
|
|
|
|
|
|
|
| 147 |
x1 = bbox_i[0] + (bbox_i[2] - bbox_i[0]) / 2
|
| 148 |
y1 = bbox_i[1] + (bbox_i[3] - bbox_i[1]) / 2
|
| 149 |
x2 = bbox_j[0] + (bbox_j[2] - bbox_j[0]) / 2
|
| 150 |
y2 = bbox_j[1] + (bbox_j[3] - bbox_j[1]) / 2
|
| 151 |
-
subplot.plot([x1, x2], [y1, y2], color="red", linewidth=2, linestyle="dashed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
subplot.axis("off")
|
| 154 |
if filename is not None:
|
|
@@ -159,17 +174,18 @@ def visualise_single_image_prediction(image_as_np_array, predictions, filename):
|
|
| 159 |
plt.close()
|
| 160 |
return image
|
| 161 |
|
| 162 |
-
def plot_bboxes(subplot, bboxes, color="red",
|
|
|
|
|
|
|
| 163 |
for id, bbox in enumerate(bboxes):
|
|
|
|
|
|
|
| 164 |
w = bbox[2] - bbox[0]
|
| 165 |
h = bbox[3] - bbox[1]
|
| 166 |
rect = patches.Rectangle(
|
| 167 |
bbox[:2], w, h, linewidth=1, edgecolor=color, facecolor="none", linestyle="solid"
|
| 168 |
)
|
| 169 |
subplot.add_patch(rect)
|
| 170 |
-
if add_index:
|
| 171 |
-
cx, cy = bbox[0] + w / 2, bbox[1] + h / 2
|
| 172 |
-
subplot.text(cx, cy, str(id), color=color, fontsize=10, ha="center", va="center")
|
| 173 |
|
| 174 |
def sort_panels(rects):
|
| 175 |
before_rects = convert_to_list_of_lists(rects)
|
|
@@ -362,20 +378,19 @@ def get_text_to_panel_mapping(text_bboxes, sorted_panel_bboxes):
|
|
| 362 |
|
| 363 |
def sort_texts_within_panel(rects):
|
| 364 |
smallest_y = float("inf")
|
| 365 |
-
|
| 366 |
for i, rect in enumerate(rects):
|
| 367 |
x1, y1, x2, y2 = rect
|
| 368 |
smallest_y = min(smallest_y, y1)
|
| 369 |
-
|
| 370 |
|
| 371 |
-
reference_point = Point(
|
| 372 |
|
| 373 |
polygons_and_index = []
|
| 374 |
for i, rect in enumerate(rects):
|
| 375 |
x1, y1, x2, y2 = rect
|
| 376 |
polygons_and_index.append((box(x1,y1,x2,y2), i))
|
| 377 |
-
|
| 378 |
-
# sort points by distance to this new reference point
|
| 379 |
polygons_and_index = sorted(polygons_and_index, key=lambda x: reference_point.distance(x[0]))
|
| 380 |
indices = [x[1] for x in polygons_and_index]
|
| 381 |
return indices
|
|
|
|
| 90 |
return labels
|
| 91 |
|
| 92 |
def visualise_single_image_prediction(image_as_np_array, predictions, filename):
|
| 93 |
+
figure, subplot = plt.subplots(1, 1, figsize=(10, 10))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
subplot.imshow(image_as_np_array)
|
| 95 |
plot_bboxes(subplot, predictions["panels"], color="green")
|
| 96 |
+
plot_bboxes(subplot, predictions["texts"], color="red", visibility=predictions["is_essential_text"])
|
| 97 |
plot_bboxes(subplot, predictions["characters"], color="blue")
|
| 98 |
+
plot_bboxes(subplot, predictions["tails"], color="purple")
|
| 99 |
+
|
| 100 |
+
for i, name in enumerate(predictions["character_names"]):
|
| 101 |
+
char_bbox = predictions["characters"][i]
|
| 102 |
+
x1, y1, x2, y2 = char_bbox
|
| 103 |
+
subplot.text(x1, y1 - 2, name,
|
| 104 |
+
verticalalignment='bottom', horizontalalignment='left',
|
| 105 |
+
bbox=dict(facecolor='blue', alpha=1, edgecolor='none'), # Background settings
|
| 106 |
+
color='white', fontsize=8)
|
| 107 |
|
| 108 |
COLOURS = [
|
| 109 |
"#b7ff51", # green
|
|
|
|
| 146 |
subplot.plot([x2], [y2], color=random_colour, marker="o", markersize=5)
|
| 147 |
|
| 148 |
for (i, j) in predictions["text_character_associations"]:
|
|
|
|
| 149 |
bbox_i = predictions["texts"][i]
|
| 150 |
bbox_j = predictions["characters"][j]
|
| 151 |
+
if not predictions["is_essential_text"][i]:
|
| 152 |
+
continue
|
| 153 |
x1 = bbox_i[0] + (bbox_i[2] - bbox_i[0]) / 2
|
| 154 |
y1 = bbox_i[1] + (bbox_i[3] - bbox_i[1]) / 2
|
| 155 |
x2 = bbox_j[0] + (bbox_j[2] - bbox_j[0]) / 2
|
| 156 |
y2 = bbox_j[1] + (bbox_j[3] - bbox_j[1]) / 2
|
| 157 |
+
subplot.plot([x1, x2], [y1, y2], color="red", linewidth=2, linestyle="dashed")
|
| 158 |
+
|
| 159 |
+
for (i, j) in predictions["text_tail_associations"]:
|
| 160 |
+
bbox_i = predictions["texts"][i]
|
| 161 |
+
bbox_j = predictions["tails"][j]
|
| 162 |
+
x1 = bbox_i[0] + (bbox_i[2] - bbox_i[0]) / 2
|
| 163 |
+
y1 = bbox_i[1] + (bbox_i[3] - bbox_i[1]) / 2
|
| 164 |
+
x2 = bbox_j[0] + (bbox_j[2] - bbox_j[0]) / 2
|
| 165 |
+
y2 = bbox_j[1] + (bbox_j[3] - bbox_j[1]) / 2
|
| 166 |
+
subplot.plot([x1, x2], [y1, y2], color="purple", linewidth=2, linestyle="dashed")
|
| 167 |
|
| 168 |
subplot.axis("off")
|
| 169 |
if filename is not None:
|
|
|
|
| 174 |
plt.close()
|
| 175 |
return image
|
| 176 |
|
| 177 |
+
def plot_bboxes(subplot, bboxes, color="red", visibility=None):
|
| 178 |
+
if visibility is None:
|
| 179 |
+
visibility = [1] * len(bboxes)
|
| 180 |
for id, bbox in enumerate(bboxes):
|
| 181 |
+
if visibility[id] == 0:
|
| 182 |
+
continue
|
| 183 |
w = bbox[2] - bbox[0]
|
| 184 |
h = bbox[3] - bbox[1]
|
| 185 |
rect = patches.Rectangle(
|
| 186 |
bbox[:2], w, h, linewidth=1, edgecolor=color, facecolor="none", linestyle="solid"
|
| 187 |
)
|
| 188 |
subplot.add_patch(rect)
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
def sort_panels(rects):
|
| 191 |
before_rects = convert_to_list_of_lists(rects)
|
|
|
|
| 378 |
|
| 379 |
def sort_texts_within_panel(rects):
|
| 380 |
smallest_y = float("inf")
|
| 381 |
+
greatest_x = float("-inf")
|
| 382 |
for i, rect in enumerate(rects):
|
| 383 |
x1, y1, x2, y2 = rect
|
| 384 |
smallest_y = min(smallest_y, y1)
|
| 385 |
+
greatest_x = max(greatest_x, x2)
|
| 386 |
|
| 387 |
+
reference_point = Point(greatest_x, smallest_y)
|
| 388 |
|
| 389 |
polygons_and_index = []
|
| 390 |
for i, rect in enumerate(rects):
|
| 391 |
x1, y1, x2, y2 = rect
|
| 392 |
polygons_and_index.append((box(x1,y1,x2,y2), i))
|
| 393 |
+
# sort points by closest to reference point
|
|
|
|
| 394 |
polygons_and_index = sorted(polygons_and_index, key=lambda x: reference_point.distance(x[0]))
|
| 395 |
indices = [x[1] for x in polygons_and_index]
|
| 396 |
return indices
|