enpaiva commited on
Commit
fc642fe
Β·
verified Β·
1 Parent(s): 2061888

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -10
app.py CHANGED
@@ -139,7 +139,7 @@ def load_model(model_name):
139
  print(f"Error loading model: {e}")
140
  return f"❌ Error loading {model_name}: {str(e)}"
141
 
142
- def visualize_bbox(image_input, bboxes, classes, scores, id_to_names, alpha=0.3):
143
  """Visualize bounding boxes with OpenCV."""
144
  if isinstance(image_input, Image.Image):
145
  image = np.array(image_input)
@@ -176,7 +176,6 @@ def visualize_bbox(image_input, bboxes, classes, scores, id_to_names, alpha=0.3)
176
  class_id = int(class_id)
177
  class_name = id_to_names.get(class_id, f"unknown_{class_id}")
178
 
179
- text = f"{class_name}: {score:.3f}"
180
  color = tuple(int(c) for c in cmap[class_id % len(cmap)])
181
 
182
  # Draw filled rectangle on overlay
@@ -184,11 +183,13 @@ def visualize_bbox(image_input, bboxes, classes, scores, id_to_names, alpha=0.3)
184
  # Draw border on main image
185
  cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 3)
186
 
187
- # Add text label
188
- (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)
189
- cv2.rectangle(image, (x_min, y_min - text_height - baseline - 4),
190
- (x_min + text_width + 8, y_min), color, -1)
191
- cv2.putText(image, text, (x_min + 4, y_min - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
 
 
192
 
193
  except Exception as e:
194
  print(f"Skipping box {i} due to error: {e}")
@@ -198,7 +199,7 @@ def visualize_bbox(image_input, bboxes, classes, scores, id_to_names, alpha=0.3)
198
 
199
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
200
 
201
- def process_image(input_img, conf_threshold, iou_threshold, nms_method, alpha):
202
  """Process image with document layout detection."""
203
  if input_img is None:
204
  return None, "❌ Please upload an image first."
@@ -252,8 +253,10 @@ def process_image(input_img, conf_threshold, iou_threshold, nms_method, alpha):
252
  labels = labels[keep_indices]
253
 
254
  # Visualize results
255
- output = visualize_bbox(input_img, boxes, labels, scores, classes_map, alpha=alpha)
256
- info = f"βœ… Found {len(boxes)} detections | NMS: {nms_method} | Threshold: {conf_threshold:.2f}"
 
 
257
 
258
  return output, info
259
 
@@ -325,6 +328,13 @@ if __name__ == "__main__":
325
  .status-good { color: #28a745; font-weight: bold; }
326
  .status-error { color: #dc3545; font-weight: bold; }
327
  .status-info { color: #17a2b8; font-weight: bold; }
 
 
 
 
 
 
 
328
  """
329
 
330
  # Create Gradio interface
@@ -420,6 +430,17 @@ if __name__ == "__main__":
420
  label="Overlay Transparency",
421
  info="Transparency of detection overlays"
422
  )
 
 
 
 
 
 
 
 
 
 
 
423
 
424
  # RIGHT COLUMN - Results and Output
425
  with gr.Column(scale=1, elem_classes=["panel-right"]):
 
139
  print(f"Error loading model: {e}")
140
  return f"❌ Error loading {model_name}: {str(e)}"
141
 
142
+ def visualize_bbox(image_input, bboxes, classes, scores, id_to_names, alpha=0.3, show_labels=True):
143
  """Visualize bounding boxes with OpenCV."""
144
  if isinstance(image_input, Image.Image):
145
  image = np.array(image_input)
 
176
  class_id = int(class_id)
177
  class_name = id_to_names.get(class_id, f"unknown_{class_id}")
178
 
 
179
  color = tuple(int(c) for c in cmap[class_id % len(cmap)])
180
 
181
  # Draw filled rectangle on overlay
 
183
  # Draw border on main image
184
  cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 3)
185
 
186
+ # Add text label only if show_labels is True
187
+ if show_labels:
188
+ text = f"{class_name}: {score:.3f}"
189
+ (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)
190
+ cv2.rectangle(image, (x_min, y_min - text_height - baseline - 4),
191
+ (x_min + text_width + 8, y_min), color, -1)
192
+ cv2.putText(image, text, (x_min + 4, y_min - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
193
 
194
  except Exception as e:
195
  print(f"Skipping box {i} due to error: {e}")
 
199
 
200
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
201
 
202
+ def process_image(input_img, conf_threshold, iou_threshold, nms_method, alpha, show_labels):
203
  """Process image with document layout detection."""
204
  if input_img is None:
205
  return None, "❌ Please upload an image first."
 
253
  labels = labels[keep_indices]
254
 
255
  # Visualize results
256
+ output = visualize_bbox(input_img, boxes, labels, scores, classes_map, alpha=alpha, show_labels=show_labels)
257
+
258
+ labels_status = "with labels" if show_labels else "without labels"
259
+ info = f"βœ… Found {len(boxes)} detections ({labels_status}) | NMS: {nms_method} | Threshold: {conf_threshold:.2f}"
260
 
261
  return output, info
262
 
 
328
  .status-good { color: #28a745; font-weight: bold; }
329
  .status-error { color: #dc3545; font-weight: bold; }
330
  .status-info { color: #17a2b8; font-weight: bold; }
331
+
332
+ .toggle-labels {
333
+ background: linear-gradient(45deg, #667eea, #764ba2) !important;
334
+ border: none !important;
335
+ color: white !important;
336
+ font-weight: bold !important;
337
+ }
338
  """
339
 
340
  # Create Gradio interface
 
430
  label="Overlay Transparency",
431
  info="Transparency of detection overlays"
432
  )
433
+
434
+ # Visualization Options Section
435
+ with gr.Group(elem_classes=["control-section"]):
436
+ gr.HTML("<h3>🎨 Visualization Options</h3>")
437
+
438
+ show_labels_checkbox = gr.Checkbox(
439
+ value=True,
440
+ label="Show Class Labels",
441
+ info="Display class names and confidence scores on detections",
442
+ interactive=True
443
+ )
444
 
445
  # RIGHT COLUMN - Results and Output
446
  with gr.Column(scale=1, elem_classes=["panel-right"]):