Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,17 +82,21 @@ class CustomJSONEncoder(json.JSONEncoder):
|
|
| 82 |
if isinstance(obj, Image.Image):
|
| 83 |
return "Image object (not serializable)"
|
| 84 |
if hasattr(obj, '__dict__'):
|
| 85 |
-
return obj.__dict__
|
| 86 |
-
return
|
| 87 |
|
| 88 |
def serialize_result(result):
|
| 89 |
return json.dumps(result, cls=CustomJSONEncoder, indent=2)
|
| 90 |
|
| 91 |
def draw_boxes(image, predictions, color=(255, 0, 0)):
|
| 92 |
draw = ImageDraw.Draw(image)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
draw.rectangle(bbox, outline=color, width=2)
|
| 97 |
return image
|
| 98 |
|
|
@@ -104,10 +108,10 @@ def ocr_workflow(image, langs):
|
|
| 104 |
predictions = run_ocr([image], [langs.split(',')], det_model, det_processor, rec_model, rec_processor)
|
| 105 |
|
| 106 |
# Draw bounding boxes on the image
|
| 107 |
-
image_with_boxes = draw_boxes(image.copy(), predictions[0]
|
| 108 |
|
| 109 |
# Format the OCR results
|
| 110 |
-
formatted_text = "\n".join([line
|
| 111 |
|
| 112 |
logger.info("Workflow OCR concluído com sucesso")
|
| 113 |
return serialize_result(predictions), image_with_boxes, formatted_text
|
|
@@ -123,7 +127,7 @@ def text_detection_workflow(image):
|
|
| 123 |
predictions = batch_text_detection([image], det_model, det_processor)
|
| 124 |
|
| 125 |
# Draw bounding boxes on the image
|
| 126 |
-
image_with_boxes = draw_boxes(image.copy(), predictions[0]
|
| 127 |
|
| 128 |
logger.info("Workflow de detecção de texto concluído com sucesso")
|
| 129 |
return serialize_result(predictions), image_with_boxes
|
|
@@ -141,7 +145,7 @@ def layout_analysis_workflow(image):
|
|
| 141 |
layout_predictions = batch_layout_detection([image], layout_model, layout_processor, line_predictions)
|
| 142 |
|
| 143 |
# Draw bounding boxes on the image
|
| 144 |
-
image_with_boxes = draw_boxes(image.copy(), layout_predictions[0]
|
| 145 |
|
| 146 |
logger.info("Workflow de análise de layout concluído com sucesso")
|
| 147 |
return serialize_result(layout_predictions), image_with_boxes
|
|
@@ -163,10 +167,10 @@ def reading_order_workflow(image):
|
|
| 163 |
|
| 164 |
# Draw bounding boxes on the image
|
| 165 |
image_with_boxes = image.copy()
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
draw.rectangle(bbox
|
| 169 |
-
draw.text((bbox
|
| 170 |
|
| 171 |
logger.info("Workflow de ordem de leitura concluído com sucesso")
|
| 172 |
return serialize_result(order_predictions), image_with_boxes
|
|
|
|
| 82 |
if isinstance(obj, Image.Image):
|
| 83 |
return "Image object (not serializable)"
|
| 84 |
if hasattr(obj, '__dict__'):
|
| 85 |
+
return {k: self.default(v) for k, v in obj.__dict__.items()}
|
| 86 |
+
return str(obj)
|
| 87 |
|
| 88 |
def serialize_result(result):
|
| 89 |
return json.dumps(result, cls=CustomJSONEncoder, indent=2)
|
| 90 |
|
| 91 |
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)
|
| 101 |
return image
|
| 102 |
|
|
|
|
| 108 |
predictions = run_ocr([image], [langs.split(',')], det_model, det_processor, rec_model, rec_processor)
|
| 109 |
|
| 110 |
# Draw bounding boxes on the image
|
| 111 |
+
image_with_boxes = draw_boxes(image.copy(), predictions[0].text_lines)
|
| 112 |
|
| 113 |
# Format the OCR results
|
| 114 |
+
formatted_text = "\n".join([line.text for line in predictions[0].text_lines])
|
| 115 |
|
| 116 |
logger.info("Workflow OCR concluído com sucesso")
|
| 117 |
return serialize_result(predictions), image_with_boxes, formatted_text
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 167 |
|
| 168 |
# Draw bounding boxes on the image
|
| 169 |
image_with_boxes = image.copy()
|
| 170 |
+
draw = ImageDraw.Draw(image_with_boxes)
|
| 171 |
+
for i, bbox in enumerate(order_predictions[0].bboxes):
|
| 172 |
+
draw.rectangle(bbox.bbox, outline=(0, 0, 255), width=2)
|
| 173 |
+
draw.text((bbox.bbox[0], bbox.bbox[1]), str(bbox.position), fill=(255, 0, 0))
|
| 174 |
|
| 175 |
logger.info("Workflow de ordem de leitura concluído com sucesso")
|
| 176 |
return serialize_result(order_predictions), image_with_boxes
|