File size: 1,175 Bytes
19bf107
 
b65eb16
19bf107
1e9fcfe
 
19bf107
b65eb16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19bf107
1e9fcfe
 
 
 
b65eb16
1e9fcfe
 
19bf107
 
1e9fcfe
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import gradio as gr
from transformers import pipeline
from PIL import Image, ImageEnhance, ImageFilter

# Load the OCR model
ocr_model = pipeline("image-to-text", model="microsoft/trocr-large-printed")

def preprocess_image(image_path):
    # Open the image
    image = Image.open(image_path)
    # Convert to grayscale
    image = image.convert("L")
    # Enhance the contrast
    enhancer = ImageEnhance.Contrast(image)
    image = enhancer.enhance(2)  # Adjust contrast level as needed
    # Optionally apply a filter
    image = image.filter(ImageFilter.SHARPEN)
    return image

def recognize_text(image_path):
    # Preprocess the image
    preprocessed_image = preprocess_image(image_path)
    # Use the model on the preprocessed image
    result = ocr_model(preprocessed_image)
    return result[0]['generated_text']

# Set up the Gradio interface
interface = gr.Interface(
    fn=recognize_text,
    inputs=gr.Image(type="filepath"),  # Use filepath to accept image input
    outputs="text",
    title="OCR with Trocr",
    description="Upload an image to recognize text using the Trocr model."
)

# Launch the app
if __name__ == "__main__":
    interface.launch()