Spaces:
Sleeping
Sleeping
Kabilash10
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
4 |
+
import requests
|
5 |
+
import io
|
6 |
+
|
7 |
+
# Load the OCR model and processor
|
8 |
+
model_name = "microsoft/trocr-base-stage1"
|
9 |
+
processor = TrOCRProcessor.from_pretrained(model_name)
|
10 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Streamlit app title
|
13 |
+
st.title("OCR with TrOCR")
|
14 |
+
|
15 |
+
# Upload image section
|
16 |
+
uploaded_image = st.file_uploader("Upload an image for OCR", type=["jpg", "jpeg", "png"])
|
17 |
+
|
18 |
+
if uploaded_image is not None:
|
19 |
+
# Open and display the uploaded image
|
20 |
+
image = Image.open(uploaded_image)
|
21 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
22 |
+
|
23 |
+
# Convert image to suitable format
|
24 |
+
inputs = processor(images=image, return_tensors="pt")
|
25 |
+
|
26 |
+
# Perform OCR
|
27 |
+
with torch.no_grad():
|
28 |
+
outputs = model.generate(**inputs)
|
29 |
+
|
30 |
+
# Decode the generated text
|
31 |
+
text = processor.decode(outputs[0], skip_special_tokens=True)
|
32 |
+
|
33 |
+
# Display the OCR result
|
34 |
+
st.write("Extracted Text:")
|
35 |
+
st.text(text)
|