Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from hezar.models import Model
|
| 2 |
from hezar.utils import load_image, draw_boxes
|
| 3 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
|
@@ -12,13 +14,13 @@ trocr_model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-ha
|
|
| 12 |
|
| 13 |
def recognize_handwritten_text(image):
|
| 14 |
try:
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
image
|
| 18 |
-
|
| 19 |
|
| 20 |
-
# Load image with hezar utils
|
| 21 |
-
processed_image = load_image(
|
| 22 |
|
| 23 |
# Detect text regions with CRAFT
|
| 24 |
outputs = craft_model.predict(processed_image)
|
|
@@ -47,7 +49,11 @@ def recognize_handwritten_text(image):
|
|
| 47 |
return result_pil, f"Recognized text: {text_data}"
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
-
return Image.fromarray(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Create Gradio interface
|
| 53 |
interface = gr.Interface(
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
from hezar.models import Model
|
| 4 |
from hezar.utils import load_image, draw_boxes
|
| 5 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
|
|
|
| 14 |
|
| 15 |
def recognize_handwritten_text(image):
|
| 16 |
try:
|
| 17 |
+
# Save the uploaded image to a temporary file
|
| 18 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
| 19 |
+
image.save(tmp_file.name, format="JPEG")
|
| 20 |
+
tmp_path = tmp_file.name
|
| 21 |
|
| 22 |
+
# Load image with hezar utils using file path
|
| 23 |
+
processed_image = load_image(tmp_path)
|
| 24 |
|
| 25 |
# Detect text regions with CRAFT
|
| 26 |
outputs = craft_model.predict(processed_image)
|
|
|
|
| 49 |
return result_pil, f"Recognized text: {text_data}"
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
+
return Image.fromarray(np.array(image)), f"Error: {str(e)}"
|
| 53 |
+
finally:
|
| 54 |
+
# Clean up temporary file
|
| 55 |
+
if 'tmp_path' in locals():
|
| 56 |
+
os.unlink(tmp_path)
|
| 57 |
|
| 58 |
# Create Gradio interface
|
| 59 |
interface = gr.Interface(
|