Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,30 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import pytesseract
|
|
|
4 |
import io
|
5 |
|
|
|
|
|
|
|
6 |
st.set_page_config(page_title="OCR – Image to Text", layout="centered")
|
7 |
st.title("🖼️ OCR – Image to Text")
|
8 |
-
|
9 |
st.write("Upload a PNG, JPG or JPEG image to extract text.")
|
10 |
|
11 |
uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
|
|
|
12 |
if uploaded_file:
|
13 |
try:
|
14 |
-
# Read and
|
15 |
-
|
16 |
-
image = Image.open(io.BytesIO(img_data))
|
17 |
-
|
18 |
-
# Show preview
|
19 |
st.image(image, caption="Preview", use_column_width=True)
|
20 |
|
21 |
-
#
|
22 |
if st.button("Extract Text"):
|
23 |
with st.spinner("Running OCR..."):
|
24 |
-
|
25 |
st.subheader("Extracted Text")
|
26 |
-
st.text_area("",
|
27 |
-
|
28 |
except Exception as e:
|
29 |
st.error(f"Failed to process image: {e}")
|
30 |
-
|
31 |
-
st.info("Please upload an image to get started.")
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import pytesseract
|
4 |
+
import os
|
5 |
import io
|
6 |
|
7 |
+
# Explicitly set tesseract path (only needed in hosted environments)
|
8 |
+
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
|
9 |
+
|
10 |
st.set_page_config(page_title="OCR – Image to Text", layout="centered")
|
11 |
st.title("🖼️ OCR – Image to Text")
|
|
|
12 |
st.write("Upload a PNG, JPG or JPEG image to extract text.")
|
13 |
|
14 |
uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
|
15 |
+
|
16 |
if uploaded_file:
|
17 |
try:
|
18 |
+
# Read and display image
|
19 |
+
image = Image.open(io.BytesIO(uploaded_file.read()))
|
|
|
|
|
|
|
20 |
st.image(image, caption="Preview", use_column_width=True)
|
21 |
|
22 |
+
# Extract text on button click
|
23 |
if st.button("Extract Text"):
|
24 |
with st.spinner("Running OCR..."):
|
25 |
+
extracted_text = pytesseract.image_to_string(image)
|
26 |
st.subheader("Extracted Text")
|
27 |
+
st.text_area(label="", value=extracted_text, height=300)
|
|
|
28 |
except Exception as e:
|
29 |
st.error(f"Failed to process image: {e}")
|
30 |
+
|
|