File size: 1,037 Bytes
f0a1f2e
 
 
 
addd52c
f0a1f2e
addd52c
f0a1f2e
 
 
 
 
addd52c
f0a1f2e
addd52c
f0a1f2e
 
 
 
 
addd52c
f0a1f2e
addd52c
f0a1f2e
 
 
addd52c
f0a1f2e
addd52c
f0a1f2e
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
import streamlit as st
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import torch

st.title("Handwriting Detection AI")

@st.cache_resource
def load_model():
    processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-handwritten')
    model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-handwritten')
    return processor, model

processor, model = load_model()

def predict_text(image):
    pixel_values = processor(images=image, return_tensors="pt").pixel_values
    generated_ids = model.generate(pixel_values)
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return generated_text

uploaded_file = st.file_uploader("Upload a Handwritten Image", type=["png", "jpg", "jpeg"])

if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption='Uploaded Handwritten Image', use_column_width=True)

    generated_text = predict_text(image)

    st.write(f"Recognized Text: {generated_text}")