Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,37 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image, ImageEnhance
|
| 3 |
from io import BytesIO
|
| 4 |
-
import os
|
| 5 |
import numpy as np
|
| 6 |
import zipfile
|
|
|
|
| 7 |
from fpdf import FPDF
|
| 8 |
from streamlit_cropper import st_cropper
|
| 9 |
|
| 10 |
# --- PAGE CONFIG ---
|
| 11 |
st.set_page_config(page_title="Smart Doc Scanner", page_icon="🖼️", layout="centered")
|
| 12 |
|
| 13 |
-
# ---
|
| 14 |
st.title("📄 Smart ID & Document Scanner")
|
| 15 |
st.write("---")
|
| 16 |
|
| 17 |
-
# ---
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
# --- FILE UPLOAD ---
|
| 21 |
uploaded_files = st.file_uploader("Upload your Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 22 |
|
| 23 |
if uploaded_files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
st.write("---")
|
| 25 |
processed_images = []
|
| 26 |
|
|
@@ -31,21 +42,26 @@ if uploaded_files:
|
|
| 31 |
st.image(img, caption=uploaded_file.name, use_container_width=True)
|
| 32 |
|
| 33 |
else: # Advanced Mode
|
| 34 |
-
st.info("🔧 Edit each image manually: crop, rotate, enhance, reset.")
|
| 35 |
for idx, uploaded_file in enumerate(uploaded_files):
|
|
|
|
|
|
|
| 36 |
original_img = Image.open(uploaded_file).convert("RGB")
|
| 37 |
if f"edited_{idx}" not in st.session_state:
|
| 38 |
st.session_state[f"edited_{idx}"] = original_img
|
|
|
|
| 39 |
|
| 40 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
with col_left:
|
| 45 |
-
st.write("### Select Crop Area:")
|
| 46 |
crop_output = st_cropper(
|
| 47 |
-
st.session_state[f"
|
| 48 |
-
realtime_update=False,
|
| 49 |
box_color='#FF9900',
|
| 50 |
aspect_ratio=None
|
| 51 |
)
|
|
@@ -54,25 +70,26 @@ if uploaded_files:
|
|
| 54 |
crop_output = Image.fromarray(crop_output)
|
| 55 |
|
| 56 |
if st.button(f"Crop Now {idx}", key=f"crop_{idx}"):
|
| 57 |
-
st.session_state[f"
|
| 58 |
|
| 59 |
-
if st.button(f"Enhance {idx}", key=f"enhance_{idx}"):
|
| 60 |
-
enhancer = ImageEnhance.Contrast(st.session_state[f"
|
| 61 |
sharpener = ImageEnhance.Sharpness(enhancer.enhance(1.5))
|
| 62 |
-
st.session_state[f"
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
angle = st.slider(f"Select Rotate Angle for {uploaded_file.name}", -180, 180, 0, key=f"rotate_slider_{idx}")
|
| 65 |
if st.button(f"Rotate Now {idx}", key=f"rotate_{idx}"):
|
| 66 |
-
st.session_state[f"
|
| 67 |
|
| 68 |
-
if st.button(f"Reset {idx}", key=f"
|
| 69 |
-
st.session_state[f"
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
processed_images.append(st.session_state
|
| 76 |
|
| 77 |
st.write("---")
|
| 78 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image, ImageEnhance
|
| 3 |
from io import BytesIO
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import zipfile
|
| 6 |
+
import os
|
| 7 |
from fpdf import FPDF
|
| 8 |
from streamlit_cropper import st_cropper
|
| 9 |
|
| 10 |
# --- PAGE CONFIG ---
|
| 11 |
st.set_page_config(page_title="Smart Doc Scanner", page_icon="🖼️", layout="centered")
|
| 12 |
|
| 13 |
+
# --- HEADER ---
|
| 14 |
st.title("📄 Smart ID & Document Scanner")
|
| 15 |
st.write("---")
|
| 16 |
|
| 17 |
+
# --- Clear Session State on New Upload ---
|
| 18 |
+
if "uploaded_files_hash" not in st.session_state:
|
| 19 |
+
st.session_state.uploaded_files_hash = None
|
| 20 |
|
|
|
|
| 21 |
uploaded_files = st.file_uploader("Upload your Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 22 |
|
| 23 |
if uploaded_files:
|
| 24 |
+
# Create a hash based on filenames
|
| 25 |
+
uploaded_files_names = [file.name for file in uploaded_files]
|
| 26 |
+
current_hash = hash(tuple(uploaded_files_names))
|
| 27 |
+
|
| 28 |
+
if st.session_state.uploaded_files_hash != current_hash:
|
| 29 |
+
# New files uploaded, clear session
|
| 30 |
+
st.session_state.clear()
|
| 31 |
+
st.session_state.uploaded_files_hash = current_hash
|
| 32 |
+
|
| 33 |
+
app_mode = st.radio("Select Mode:", ["Normal Mode (No Editing)", "Advanced Mode (Edit Each Image)"])
|
| 34 |
+
|
| 35 |
st.write("---")
|
| 36 |
processed_images = []
|
| 37 |
|
|
|
|
| 42 |
st.image(img, caption=uploaded_file.name, use_container_width=True)
|
| 43 |
|
| 44 |
else: # Advanced Mode
|
|
|
|
| 45 |
for idx, uploaded_file in enumerate(uploaded_files):
|
| 46 |
+
st.subheader(f"Editing: {uploaded_file.name}")
|
| 47 |
+
|
| 48 |
original_img = Image.open(uploaded_file).convert("RGB")
|
| 49 |
if f"edited_{idx}" not in st.session_state:
|
| 50 |
st.session_state[f"edited_{idx}"] = original_img
|
| 51 |
+
st.session_state[f"preview_{idx}"] = original_img.copy()
|
| 52 |
|
| 53 |
+
col1, col2 = st.columns(2)
|
| 54 |
+
|
| 55 |
+
with col1:
|
| 56 |
+
st.write("### Preview:")
|
| 57 |
+
st.image(st.session_state[f"preview_{idx}"], caption=f"Preview of {uploaded_file.name}", use_container_width=True)
|
| 58 |
|
| 59 |
+
with col2:
|
| 60 |
+
st.write("### Editing Options:")
|
| 61 |
|
|
|
|
|
|
|
| 62 |
crop_output = st_cropper(
|
| 63 |
+
st.session_state[f"preview_{idx}"],
|
| 64 |
+
realtime_update=False,
|
| 65 |
box_color='#FF9900',
|
| 66 |
aspect_ratio=None
|
| 67 |
)
|
|
|
|
| 70 |
crop_output = Image.fromarray(crop_output)
|
| 71 |
|
| 72 |
if st.button(f"Crop Now {idx}", key=f"crop_{idx}"):
|
| 73 |
+
st.session_state[f"preview_{idx}"] = crop_output
|
| 74 |
|
| 75 |
+
if st.button(f"Enhance Now {idx}", key=f"enhance_{idx}"):
|
| 76 |
+
enhancer = ImageEnhance.Contrast(st.session_state[f"preview_{idx}"])
|
| 77 |
sharpener = ImageEnhance.Sharpness(enhancer.enhance(1.5))
|
| 78 |
+
st.session_state[f"preview_{idx}"] = sharpener.enhance(2.0)
|
| 79 |
+
|
| 80 |
+
angle = st.slider(f"Rotate Angle {uploaded_file.name}", -180, 180, 0, key=f"rotate_slider_{idx}")
|
| 81 |
|
|
|
|
| 82 |
if st.button(f"Rotate Now {idx}", key=f"rotate_{idx}"):
|
| 83 |
+
st.session_state[f"preview_{idx}"] = st.session_state[f"preview_{idx}"].rotate(angle, expand=True)
|
| 84 |
|
| 85 |
+
if st.button(f"Reset Preview {idx}", key=f"reset_preview_{idx}"):
|
| 86 |
+
st.session_state[f"preview_{idx}"] = original_img.copy()
|
| 87 |
|
| 88 |
+
if st.button(f"Apply Changes {idx}", key=f"apply_{idx}"):
|
| 89 |
+
st.session_state[f"edited_{idx}"] = st.session_state[f"preview_{idx}"].copy()
|
| 90 |
+
st.success("✅ Changes Applied!")
|
| 91 |
|
| 92 |
+
processed_images.append(st.session_state.get(f"edited_{idx}", original_img))
|
| 93 |
|
| 94 |
st.write("---")
|
| 95 |
|