omarelsayeed
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -94,69 +94,87 @@ def detect_layout(img, conf_threshold, iou_threshold):
|
|
94 |
classes = [mapping[i] for i in classes]
|
95 |
return bboxes , classes
|
96 |
|
|
|
97 |
|
98 |
def draw_bboxes_on_image(image_path, bboxes, classes, reading_order):
|
99 |
-
# Define a color
|
100 |
class_colors = {
|
101 |
-
'CheckBox': '
|
102 |
-
'List': '
|
103 |
-
'P': '
|
104 |
-
'abandon': '
|
105 |
-
'figure': '
|
106 |
-
'gridless_table': '
|
107 |
-
'handwritten_signature': '
|
108 |
-
'qr_code': '
|
109 |
-
'table': '
|
110 |
-
'title': '
|
111 |
}
|
112 |
|
113 |
# Open the image using PIL
|
114 |
-
image = Image.open(image_path)
|
115 |
-
|
116 |
-
|
117 |
-
draw = ImageDraw.Draw(image)
|
118 |
|
119 |
-
# Try loading a
|
120 |
try:
|
121 |
font = ImageFont.truetype("arial.ttf", 20)
|
122 |
-
title_font = ImageFont.truetype("arial.ttf", 30)
|
123 |
except IOError:
|
124 |
-
font = ImageFont.load_default(
|
125 |
-
title_font = font
|
126 |
|
127 |
# Loop through the bounding boxes and corresponding labels
|
128 |
for i in range(len(bboxes)):
|
129 |
x1, y1, x2, y2 = bboxes[i]
|
130 |
class_name = classes[i]
|
131 |
order = reading_order[i]
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
# If it's a title, make the bounding box thicker and text larger
|
137 |
if class_name == 'title':
|
138 |
-
box_thickness =
|
139 |
-
label_font = title_font
|
140 |
else:
|
141 |
-
box_thickness =
|
142 |
-
label_font = font
|
143 |
-
|
144 |
-
# Draw
|
145 |
-
draw.
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
148 |
label = f"{class_name}-{order}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
-
#
|
151 |
-
|
152 |
-
label_width = bbox[2] - bbox[0]
|
153 |
-
label_height = bbox[3] - bbox[1]
|
154 |
|
155 |
-
# Draw the text
|
156 |
-
draw.text(
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
|
162 |
def scale_and_normalize_boxes(bboxes, old_width = 1024, old_height= 1024, new_width=640, new_height=640, normalize_width=1000, normalize_height=1000):
|
|
|
94 |
classes = [mapping[i] for i in classes]
|
95 |
return bboxes , classes
|
96 |
|
97 |
+
from PIL import Image, ImageDraw, ImageFont
|
98 |
|
99 |
def draw_bboxes_on_image(image_path, bboxes, classes, reading_order):
|
100 |
+
# Define a color palette for classes
|
101 |
class_colors = {
|
102 |
+
'CheckBox': '#FFA500', # Orange
|
103 |
+
'List': '#4682B4', # Steel Blue
|
104 |
+
'P': '#32CD32', # Lime Green
|
105 |
+
'abandon': '#8A2BE2', # Blue Violet
|
106 |
+
'figure': '#00CED1', # Dark Turquoise
|
107 |
+
'gridless_table': '#FFD700', # Gold
|
108 |
+
'handwritten_signature': '#FF69B4', # Hot Pink
|
109 |
+
'qr_code': '#FF4500', # Orange Red
|
110 |
+
'table': '#8B4513', # Saddle Brown
|
111 |
+
'title': '#FF1493' # Deep Pink
|
112 |
}
|
113 |
|
114 |
# Open the image using PIL
|
115 |
+
image = Image.open(image_path).convert("RGBA")
|
116 |
+
overlay = Image.new("RGBA", image.size, (255, 255, 255, 0)) # Transparent overlay
|
117 |
+
draw = ImageDraw.Draw(overlay)
|
|
|
118 |
|
119 |
+
# Try loading a modern font, or fall back to default
|
120 |
try:
|
121 |
font = ImageFont.truetype("arial.ttf", 20)
|
122 |
+
title_font = ImageFont.truetype("arial.ttf", 30)
|
123 |
except IOError:
|
124 |
+
font = ImageFont.load_default()
|
125 |
+
title_font = font
|
126 |
|
127 |
# Loop through the bounding boxes and corresponding labels
|
128 |
for i in range(len(bboxes)):
|
129 |
x1, y1, x2, y2 = bboxes[i]
|
130 |
class_name = classes[i]
|
131 |
order = reading_order[i]
|
132 |
+
color = class_colors.get(class_name, "#FFFFFF") # Default white if class is unknown
|
133 |
+
|
134 |
+
# Determine box and label styles
|
|
|
|
|
135 |
if class_name == 'title':
|
136 |
+
box_thickness = 6
|
137 |
+
label_font = title_font
|
138 |
else:
|
139 |
+
box_thickness = 3
|
140 |
+
label_font = font
|
141 |
+
|
142 |
+
# Draw a rounded rectangle for the bounding box
|
143 |
+
draw.rounded_rectangle(
|
144 |
+
[(x1, y1), (x2, y2)],
|
145 |
+
radius=10, # Rounded corners
|
146 |
+
outline=color,
|
147 |
+
width=box_thickness
|
148 |
+
)
|
149 |
+
|
150 |
+
# Create the label with the class and order
|
151 |
label = f"{class_name}-{order}"
|
152 |
+
text_width, text_height = draw.textsize(label, font=label_font)
|
153 |
+
|
154 |
+
# Add padding to the text background
|
155 |
+
padding = 5
|
156 |
+
label_bg_coords = [
|
157 |
+
x1, y1 - text_height - 2 * padding,
|
158 |
+
x1 + text_width + 2 * padding, y1
|
159 |
+
]
|
160 |
|
161 |
+
# Draw a semi-transparent rectangle for the label background
|
162 |
+
draw.rectangle(label_bg_coords, fill=(0, 0, 0, 128)) # Semi-transparent black
|
|
|
|
|
163 |
|
164 |
+
# Draw the label text
|
165 |
+
draw.text(
|
166 |
+
(x1 + padding, y1 - text_height - padding),
|
167 |
+
label,
|
168 |
+
fill="white",
|
169 |
+
font=label_font
|
170 |
+
)
|
171 |
+
|
172 |
+
# Merge the overlay with the original image
|
173 |
+
image_with_overlay = Image.alpha_composite(image, overlay)
|
174 |
+
|
175 |
+
# Convert back to RGB mode for saving/display
|
176 |
+
return image_with_overlay.convert("RGB")
|
177 |
+
|
178 |
|
179 |
|
180 |
def scale_and_normalize_boxes(bboxes, old_width = 1024, old_height= 1024, new_width=640, new_height=640, normalize_width=1000, normalize_height=1000):
|