File size: 1,317 Bytes
9b26701
 
8bd9e0b
9b26701
 
8bd9e0b
 
 
9b26701
 
 
 
 
 
8bd9e0b
9b26701
8bd9e0b
 
 
 
 
9b26701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
import gradio as gr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image

# Load model and processor
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")

def predict_handwriting(image):
    """
    Function to process handwritten text image and return transcription
    """
    try:
        # Preprocess the image
        image = image.convert("RGB")
        # Prepare image pixel values
        pixel_values = processor(image, return_tensors="pt").pixel_values
        # Generate text
        generated_ids = model.generate(pixel_values)
        transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
        return transcription
    
    except Exception as e:
        return f"Error processing image: {str(e)}"

# Create Gradio interface
demo = gr.Interface(
    fn=predict_handwriting,
    inputs=gr.Image(type="pil", label="Upload Handwritten Text Image"),
    outputs=gr.Textbox(label="Transcription"),
    title="Handwritten Text to Text Converter",
    description="Upload a handwritten text image and get the transcribed text. Best results with clear, high-contrast images."
)

if __name__ == "__main__":
    demo.launch()