artificialguybr commited on
Commit
a3e6b2a
·
verified ·
1 Parent(s): 2fee754

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -7
app.py CHANGED
@@ -92,9 +92,13 @@ def draw_boxes(image, predictions, color=(255, 0, 0)):
92
  draw = ImageDraw.Draw(image)
93
  if isinstance(predictions, list):
94
  for pred in predictions:
95
- bbox = pred.bbox if hasattr(pred, 'bbox') else pred.polygon if hasattr(pred, 'polygon') else None
96
- if bbox:
97
- draw.rectangle(bbox, outline=color, width=2)
 
 
 
 
98
  elif hasattr(predictions, 'bboxes'):
99
  for bbox in predictions.bboxes:
100
  draw.rectangle(bbox, outline=color, width=2)
@@ -127,10 +131,22 @@ def text_detection_workflow(image):
127
  predictions = batch_text_detection([image], det_model, det_processor)
128
 
129
  # Draw bounding boxes on the image
130
- image_with_boxes = draw_boxes(image.copy(), predictions[0])
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
  logger.info("Workflow de detecção de texto concluído com sucesso")
133
- return serialize_result(predictions), image_with_boxes
134
  except Exception as e:
135
  logger.error(f"Erro durante o workflow de detecção de texto: {e}")
136
  return serialize_result({"error": str(e)}), None
@@ -145,10 +161,26 @@ def layout_analysis_workflow(image):
145
  layout_predictions = batch_layout_detection([image], layout_model, layout_processor, line_predictions)
146
 
147
  # Draw bounding boxes on the image
148
- image_with_boxes = draw_boxes(image.copy(), layout_predictions[0], color=(0, 255, 0))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
  logger.info("Workflow de análise de layout concluído com sucesso")
151
- return serialize_result(layout_predictions), image_with_boxes
152
  except Exception as e:
153
  logger.error(f"Erro durante o workflow de análise de layout: {e}")
154
  return serialize_result({"error": str(e)}), None
 
92
  draw = ImageDraw.Draw(image)
93
  if isinstance(predictions, list):
94
  for pred in predictions:
95
+ if hasattr(pred, 'bboxes'):
96
+ for bbox in pred.bboxes:
97
+ draw.rectangle(bbox, outline=color, width=2)
98
+ elif hasattr(pred, 'bbox'):
99
+ draw.rectangle(pred.bbox, outline=color, width=2)
100
+ elif hasattr(pred, 'polygon'):
101
+ draw.polygon(pred.polygon, outline=color, width=2)
102
  elif hasattr(predictions, 'bboxes'):
103
  for bbox in predictions.bboxes:
104
  draw.rectangle(bbox, outline=color, width=2)
 
131
  predictions = batch_text_detection([image], det_model, det_processor)
132
 
133
  # Draw bounding boxes on the image
134
+ image_with_boxes = draw_boxes(image.copy(), predictions)
135
+
136
+ # Convert predictions to a serializable format
137
+ serializable_predictions = []
138
+ for pred in predictions:
139
+ serializable_pred = {
140
+ 'bboxes': [bbox.tolist() if hasattr(bbox, 'tolist') else bbox for bbox in pred.bboxes],
141
+ 'polygons': [poly.tolist() if hasattr(poly, 'tolist') else poly for poly in pred.polygons],
142
+ 'confidences': pred.confidences,
143
+ 'vertical_lines': [line.tolist() if hasattr(line, 'tolist') else line for line in pred.vertical_lines],
144
+ 'image_bbox': pred.image_bbox.tolist() if hasattr(pred.image_bbox, 'tolist') else pred.image_bbox
145
+ }
146
+ serializable_predictions.append(serializable_pred)
147
 
148
  logger.info("Workflow de detecção de texto concluído com sucesso")
149
+ return serialize_result(serializable_predictions), image_with_boxes
150
  except Exception as e:
151
  logger.error(f"Erro durante o workflow de detecção de texto: {e}")
152
  return serialize_result({"error": str(e)}), None
 
161
  layout_predictions = batch_layout_detection([image], layout_model, layout_processor, line_predictions)
162
 
163
  # Draw bounding boxes on the image
164
+ image_with_boxes = draw_boxes(image.copy(), layout_predictions, color=(0, 255, 0))
165
+
166
+ # Convert predictions to a serializable format
167
+ serializable_predictions = []
168
+ for pred in layout_predictions:
169
+ serializable_pred = {
170
+ 'bboxes': [
171
+ {
172
+ 'bbox': bbox.bbox.tolist() if hasattr(bbox.bbox, 'tolist') else bbox.bbox,
173
+ 'polygon': bbox.polygon.tolist() if hasattr(bbox.polygon, 'tolist') else bbox.polygon,
174
+ 'confidence': bbox.confidence,
175
+ 'label': bbox.label
176
+ } for bbox in pred.bboxes
177
+ ],
178
+ 'image_bbox': pred.image_bbox.tolist() if hasattr(pred.image_bbox, 'tolist') else pred.image_bbox
179
+ }
180
+ serializable_predictions.append(serializable_pred)
181
 
182
  logger.info("Workflow de análise de layout concluído com sucesso")
183
+ return serialize_result(serializable_predictions), image_with_boxes
184
  except Exception as e:
185
  logger.error(f"Erro durante o workflow de análise de layout: {e}")
186
  return serialize_result({"error": str(e)}), None