Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,13 @@ import gradio as gr
|
|
| 2 |
from kiri_ocr import OCR
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
# Initialize OCR
|
| 7 |
try:
|
| 8 |
print("Loading Kiri OCR model...")
|
| 9 |
-
|
|
|
|
| 10 |
print("Model loaded successfully")
|
| 11 |
except Exception as e:
|
| 12 |
print(f"Error loading model: {e}")
|
|
@@ -20,8 +22,11 @@ def process_image(image_path):
|
|
| 20 |
return None, "Please upload an image."
|
| 21 |
|
| 22 |
try:
|
|
|
|
| 23 |
# extract_text returns (text, results)
|
| 24 |
-
text, results = ocr.extract_text(image_path)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Open image for drawing
|
| 27 |
img = Image.open(image_path)
|
|
@@ -34,11 +39,15 @@ def process_image(image_path):
|
|
| 34 |
for item in results:
|
| 35 |
if 'box' in item:
|
| 36 |
x, y, w, h = item['box']
|
|
|
|
|
|
|
| 37 |
draw.rectangle([x, y, x + w, y + h], outline="red", width=3)
|
| 38 |
|
| 39 |
return np.array(img), text
|
| 40 |
|
| 41 |
except Exception as e:
|
|
|
|
|
|
|
| 42 |
return None, f"Error during extraction: {str(e)}"
|
| 43 |
|
| 44 |
# Build the interface
|
|
@@ -51,8 +60,8 @@ demo = gr.Interface(
|
|
| 51 |
],
|
| 52 |
title="Kiri OCR Demo",
|
| 53 |
description="Upload an image to extract English and Khmer text. Detected regions are highlighted in red.",
|
| 54 |
-
examples=[
|
| 55 |
)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
-
demo.launch()
|
|
|
|
| 2 |
from kiri_ocr import OCR
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import numpy as np
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# Initialize OCR
|
| 8 |
try:
|
| 9 |
print("Loading Kiri OCR model...")
|
| 10 |
+
# Use verbose=True to see what's happening
|
| 11 |
+
ocr = OCR(verbose=True)
|
| 12 |
print("Model loaded successfully")
|
| 13 |
except Exception as e:
|
| 14 |
print(f"Error loading model: {e}")
|
|
|
|
| 22 |
return None, "Please upload an image."
|
| 23 |
|
| 24 |
try:
|
| 25 |
+
print(f"Processing image: {image_path}")
|
| 26 |
# extract_text returns (text, results)
|
| 27 |
+
text, results = ocr.extract_text(image_path, verbose=True)
|
| 28 |
+
|
| 29 |
+
print(f"Extracted {len(results)} regions.")
|
| 30 |
|
| 31 |
# Open image for drawing
|
| 32 |
img = Image.open(image_path)
|
|
|
|
| 39 |
for item in results:
|
| 40 |
if 'box' in item:
|
| 41 |
x, y, w, h = item['box']
|
| 42 |
+
# Ensure coordinates are ints
|
| 43 |
+
x, y, w, h = int(x), int(y), int(w), int(h)
|
| 44 |
draw.rectangle([x, y, x + w, y + h], outline="red", width=3)
|
| 45 |
|
| 46 |
return np.array(img), text
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
+
import traceback
|
| 50 |
+
traceback.print_exc()
|
| 51 |
return None, f"Error during extraction: {str(e)}"
|
| 52 |
|
| 53 |
# Build the interface
|
|
|
|
| 60 |
],
|
| 61 |
title="Kiri OCR Demo",
|
| 62 |
description="Upload an image to extract English and Khmer text. Detected regions are highlighted in red.",
|
| 63 |
+
examples=[]
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|