Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import gradio as gr
|
2 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
@@ -15,13 +15,13 @@ model2 = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwri
|
|
15 |
# Function to extract text from handwritten images
|
16 |
def extract_text(image):
|
17 |
try:
|
18 |
-
#
|
19 |
if isinstance(image, np.ndarray):
|
20 |
if len(image.shape) == 2: # Grayscale (H, W) -> Convert to RGB
|
21 |
image = np.stack([image] * 3, axis=-1)
|
22 |
image = Image.fromarray(image)
|
23 |
-
|
24 |
-
image = Image.open(image).convert("RGB")
|
25 |
|
26 |
# Maintain aspect ratio while resizing (better for OCR)
|
27 |
image.thumbnail((800, 800))
|
@@ -45,7 +45,7 @@ def extract_text(image):
|
|
45 |
# Gradio UI for OCR Extraction
|
46 |
iface = gr.Interface(
|
47 |
fn=extract_text,
|
48 |
-
inputs=gr.Image(type="pil"),
|
49 |
outputs="text",
|
50 |
title="Handwritten OCR Extraction",
|
51 |
description="Upload a handwritten image to extract text using AI OCR.",
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
|
|
15 |
# Function to extract text from handwritten images
|
16 |
def extract_text(image):
|
17 |
try:
|
18 |
+
# Ensure input is a PIL Image
|
19 |
if isinstance(image, np.ndarray):
|
20 |
if len(image.shape) == 2: # Grayscale (H, W) -> Convert to RGB
|
21 |
image = np.stack([image] * 3, axis=-1)
|
22 |
image = Image.fromarray(image)
|
23 |
+
elif isinstance(image, str): # If file path is given, open the image
|
24 |
+
image = Image.open(image).convert("RGB")
|
25 |
|
26 |
# Maintain aspect ratio while resizing (better for OCR)
|
27 |
image.thumbnail((800, 800))
|
|
|
45 |
# Gradio UI for OCR Extraction
|
46 |
iface = gr.Interface(
|
47 |
fn=extract_text,
|
48 |
+
inputs=gr.Image(type="pil"), # Ensures input is a PIL image
|
49 |
outputs="text",
|
50 |
title="Handwritten OCR Extraction",
|
51 |
description="Upload a handwritten image to extract text using AI OCR.",
|