File size: 1,350 Bytes
832f690
 
 
 
94d675c
832f690
94d675c
 
 
 
 
 
f0aa330
832f690
 
94d675c
832f690
94d675c
f0aa330
832f690
 
f0aa330
94d675c
832f690
 
94d675c
 
 
 
 
 
 
9bed501
 
 
94d675c
 
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
35
36
37
38
import streamlit as st
from PIL import Image
import pytesseract
import io
import os

# Try to set Tesseract path if available
TESSERACT_PATH = "/usr/bin/tesseract"
if os.path.exists(TESSERACT_PATH):
    pytesseract.pytesseract.tesseract_cmd = TESSERACT_PATH
else:
    pytesseract.pytesseract.tesseract_cmd = "tesseract"  # fallback

st.set_page_config(page_title="OCR โ€“ Image to Text", layout="centered")
st.title("๐Ÿ–ผ๏ธ OCR โ€“ Image to Text")
st.write("Upload a PNG or JPG image to extract text.")

uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])

if uploaded_file:
    try:
        image = Image.open(io.BytesIO(uploaded_file.read()))
        st.image(image, caption="Preview", use_column_width=True)

        if st.button("Extract Text"):
            with st.spinner("Running OCR..."):
                try:
                    text = pytesseract.image_to_string(image)
                    st.subheader("Extracted Text")
                    st.text_area("", text, height=300)
                except pytesseract.TesseractNotFoundError:
                    st.error("โš ๏ธ Tesseract is not installed on the server. Please check your `apt.txt` or switch SDK to Docker.")
    except Exception as e:
        st.error(f"โŒ Failed to process image: {e}")
else:
    st.info("Please upload an image to begin.")