Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,30 +37,39 @@ def process_image(image_path, langs):
|
|
| 37 |
logger.info(f"Processing image: {image_path}")
|
| 38 |
image = Image.open(image_path)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
logger.info("Performing OCR...")
|
| 42 |
-
ocr_predictions = run_ocr([image], [langs.split(',')], det_model, det_processor, rec_model, rec_processor)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
logger.info("Processing complete.")
|
| 66 |
return json.dumps(results, indent=2)
|
|
@@ -73,7 +82,7 @@ def surya_ui(image, langs):
|
|
| 73 |
result = process_image(image, langs)
|
| 74 |
return result
|
| 75 |
except Exception as e:
|
| 76 |
-
logger.error(f"Error processing
|
| 77 |
return f"An error occurred: {str(e)}"
|
| 78 |
|
| 79 |
# Create Gradio interface
|
|
|
|
| 37 |
logger.info(f"Processing image: {image_path}")
|
| 38 |
image = Image.open(image_path)
|
| 39 |
|
| 40 |
+
results = {}
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
try:
|
| 43 |
+
# OCR
|
| 44 |
+
logger.info("Performing OCR...")
|
| 45 |
+
ocr_predictions = run_ocr([image], [langs.split(',')], det_model, det_processor, rec_model, rec_processor)
|
| 46 |
+
results["ocr"] = ocr_predictions[0]
|
| 47 |
+
|
| 48 |
+
# Text line detection
|
| 49 |
+
logger.info("Detecting text lines...")
|
| 50 |
+
line_predictions = batch_text_detection([image], det_model, det_processor)
|
| 51 |
+
results["text_lines"] = line_predictions[0]
|
| 52 |
+
|
| 53 |
+
# Layout analysis
|
| 54 |
+
logger.info("Analyzing layout...")
|
| 55 |
+
layout_predictions = batch_layout_detection([image], layout_model, layout_processor, line_predictions)
|
| 56 |
+
results["layout"] = layout_predictions[0]
|
| 57 |
+
|
| 58 |
+
# Reading order
|
| 59 |
+
logger.info("Determining reading order...")
|
| 60 |
+
logger.debug(f"Layout predictions: {layout_predictions}")
|
| 61 |
+
|
| 62 |
+
if isinstance(layout_predictions[0], dict) and 'bboxes' in layout_predictions[0]:
|
| 63 |
+
bboxes = [bbox['bbox'] for bbox in layout_predictions[0]['bboxes']]
|
| 64 |
+
order_predictions = batch_ordering([image], [bboxes], order_model, order_processor)
|
| 65 |
+
results["reading_order"] = order_predictions[0]
|
| 66 |
+
else:
|
| 67 |
+
logger.warning("Layout predictions do not have the expected structure. Skipping reading order detection.")
|
| 68 |
+
results["reading_order"] = "Reading order detection skipped due to unexpected layout prediction structure."
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
logger.error(f"Error processing image: {str(e)}", exc_info=True)
|
| 72 |
+
results["error"] = str(e)
|
| 73 |
|
| 74 |
logger.info("Processing complete.")
|
| 75 |
return json.dumps(results, indent=2)
|
|
|
|
| 82 |
result = process_image(image, langs)
|
| 83 |
return result
|
| 84 |
except Exception as e:
|
| 85 |
+
logger.error(f"Error in UI processing: {str(e)}", exc_info=True)
|
| 86 |
return f"An error occurred: {str(e)}"
|
| 87 |
|
| 88 |
# Create Gradio interface
|