Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,34 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
# Load the OCR model
|
5 |
ocr_model = pipeline("image-to-text", model="microsoft/trocr-large-printed")
|
6 |
|
7 |
-
def
|
8 |
-
#
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Set up the Gradio interface
|
13 |
interface = gr.Interface(
|
14 |
fn=recognize_text,
|
15 |
inputs=gr.Image(type="filepath"), # Use filepath to accept image input
|
16 |
-
outputs="text",
|
17 |
title="OCR with Trocr",
|
18 |
description="Upload an image to recognize text using the Trocr model."
|
19 |
)
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
4 |
|
5 |
# Load the OCR model
|
6 |
ocr_model = pipeline("image-to-text", model="microsoft/trocr-large-printed")
|
7 |
|
8 |
+
def preprocess_image(image_path):
|
9 |
+
# Open the image
|
10 |
+
image = Image.open(image_path)
|
11 |
+
# Convert to grayscale
|
12 |
+
image = image.convert("L")
|
13 |
+
# Enhance the contrast
|
14 |
+
enhancer = ImageEnhance.Contrast(image)
|
15 |
+
image = enhancer.enhance(2) # Adjust contrast level as needed
|
16 |
+
# Optionally apply a filter
|
17 |
+
image = image.filter(ImageFilter.SHARPEN)
|
18 |
+
return image
|
19 |
+
|
20 |
+
def recognize_text(image_path):
|
21 |
+
# Preprocess the image
|
22 |
+
preprocessed_image = preprocess_image(image_path)
|
23 |
+
# Use the model on the preprocessed image
|
24 |
+
result = ocr_model(preprocessed_image)
|
25 |
+
return result[0]['generated_text']
|
26 |
|
27 |
# Set up the Gradio interface
|
28 |
interface = gr.Interface(
|
29 |
fn=recognize_text,
|
30 |
inputs=gr.Image(type="filepath"), # Use filepath to accept image input
|
31 |
+
outputs="text",
|
32 |
title="OCR with Trocr",
|
33 |
description="Upload an image to recognize text using the Trocr model."
|
34 |
)
|