omarelsayeed commited on
Commit
e996bd4
1 Parent(s): c1e6275

Update app.py

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