Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,28 +2,36 @@ import streamlit as st
|
|
2 |
from PIL import Image
|
3 |
import pytesseract
|
4 |
import io
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
st.set_page_config(page_title="OCR – Image to Text", layout="centered")
|
10 |
st.title("🖼️ OCR – Image to Text")
|
|
|
11 |
|
12 |
-
st.
|
13 |
|
14 |
-
uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
|
15 |
if uploaded_file:
|
16 |
try:
|
17 |
image = Image.open(io.BytesIO(uploaded_file.read()))
|
18 |
-
st.image(image, caption="
|
19 |
|
20 |
if st.button("Extract Text"):
|
21 |
-
with st.spinner("
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
except Exception as e:
|
27 |
st.error(f"❌ Failed to process image: {e}")
|
28 |
else:
|
29 |
-
st.info("Please upload an image.")
|
|
|
|
2 |
from PIL import Image
|
3 |
import pytesseract
|
4 |
import io
|
5 |
+
import os
|
6 |
|
7 |
+
# Try to set Tesseract path if available
|
8 |
+
TESSERACT_PATH = "/usr/bin/tesseract"
|
9 |
+
if os.path.exists(TESSERACT_PATH):
|
10 |
+
pytesseract.pytesseract.tesseract_cmd = TESSERACT_PATH
|
11 |
+
else:
|
12 |
+
pytesseract.pytesseract.tesseract_cmd = "tesseract" # fallback
|
13 |
|
14 |
st.set_page_config(page_title="OCR – Image to Text", layout="centered")
|
15 |
st.title("🖼️ OCR – Image to Text")
|
16 |
+
st.write("Upload a PNG or JPG image to extract text.")
|
17 |
|
18 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
|
19 |
|
|
|
20 |
if uploaded_file:
|
21 |
try:
|
22 |
image = Image.open(io.BytesIO(uploaded_file.read()))
|
23 |
+
st.image(image, caption="Preview", use_column_width=True)
|
24 |
|
25 |
if st.button("Extract Text"):
|
26 |
+
with st.spinner("Running OCR..."):
|
27 |
+
try:
|
28 |
+
text = pytesseract.image_to_string(image)
|
29 |
+
st.subheader("Extracted Text")
|
30 |
+
st.text_area("", text, height=300)
|
31 |
+
except pytesseract.TesseractNotFoundError:
|
32 |
+
st.error("⚠️ Tesseract is not installed on the server. Please check your `apt.txt` or switch SDK to Docker.")
|
33 |
except Exception as e:
|
34 |
st.error(f"❌ Failed to process image: {e}")
|
35 |
else:
|
36 |
+
st.info("Please upload an image to begin.")
|
37 |
+
|