Update app.py
Browse files
app.py
CHANGED
|
@@ -9,33 +9,34 @@ ocr = PaddleOCR(use_angle_cls=True, lang="en")
|
|
| 9 |
|
| 10 |
def ocr_recognition(image):
|
| 11 |
try:
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if isinstance(image, np.ndarray):
|
| 14 |
image = Image.fromarray(image)
|
|
|
|
| 15 |
if not isinstance(image, Image.Image):
|
| 16 |
return "Invalid image format"
|
| 17 |
-
|
| 18 |
-
# Convert to numpy array
|
| 19 |
image_np = np.array(image)
|
| 20 |
-
|
| 21 |
-
# Perform OCR recognition
|
| 22 |
result = ocr.ocr(image_np, cls=True)
|
| 23 |
-
|
| 24 |
if not result or len(result) == 0 or result[0] is None:
|
| 25 |
return "No text recognized"
|
| 26 |
-
|
| 27 |
-
# Extract text
|
| 28 |
texts = [line[1][0] for line in result[0] if line]
|
| 29 |
-
|
| 30 |
if not texts:
|
| 31 |
return "No text recognized"
|
| 32 |
-
|
| 33 |
-
# Parse text to extract structured information
|
| 34 |
structured_info = parse_text_to_structure(texts)
|
| 35 |
return structured_info
|
| 36 |
-
|
| 37 |
except Exception as e:
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def parse_text_to_structure(texts):
|
| 41 |
# Initialize structured fields
|
|
|
|
| 9 |
|
| 10 |
def ocr_recognition(image):
|
| 11 |
try:
|
| 12 |
+
# Handle Gradio file upload from client (TempFileWrapper)
|
| 13 |
+
if hasattr(image, "read"): # This is a file-like object
|
| 14 |
+
image = Image.open(image)
|
| 15 |
+
|
| 16 |
+
# If it's still np.ndarray (browser upload), convert
|
| 17 |
if isinstance(image, np.ndarray):
|
| 18 |
image = Image.fromarray(image)
|
| 19 |
+
|
| 20 |
if not isinstance(image, Image.Image):
|
| 21 |
return "Invalid image format"
|
| 22 |
+
|
|
|
|
| 23 |
image_np = np.array(image)
|
|
|
|
|
|
|
| 24 |
result = ocr.ocr(image_np, cls=True)
|
| 25 |
+
|
| 26 |
if not result or len(result) == 0 or result[0] is None:
|
| 27 |
return "No text recognized"
|
| 28 |
+
|
|
|
|
| 29 |
texts = [line[1][0] for line in result[0] if line]
|
|
|
|
| 30 |
if not texts:
|
| 31 |
return "No text recognized"
|
| 32 |
+
|
|
|
|
| 33 |
structured_info = parse_text_to_structure(texts)
|
| 34 |
return structured_info
|
| 35 |
+
|
| 36 |
except Exception as e:
|
| 37 |
+
import traceback
|
| 38 |
+
return f"Error processing image:\n{traceback.format_exc()}"
|
| 39 |
+
|
| 40 |
|
| 41 |
def parse_text_to_structure(texts):
|
| 42 |
# Initialize structured fields
|