rijilraj77's picture
Update app.py
cce7357 verified
Raw History Blame Contribute Delete
3.42 kB
import streamlit as st
import torch
import re
from PIL import Image, ImageOps
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
# -----------------------------
# CONFIG
# -----------------------------
st.set_page_config(page_title="VISIO Hybrid OCR", layout="wide")
TARGET_SIZE = (1024, 1024)
# -----------------------------
# LOAD MODELS
# -----------------------------
@st.cache_resource
def load_models():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
proc_hw = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten")
model_hw = VisionEncoderDecoderModel.from_pretrained(
"microsoft/trocr-large-handwritten"
).to(device).eval()
proc_pr = TrOCRProcessor.from_pretrained("microsoft/trocr-large-printed")
model_pr = VisionEncoderDecoderModel.from_pretrained(
"microsoft/trocr-large-printed"
).to(device).eval()
proc_cv = TrOCRProcessor.from_pretrained("rijilraj77/trocr-curved-finetuned")
model_cv = VisionEncoderDecoderModel.from_pretrained(
"rijilraj77/trocr-curved-finetuned"
).to(device).eval()
return {
"handwritten": (proc_hw, model_hw),
"printed": (proc_pr, model_pr),
"curved": (proc_cv, model_cv),
"device": device
}
# -----------------------------
# PREPROCESSING
# -----------------------------
def preprocess_light(img: Image.Image) -> Image.Image:
img = ImageOps.exif_transpose(img).convert("RGB")
return img
# -----------------------------
# OCR
# -----------------------------
def ocr_trocr(img: Image.Image, proc, model, device) -> str:
img = img.resize(TARGET_SIZE, Image.Resampling.LANCZOS)
px = proc(images=img, return_tensors="pt").pixel_values.to(device)
with torch.no_grad():
ids = model.generate(px, max_length=256)
return proc.batch_decode(ids, skip_special_tokens=True)[0]
# -----------------------------
# POST-PROCESSING
# -----------------------------
def clean_text(text: str) -> str:
text = re.sub(r"\s+", " ", text)
text = text.replace("|", "I")
return text.strip()
# -----------------------------
# UI
# -----------------------------
st.title("VISIO Hybrid OCR System")
st.caption("Printed, Handwritten & Curved Text Recognition using TrOCR")
models = load_models()
mode = st.radio("Select OCR Mode", ["Printed", "Handwritten", "Curved"])
uploaded = st.file_uploader("Upload image", type=["png", "jpg", "jpeg"])
if uploaded:
img = Image.open(uploaded)
st.image(img, width=700)
if st.button("Run OCR"):
with st.spinner("Running OCR…"):
img_prep = preprocess_light(img)
proc, model = models[mode.lower()]
device = models["device"]
raw = ocr_trocr(img_prep, proc, model, device)
st.session_state.raw_text = raw
st.session_state.clean_text = clean_text(raw)
st.success("OCR completed")
if "raw_text" in st.session_state:
st.subheader("Raw OCR Output")
st.text_area("Raw", st.session_state.raw_text, height=200)
st.subheader("Accurate Output (Post-processed)")
st.text_area("Cleaned", st.session_state.clean_text, height=200)
st.markdown("---")
st.markdown(
"**Notes:**\n"
"• Upload clear word-level images for best results\n"
"• Works for Printed, Handwritten and Curved text\n"
"• Crop tightly around text for higher accuracy\n"
)