Spaces:
Running
on
Zero
Running
on
Zero
File size: 7,727 Bytes
9012453 |
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 |
"""
Bounding box clustering module for grouping nearby text elements.
Copied from the original with minimal modifications for HuggingFace Spaces.
"""
from math import sqrt, dist
import extcolors
from py_files.pycolor import find
class Node:
def __init__(self, data=None, x1=None, y1=None, x2=None, y2=None,
font_size=None, children=None):
if children is None:
children = []
self.data = data
self.top_left_x = min(int(x1), int(x2)) if x1 is not None and x2 is not None else None
self.top_left_y = min(int(y1), int(y2)) if y1 is not None and y2 is not None else None
self.bottom_right_x = max(int(x1), int(x2)) if x1 is not None and x2 is not None else None
self.bottom_right_y = max(int(y1), int(y2)) if y1 is not None and y2 is not None else None
self.font_size = abs(self.bottom_right_y - self.top_left_y) if not font_size else font_size
self.children = children
def add_child(self, child):
child_top_left_x = child.top_left_x
child_top_left_y = child.top_left_y
child_bottom_right_x = child.bottom_right_x
child_bottom_right_y = child.bottom_right_y
self.top_left_x = min(self.top_left_x, child_top_left_x)
self.top_left_y = min(self.top_left_y, child_top_left_y)
self.bottom_right_x = max(self.bottom_right_x, child_bottom_right_x)
self.bottom_right_y = max(self.bottom_right_y, child_bottom_right_y)
self.data += " " + child.data
self.children.append(child)
self.font_size = (max(self.font_size, 0) + max(child.font_size, 0)) // 2
def get_data(self):
return self.data
def __repr__(self):
return f'Node({self.data}, size: {self.font_size}, {self.top_left_x}, {self.top_left_y}, {self.bottom_right_x}, {self.bottom_right_y}, {self.children})'
class ImageNode(Node):
def __init__(self, category=None, top_left_x=None, top_left_y=None, bottom_right_x=None, bottom_right_y=None,
text=""):
super().__init__(data=None, top_left_x=top_left_x, top_left_y=top_left_y, bottom_right_x=bottom_right_x,
bottom_right_y=bottom_right_y)
self.category = category
self.text = text
def get_boundaries(self):
return self.top_left_x, self.top_left_y, self.bottom_right_x, self.bottom_right_y
def get_text(self):
return self.text
def set_text(self, text):
self.text = text
def get_category(self):
return self.category
def __repr__(self):
return f'ImageNode({self.text}, {self.top_left_x}, {self.top_left_y}, {self.bottom_right_x}, {self.bottom_right_y})'
def distance_between_parallel_lines(pt1, pt2, pt3, pt4):
x1, y1 = pt1
x2, y2 = pt2
x3, y3 = pt3
numerator = abs((y2 - y1) * x3 - (x2 - x1) * y3 + x2 * y1 - y2 * x1)
denominator = sqrt((y2 - y1) ** 2 + (x2 - x1) ** 2)
if denominator == 0:
return None
return numerator / denominator
def create_node_lines(node):
top_left = (node.top_left_x, node.top_left_y)
top_right = (node.bottom_right_x, node.top_left_y)
bottom_left = (node.top_left_x, node.bottom_right_y)
bottom_right = (node.bottom_right_x, node.bottom_right_y)
return {
"left_line": [top_left, bottom_left],
"right_line": [top_right, bottom_right],
"top_line": [top_left, top_right],
"bottom_line": [bottom_left, bottom_right],
}
def rect_distance(x1, y1, x1b, y1b, x2, y2, x2b, y2b):
left = x2b < x1
right = x1b < x2
bottom = y2b < y1
top = y1b < y2
if top and left:
return dist((x1, y1b), (x2b, y2))
elif left and bottom:
return dist((x1, y1), (x2b, y2b))
elif bottom and right:
return dist((x1b, y1), (x2, y2b))
elif right and top:
return dist((x1b, y1b), (x2, y2))
elif left:
return x1 - x2b
elif right:
return x2 - x1b
elif bottom:
return y1 - y2b
elif top:
return y2 - y1b
else: # rectangles intersect
return 0
def distance_between_nodes(node1, node2):
return rect_distance(node1.top_left_x, node1.top_left_y, node1.bottom_right_x, node1.bottom_right_y,
node2.top_left_x, node2.top_left_y, node2.bottom_right_x, node2.bottom_right_y)
class QuadTree:
def __init__(self, root=None, max_dist=5):
self.root = root if isinstance(root, Node) else Node(None, 0, 0, 0, 0)
self.max_dist = max_dist
def insert(self, node: Node, parent=None):
if parent is None:
parent = self.root
if len(parent.children) == 0:
parent.children.append(node)
return
min_dist = float('inf')
min_child = None
for child in parent.children:
_dist = distance_between_nodes(node, child)
if _dist < min_dist:
min_dist = _dist
min_child = child
if min_dist <= self.max_dist:
min_child.add_child(node)
return True
else:
parent.children.append(node)
return False
def get_root(self):
return self.root
def get_children(self, data=False):
boxes = []
datas = []
for child in self.root.children:
boxes.append([child.top_left_x, child.top_left_y, child.bottom_right_x, child.bottom_right_y])
datas.append(child.data)
if data:
return datas, boxes
return boxes
def get_children_nodes(self):
return self.root.children
def get_dataframe(self, image):
import pandas as pd
def custom_sort(row):
x, y = row
return (y, x)
data = []
for child in self.root.children:
x1, y1, x2, y2 = child.top_left_x, child.top_left_y, child.bottom_right_x + 3, child.bottom_right_y + 3
try:
img_crop = image.crop((x1, y1, x2, y2))
colors, pixel_count = extcolors.extract_from_image(img_crop)
# Get background and font colors
bg_color = colors[0][0] if colors else (255, 255, 255)
font_color = colors[-1][0] if len(colors) > 1 else (0, 0, 0)
data.append([
child.data,
(child.top_left_x, child.top_left_y),
(child.bottom_right_x, child.bottom_right_y),
child.font_size,
f"{find(bg_color)}, (RGB: {bg_color[0]}, {bg_color[1]}, {bg_color[2]})",
f"{find(font_color)}, (RGB: {font_color[0]}, {font_color[1]}, {font_color[2]})"
])
except Exception as e:
# Fallback for color extraction errors
data.append([
child.data,
(child.top_left_x, child.top_left_y),
(child.bottom_right_x, child.bottom_right_y),
child.font_size,
"white, (RGB: 255, 255, 255)",
"black, (RGB: 0, 0, 0)"
])
df = pd.DataFrame(data, columns=["Text", "Top Co-ordinates", "Bottom Co-ordinates", "Font Size",
"Background Color", "Font Color"])
df = df.sort_values(by="Top Co-ordinates", key=lambda x: x.apply(custom_sort))
return df
def __repr__(self):
repr = ""
for c in self.root.children:
repr += f'Node({c.data}, size: {c.font_size}, {c.top_left_x}, {c.top_left_y}, {c.bottom_right_x}, {c.bottom_right_y})\n'
return repr
|