Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,38 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import pytesseract
|
|
|
4 |
|
5 |
-
st.set_page_config(page_title="OCR
|
6 |
-
st.title("🖼️ OCR
|
7 |
|
8 |
-
st.write("Upload an image to extract text
|
9 |
|
10 |
-
# Upload block
|
11 |
uploaded_file = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
|
|
|
12 |
|
13 |
-
# Show uploaded image and extraction options
|
14 |
if uploaded_file is not None:
|
|
|
|
|
|
|
|
|
|
|
15 |
try:
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
text = pytesseract.image_to_string(image)
|
22 |
-
|
23 |
-
|
|
|
|
|
24 |
|
25 |
-
except Exception as e:
|
26 |
-
st.error(f"❌ Error: {e}")
|
|
|
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 an image (PNG, JPG, JPEG) to extract text.")
|
10 |
|
|
|
11 |
uploaded_file = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
|
12 |
+
image = None
|
13 |
|
|
|
14 |
if uploaded_file is not None:
|
15 |
+
# Display file metadata
|
16 |
+
st.write(f"**Filename:** {uploaded_file.name}")
|
17 |
+
st.write(f"**Type:** {uploaded_file.type}")
|
18 |
+
st.write(f"**Size:** {uploaded_file.size} bytes")
|
19 |
+
|
20 |
try:
|
21 |
+
# Read bytes and open with PIL
|
22 |
+
bytes_data = uploaded_file.getvalue()
|
23 |
+
image = Image.open(io.BytesIO(bytes_data))
|
24 |
+
st.image(image, caption="Preview", use_column_width=True)
|
25 |
+
except Exception as e:
|
26 |
+
st.error(f"⚠️ Failed to open image: {e}")
|
27 |
|
28 |
+
# Only show extract button if image was loaded successfully
|
29 |
+
if image is not None:
|
30 |
+
if st.button("Extract Text"):
|
31 |
+
with st.spinner("Running OCR..."):
|
32 |
+
try:
|
33 |
text = pytesseract.image_to_string(image)
|
34 |
+
st.success("✅ Text extracted!")
|
35 |
+
st.text_area("Extracted Text", value=text, height=300)
|
36 |
+
except Exception as e:
|
37 |
+
st.error(f"❌ OCR error: {e}")
|
38 |
|
|
|
|