omarelsayeed commited on
Commit
6329b7d
·
verified ·
1 Parent(s): 14db543

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -42
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 map for each class name
100
  class_colors = {
101
- 'CheckBox': 'orange',
102
- 'List': 'blue',
103
- 'P': 'green',
104
- 'abandon': 'purple',
105
- 'figure': 'cyan',
106
- 'gridless_table': 'yellow',
107
- 'handwritten_signature': 'magenta',
108
- 'qr_code': 'red',
109
- 'table': 'brown',
110
- 'title': 'pink'
111
  }
112
 
113
  # Open the image using PIL
114
- image = Image.open(image_path)
115
-
116
- # Prepare to draw on the image
117
- draw = ImageDraw.Draw(image)
118
 
119
- # Try loading a default font, if it fails, use a basic font
120
  try:
121
  font = ImageFont.truetype("arial.ttf", 20)
122
- title_font = ImageFont.truetype("arial.ttf", 30) # Larger font for titles
123
  except IOError:
124
- font = ImageFont.load_default(size = 30)
125
- title_font = font # Use the same font for title if custom font fails
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
- # Get the color for the class
134
- color = class_colors[class_name]
135
-
136
- # If it's a title, make the bounding box thicker and text larger
137
  if class_name == 'title':
138
- box_thickness = 4 # Thicker box for title
139
- label_font = title_font # Larger font for title
140
  else:
141
- box_thickness = 2 # Default box thickness
142
- label_font = font # Default font for other classes
143
-
144
- # Draw the rectangle with the class color and box thickness
145
- draw.rectangle([x1, y1, x2, y2], outline=color, width=box_thickness)
146
-
147
- # Label the box with the class and order
 
 
 
 
 
148
  label = f"{class_name}-{order}"
 
 
 
 
 
 
 
 
149
 
150
- # Calculate text size using textbbox() to get the bounding box of the text
151
- bbox = draw.textbbox((x1, y1 - 20), label, font=label_font)
152
- label_width = bbox[2] - bbox[0]
153
- label_height = bbox[3] - bbox[1]
154
 
155
- # Draw the text above the box
156
- draw.text((x1, y1 - label_height), label, fill="black", font=label_font)
157
-
158
- # Return the modified image as a PIL image object
159
- return image
 
 
 
 
 
 
 
 
 
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):