Ankur Goyal
commited on
Commit
•
fcfd908
1
Parent(s):
27d0a44
Improve bounding box for highlight
Browse files
app.py
CHANGED
@@ -49,25 +49,24 @@ def lift_word_boxes(document):
|
|
49 |
return document.context["image"][0][1]
|
50 |
|
51 |
|
52 |
-
def expand_bbox(word_boxes
|
53 |
if len(word_boxes) == 0:
|
54 |
return None
|
55 |
|
56 |
min_x, min_y, max_x, max_y = zip(*[x[1] for x in word_boxes])
|
57 |
min_x, min_y, max_x, max_y = [min(min_x), min(min_y), max(max_x), max(max_y)]
|
58 |
-
if padding != 0:
|
59 |
-
padding = max((max_x - min_x) * padding, (max_y - min_y) * padding)
|
60 |
-
min_x = max(0, min_x - padding)
|
61 |
-
min_y = max(0, min_y - padding)
|
62 |
-
max_x = max_x + padding
|
63 |
-
max_y = max_y + padding
|
64 |
return [min_x, min_y, max_x, max_y]
|
65 |
|
66 |
|
67 |
# LayoutLM boxes are normalized to 0, 1000
|
68 |
-
def normalize_bbox(box, width, height):
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
|
73 |
examples = [
|
|
|
49 |
return document.context["image"][0][1]
|
50 |
|
51 |
|
52 |
+
def expand_bbox(word_boxes):
|
53 |
if len(word_boxes) == 0:
|
54 |
return None
|
55 |
|
56 |
min_x, min_y, max_x, max_y = zip(*[x[1] for x in word_boxes])
|
57 |
min_x, min_y, max_x, max_y = [min(min_x), min(min_y), max(max_x), max(max_y)]
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
return [min_x, min_y, max_x, max_y]
|
59 |
|
60 |
|
61 |
# LayoutLM boxes are normalized to 0, 1000
|
62 |
+
def normalize_bbox(box, width, height, padding=0.005):
|
63 |
+
min_x, min_y, max_x, max_y = [c / 1000 for c in box]
|
64 |
+
if padding != 0:
|
65 |
+
min_x = max(0, min_x - padding)
|
66 |
+
min_y = max(0, min_y - padding)
|
67 |
+
max_x = min(max_x + padding, 1)
|
68 |
+
max_y = min(max_y + padding, 1)
|
69 |
+
return [min_x * width, min_y * height, max_x * width, max_y * height]
|
70 |
|
71 |
|
72 |
examples = [
|