File size: 1,006 Bytes
d010bf6
6477a5b
0b73000
6477a5b
0b73000
6477a5b
 
 
 
0b73000
 
 
429d160
0b73000
 
 
 
 
 
 
 
c6111b8
0b73000
c6111b8
0b73000
 
9164d6d
0b73000
 
c6111b8
 
0b73000
2653a83
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
import gradio as gr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image

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

def ocr_recognition(image):
    # Open the image
    image = Image.open(image).convert("RGB")

    # Process the image and generate text
    pixel_values = processor(image, return_tensors="pt").pixel_values
    generated_ids = model.generate(pixel_values)
    
    # Decode the output text
    text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    
    return text

# Create Gradio interface
iface = gr.Interface(
    fn=ocr_recognition,
    inputs=gr.Image(type="pil"),  # Ensures PIL image input
    outputs="text",
    title="Handwritten OCR Extraction",
    description="Upload a handwritten image to extract text using TrOCR."
)

# Launch the Gradio app
iface.launch()