Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,157 +1,99 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image, ImageEnhance
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
import cv2
|
|
|
|
| 6 |
from fpdf import FPDF
|
| 7 |
-
import
|
| 8 |
-
import os
|
| 9 |
|
| 10 |
-
# --- Page
|
| 11 |
-
st.set_page_config(page_title="
|
| 12 |
|
| 13 |
-
# --- Background
|
| 14 |
-
page_bg_img =
|
| 15 |
<style>
|
| 16 |
-
|
| 17 |
-
background
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
background-
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
-
.
|
| 24 |
-
|
| 25 |
-
border-radius: 10px;
|
| 26 |
-
padding: 2rem;
|
| 27 |
}
|
| 28 |
</style>
|
| 29 |
-
|
| 30 |
st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 31 |
|
| 32 |
-
# ---
|
| 33 |
-
st.
|
| 34 |
-
st.write("
|
| 35 |
-
|
| 36 |
-
# --- Functions ---
|
| 37 |
-
|
| 38 |
-
def auto_crop_image(image):
|
| 39 |
-
# Convert to grayscale
|
| 40 |
-
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 41 |
-
blur = cv2.GaussianBlur(gray, (5,5), 0)
|
| 42 |
-
edged = cv2.Canny(blur, 50, 150)
|
| 43 |
-
|
| 44 |
-
# Find contours
|
| 45 |
-
contours, _ = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 46 |
-
if not contours:
|
| 47 |
-
return image
|
| 48 |
-
|
| 49 |
-
# Find the biggest contour
|
| 50 |
-
c = max(contours, key=cv2.contourArea)
|
| 51 |
-
x,y,w,h = cv2.boundingRect(c)
|
| 52 |
-
|
| 53 |
-
# Crop and return
|
| 54 |
-
cropped = image.crop((x,y,x+w,y+h))
|
| 55 |
-
return cropped
|
| 56 |
-
|
| 57 |
-
def enhance_image(image):
|
| 58 |
-
enhancer = ImageEnhance.Contrast(image)
|
| 59 |
-
image = enhancer.enhance(1.5)
|
| 60 |
-
|
| 61 |
-
enhancer = ImageEnhance.Sharpness(image)
|
| 62 |
-
image = enhancer.enhance(2.0)
|
| 63 |
-
|
| 64 |
-
return image
|
| 65 |
-
|
| 66 |
-
def rotate_image(image, angle):
|
| 67 |
-
return image.rotate(angle, expand=True)
|
| 68 |
-
|
| 69 |
-
def flip_image(image, mode):
|
| 70 |
-
if mode == "Horizontal":
|
| 71 |
-
return image.transpose(Image.FLIP_LEFT_RIGHT)
|
| 72 |
-
elif mode == "Vertical":
|
| 73 |
-
return image.transpose(Image.FLIP_TOP_BOTTOM)
|
| 74 |
-
return image
|
| 75 |
-
|
| 76 |
-
# --- Sidebar options ---
|
| 77 |
-
|
| 78 |
-
st.sidebar.header("⚙️ Settings")
|
| 79 |
-
crop_mode = st.sidebar.selectbox("Cropping Mode", ["Auto Crop", "Manual Crop"])
|
| 80 |
-
apply_enhance = st.sidebar.checkbox("Enhance Image (contrast, sharpness)", value=False)
|
| 81 |
-
rotate_angle = st.sidebar.selectbox("Rotate", [0, 90, 180, 270])
|
| 82 |
-
flip_option = st.sidebar.selectbox("Flip", ["None", "Horizontal", "Vertical"])
|
| 83 |
|
| 84 |
-
# --- Upload
|
| 85 |
-
|
| 86 |
-
uploaded_files = st.file_uploader("Upload images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
| 87 |
|
| 88 |
if uploaded_files:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
progress = st.progress(0)
|
| 93 |
-
|
| 94 |
-
for idx, uploaded_file in enumerate(uploaded_files):
|
| 95 |
image = Image.open(uploaded_file).convert("RGB")
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
image =
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
st.download_button(
|
| 152 |
-
label="📥 Download Scanned PDF",
|
| 153 |
-
data=f,
|
| 154 |
-
file_name="scanned_document.pdf",
|
| 155 |
-
mime="application/pdf"
|
| 156 |
-
)
|
| 157 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image, ImageEnhance
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import os
|
| 5 |
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
from fpdf import FPDF
|
| 8 |
+
from streamlit_cropper import st_cropper
|
|
|
|
| 9 |
|
| 10 |
+
# --- Set Page Config ---
|
| 11 |
+
st.set_page_config(page_title="Smart ID Scanner", page_icon="🪪", layout="centered")
|
| 12 |
|
| 13 |
+
# --- Add Background Style ---
|
| 14 |
+
page_bg_img = """
|
| 15 |
<style>
|
| 16 |
+
body {
|
| 17 |
+
background: linear-gradient(to right, #ece9e6, #ffffff);
|
| 18 |
+
}
|
| 19 |
+
.stButton>button {
|
| 20 |
+
color: white;
|
| 21 |
+
background-color: #4CAF50;
|
| 22 |
+
border: none;
|
| 23 |
+
padding: 0.5em 1em;
|
| 24 |
+
border-radius: 5px;
|
| 25 |
}
|
| 26 |
+
.stHeader, .stText, .stMarkdown {
|
| 27 |
+
color: #333333;
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
</style>
|
| 30 |
+
"""
|
| 31 |
st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 32 |
|
| 33 |
+
# --- App Heading ---
|
| 34 |
+
st.markdown("<h1 style='text-align: center; color: #2E86C1;'>📄 Smart ID & Document Scanner</h1>", unsafe_allow_html=True)
|
| 35 |
+
st.write("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# --- Upload Images ---
|
| 38 |
+
uploaded_files = st.file_uploader("Upload ID/Document Images 📷 (All formats supported)", type=['png', 'jpg', 'jpeg'], accept_multiple_files=True)
|
|
|
|
| 39 |
|
| 40 |
if uploaded_files:
|
| 41 |
+
cropped_images = []
|
| 42 |
+
for uploaded_file in uploaded_files:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
image = Image.open(uploaded_file).convert("RGB")
|
| 44 |
+
|
| 45 |
+
st.subheader(f"Crop & Adjust: {uploaded_file.name}")
|
| 46 |
+
|
| 47 |
+
# --- Free style manual crop ---
|
| 48 |
+
crop_box = st_cropper(image, realtime_update=True, box_color='#FF9900', aspect_ratio=None)
|
| 49 |
+
cropped_img = crop_box.copy()
|
| 50 |
+
|
| 51 |
+
# --- Before / After Preview ---
|
| 52 |
+
st.write("### Preview:")
|
| 53 |
+
col1, col2 = st.columns(2)
|
| 54 |
+
with col1:
|
| 55 |
+
st.image(image, caption="Original", use_container_width=True)
|
| 56 |
+
with col2:
|
| 57 |
+
st.image(cropped_img, caption="Cropped", use_container_width=True)
|
| 58 |
+
|
| 59 |
+
# --- Enhancement Option ---
|
| 60 |
+
if st.button(f"Enhance Image: {uploaded_file.name}"):
|
| 61 |
+
with st.spinner('Enhancing...'):
|
| 62 |
+
enhancer = ImageEnhance.Contrast(cropped_img)
|
| 63 |
+
cropped_img = enhancer.enhance(1.5)
|
| 64 |
+
sharpener = ImageEnhance.Sharpness(cropped_img)
|
| 65 |
+
cropped_img = sharpener.enhance(2.0)
|
| 66 |
+
st.success("Enhanced Successfully!")
|
| 67 |
+
st.image(cropped_img, caption="Enhanced", use_container_width=True)
|
| 68 |
+
|
| 69 |
+
cropped_images.append(cropped_img)
|
| 70 |
+
|
| 71 |
+
st.write("---")
|
| 72 |
+
|
| 73 |
+
# --- Download Options ---
|
| 74 |
+
col1, col2 = st.columns(2)
|
| 75 |
+
|
| 76 |
+
# Save as PDF
|
| 77 |
+
with col1:
|
| 78 |
+
if st.button("Generate PDF 📄"):
|
| 79 |
+
with st.spinner('Generating PDF...'):
|
| 80 |
+
pdf = FPDF()
|
| 81 |
+
for img in cropped_images:
|
| 82 |
+
temp_path = f"/tmp/{np.random.randint(0,10000)}.jpg"
|
| 83 |
+
img.save(temp_path, format="JPEG")
|
| 84 |
+
pdf.add_page()
|
| 85 |
+
pdf.image(temp_path, x=10, y=10, w=190)
|
| 86 |
+
os.remove(temp_path)
|
| 87 |
+
pdf_output = BytesIO()
|
| 88 |
+
pdf.output(pdf_output)
|
| 89 |
+
st.download_button(label="📥 Download PDF", data=pdf_output.getvalue(), file_name="scanned_document.pdf", mime="application/pdf")
|
| 90 |
+
|
| 91 |
+
# Save as Images
|
| 92 |
+
with col2:
|
| 93 |
+
if st.button("Save as JPG/PNG 📷"):
|
| 94 |
+
with st.spinner('Saving Images...'):
|
| 95 |
+
for idx, img in enumerate(cropped_images):
|
| 96 |
+
img_output = BytesIO()
|
| 97 |
+
img.save(img_output, format='PNG')
|
| 98 |
+
st.download_button(label=f"📥 Download Image {idx+1}", data=img_output.getvalue(), file_name=f"scanned_image_{idx+1}.png", mime="image/png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|