NaqchoAli commited on
Commit
47ea833
·
verified ·
1 Parent(s): e577678

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py CHANGED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import requests
4
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
5
+ import torch
6
+
7
+ # Load pre-trained TrOCR model and processor
8
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
9
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
10
+
11
+ # Function to perform OCR and extract text
12
+ def extract_text_from_image(image):
13
+ # Preprocess the image
14
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
15
+
16
+ # Ensure the model is in evaluation mode
17
+ model.eval()
18
+
19
+ # Perform OCR
20
+ with torch.no_grad():
21
+ generated_ids = model.generate(pixel_values)
22
+
23
+ # Decode the generated IDs to text
24
+ text = processor.decode(generated_ids[0], skip_special_tokens=True)
25
+ return text
26
+
27
+ # Streamlit UI
28
+ st.title("OCR Text Extraction from Image")
29
+
30
+ st.write("""
31
+ Upload an image containing text, and this app will extract and display the text from the image using the powerful TrOCR model!
32
+ """)
33
+
34
+ # File uploader to upload the image
35
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
36
+
37
+ if uploaded_file is not None:
38
+ # Open the uploaded image
39
+ image = Image.open(uploaded_file)
40
+
41
+ # Display the uploaded image
42
+ st.image(image, caption="Uploaded Image", use_column_width=True)
43
+
44
+ # Button to extract text
45
+ if st.button("Extract Text"):
46
+ with st.spinner('Extracting text...'):
47
+ extracted_text = extract_text_from_image(image)
48
+ st.subheader("Extracted Text:")
49
+ st.write(extracted_text)
50
+