File size: 5,020 Bytes
62030e0 81e7fbf 62030e0 81e7fbf 62030e0 |
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 |
"""utils.py: Helper Functions to keep this Repo Standalone"""
# System Imports
from math import sin, cos, radians, sqrt
__author__ = "Johannes Bayer"
__copyright__ = "Copyright 2023, DFKI"
__license__ = "CC"
__version__ = "0.0.1"
__email__ = "[email protected]"
__status__ = "Prototype"
def shift(p, q):
"""Shifts a Point by another point"""
return [p[0]+q[0], p[1]+q[1]]
def rotate(p, angle):
"""Rotates a Point by an Angle"""
return [p[0] * cos(angle) - p[1] * sin(angle),
p[0] * sin(angle) + p[1] * cos(angle)]
def scale(p, scale_x, scale_y):
"""Scales a Point in two Dimensions"""
return [p[0]*scale_x, p[1]*scale_y]
def transform(port, bb):
"""Transforms a Point from Unit Space (classes ports) to Global Bounding Box (image)"""
p = shift(port['position'], (-.5, -0.5)) # Normalize: [0.0, 1.0]^2 -> [-0.5, 0-5]^2
p = scale(p, 1.0, -1.0) # Flip
p = rotate(p, -radians(bb['rotation']))
p = scale(p, bb["xmax"] - bb["xmin"], bb["ymax"] - bb["ymin"])
p = shift(p, [(bb["xmin"]+bb["xmax"])/2, (bb["ymin"]+bb["ymax"])/2])
return {"name": port['name'], "position": p}
def bbdist(bb1, bb2):
"""Calculates the Distance between two Bounding Box Annotations"""
return sqrt(((bb1["xmin"]+bb1["xmax"])/2 - (bb2["xmin"]+bb2["xmax"])/2)**2 +
((bb1["ymin"]+bb1["ymax"])/2 - (bb2["ymin"]+bb2["ymax"])/2)**2)
def overlap(bbox1, bbox2):
if bbox1["xmin"] > bbox2["xmax"] or bbox1["xmax"] < bbox2["xmin"]:
return False
if bbox1["ymin"] > bbox2["ymax"] or bbox1["ymax"] < bbox2["ymin"]:
return False
return True
def associated_keypoints(instances, shape):
"""Returns the points with same group id as the provided polygon"""
return [point for point in instances["points"]
if point["group"] == shape["group"] and point["class"] == "connector"]
def IoU(bb1, bb2):
"""Intersection over Union"""
intersection = 1
union = 1
return intersection/union
if __name__ == "__main__":
import sys
from loader import read_pascal_voc, write_pascal_voc
import numpy as np
import random
if len(sys.argv) == 3:
source = sys.argv[1]
target = sys.argv[2]
ann1, ann2 = [[bbox for bbox in read_pascal_voc(path)['bboxes'] if bbox['class'] == "text"]
for path in [source, target]]
if not len(ann1) == len(ann2):
print(f"Warning: Unequal Text Count ({len(ann1)} vs. {len(ann2)}), cropping..")
consensus = min(len(ann1), len(ann2))
ann1 = ann1[:consensus]
ann2 = ann2[:consensus]
x1 = np.array([(bbox['xmin']+bbox['xmax'])/2 for bbox in ann1])
y1 = np.array([(bbox['ymin']+bbox['ymax'])/2 for bbox in ann1])
x2 = np.array([(bbox['xmin']+bbox['xmax'])/2 for bbox in ann2])
y2 = np.array([(bbox['ymin']+bbox['ymax'])/2 for bbox in ann2])
x1 = ((x1-np.min(x1))/(np.max(x1)-np.min(x1))) * (np.max(x2)-np.min(x2)) + np.min(x2)
y1 = ((y1-np.min(y1))/(np.max(y1)-np.min(y1))) * (np.max(y2)-np.min(y2)) + np.min(y2)
dist = np.sqrt((x1-x2[np.newaxis].T)**2 + (y1-y2[np.newaxis].T)**2)
indices_1 = np.arange(len(ann1))
indices_2 = np.arange(len(ann2))
print(np.sum(np.diagonal(dist)))
for i in range(10000):
if random.random() > 0.5:
max_dist_pos = np.argmax(np.diagonal(dist)) # Mitigate Largest Cost
else:
max_dist_pos = random.randint(0, len(ann1)-1)
if np.min(dist[max_dist_pos, :]) < np.min(dist[:, max_dist_pos]):
min_dist_pos = np.argmin(dist[max_dist_pos, :])
dist[:, [max_dist_pos, min_dist_pos]] = dist[:, [min_dist_pos, max_dist_pos]] # Swap Columns
indices_1[[max_dist_pos, min_dist_pos]] = indices_1[[min_dist_pos, max_dist_pos]]
else:
min_dist_pos = np.argmin(dist[:, max_dist_pos])
dist[[max_dist_pos, min_dist_pos], :] = dist[[min_dist_pos, max_dist_pos], :] # Swap Rows
indices_2[[max_dist_pos, min_dist_pos]] = indices_2[[min_dist_pos, max_dist_pos]]
print(np.sum(np.diagonal(dist)))
wb = read_pascal_voc(target)
for i in range(len(ann1)):
ann2[indices_2[i]]['text'] = ann1[indices_1[i]]['text']
bbox_match = [bbox for bbox in wb['bboxes']
if bbox['xmin'] == ann2[indices_2[i]]['xmin'] and
bbox['xmax'] == ann2[indices_2[i]]['xmax'] and
bbox['ymin'] == ann2[indices_2[i]]['ymin'] and
bbox['ymax'] == ann2[indices_2[i]]['ymax']]
if len(bbox_match) == 1:
bbox_match[0]['text'] = ann1[indices_1[i]]['text']
bbox_match[0]['rotation'] = ann1[indices_1[i]]['rotation']
write_pascal_voc(wb)
else:
print("Args: source target")
|