Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 open the image
|
15 |
+
img_data = uploaded_file.read()
|
16 |
+
image = Image.open(io.BytesIO(img_data))
|
17 |
+
|
18 |
+
# Show preview
|
19 |
+
st.image(image, caption="Preview", use_column_width=True)
|
20 |
+
|
21 |
+
# Extraction button
|
22 |
+
if st.button("Extract Text"):
|
23 |
+
with st.spinner("Running OCR..."):
|
24 |
+
text = pytesseract.image_to_string(image)
|
25 |
+
st.subheader("Extracted Text")
|
26 |
+
st.text_area("", text, height=300)
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
st.error(f"Failed to process image: {e}")
|
30 |
+
else:
|
31 |
+
st.info("Please upload an image to get started.")
|