Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
from datetime import datetime | |
import os | |
import threading | |
import time | |
from PIL import Image | |
from io import BytesIO | |
import base64 | |
from imutils.video import VideoStream | |
import cv2 | |
# ππ₯ Initialize session state like a galactic DJ spinning tracks! | |
if 'file_history' not in st.session_state: | |
st.session_state['file_history'] = [] | |
if 'auto_capture_running' not in st.session_state: | |
st.session_state['auto_capture_running'] = False | |
# ππΎ Save to history like a time-traveling scribe! | |
def save_to_history(file_type, file_path, img_data): | |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
# Convert RGB to BGR for OpenCV saving | |
img_bgr = cv2.cvtColor(img_data, cv2.COLOR_RGB2BGR) | |
cv2.imwrite(file_path, img_bgr) | |
st.session_state['file_history'].append({ | |
"Timestamp": timestamp, | |
"Type": file_type, | |
"Path": file_path | |
}) | |
# πΈβ° Auto-capture every 10 secs like a sneaky shutterbug! | |
def auto_capture(streams): | |
while st.session_state['auto_capture_running']: | |
for i, stream in enumerate(streams): | |
frame = stream.read() | |
if frame is not None: | |
filename = f"cam{i}_auto_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" | |
save_to_history("πΌοΈ Image", filename, frame) | |
time.sleep(10) | |
# ποΈ Sidebar config like a spaceship control panel! | |
with st.sidebar: | |
st.header("ποΈπΈ Snap Shack") | |
streams = [ | |
VideoStream(src=0).start(), # Camera 0 | |
VideoStream(src=1).start() # Camera 1 | |
] | |
if st.button("β° Start Auto-Snap"): | |
st.session_state['auto_capture_running'] = True | |
threading.Thread(target=auto_capture, args=(streams,), daemon=True).start() | |
if st.button("βΉοΈ Stop Auto-Snap"): | |
st.session_state['auto_capture_running'] = False | |
# π Sidebar file outline with emoji flair! | |
st.subheader("π Snap Stash") | |
if st.session_state['file_history']: | |
images = [f for f in st.session_state['file_history'] if f['Type'] == "πΌοΈ Image"] | |
if images: | |
st.write("πΌοΈ Images") | |
for img in images: | |
st.write(f"- {img['Path']} @ {img['Timestamp']}") | |
else: | |
st.write("π³οΈ Empty Stash!") | |
# ππ¨ Main UI kicks off like a cosmic art show! | |
st.title("πΈ Imutils Snap Craze") | |
# πΈπ· Camera snap zone! | |
st.header("πΈπ₯ Snap Zone") | |
cols = st.columns(2) | |
placeholders = [cols[0].empty(), cols[1].empty()] | |
for i, (placeholder, stream) in enumerate(zip(placeholders, streams)): | |
frame = stream.read() | |
if frame is not None: | |
placeholder.image(frame, caption=f"Live Cam {i}", use_container_width=True) | |
else: | |
placeholder.error(f"π¨ No feed from Cam {i}. Check connection or index.") | |
if st.button(f"πΈ Snap Cam {i}", key=f"snap{i}"): | |
frame = stream.read() | |
if frame is not None: | |
filename = f"cam{i}_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" | |
save_to_history("πΌοΈ Image", filename, frame) | |
else: | |
st.error(f"π¨ Failed to snap Cam {i}") | |
# π Upload zone like a media drop party! | |
st.header("π₯π Drop Zone") | |
uploaded_files = st.file_uploader("πΈ Toss Pics", accept_multiple_files=True, type=['jpg', 'png']) | |
if uploaded_files: | |
for uploaded_file in uploaded_files: | |
file_path = f"uploaded_{uploaded_file.name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" | |
img = Image.open(uploaded_file) | |
img_array = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) # Convert for consistency | |
save_to_history("πΌοΈ Image", file_path, img_array) | |
st.image(img, caption=uploaded_file.name, use_container_width=True) | |
# πΌοΈ Gallery like a media circus! | |
st.header("πͺ Snap Show") | |
if st.session_state['file_history']: | |
images = [f for f in st.session_state['file_history'] if f['Type'] == "πΌοΈ Image"] | |
if images: | |
st.subheader("πΌοΈ Pic Parade") | |
cols = st.columns(3) | |
for i, img in enumerate(images): | |
with cols[i % 3]: | |
if os.path.exists(img['Path']): | |
st.image(img['Path'], caption=img['Path'], use_container_width=True) | |
with open(img['Path'], "rb") as f: | |
img_data = f.read() | |
st.markdown(f'<a href="data:image/jpeg;base64,{base64.b64encode(img_data).decode()}" download="{os.path.basename(img["Path"])}">π₯ Snag It!</a>', unsafe_allow_html=True) | |
else: | |
st.warning(f"π¨ Missing file: {img['Path']}") | |
else: | |
st.write("π« No snaps yet!") | |
# π History log like a time machine! | |
st.header("β³ Snap Saga") | |
if st.session_state['file_history']: | |
df = pd.DataFrame(st.session_state['file_history']) | |
st.dataframe(df) | |
else: | |
st.write("π³οΈ Nothing snapped yet!") | |
# Cleanup on app exit | |
def cleanup(): | |
for stream in streams: | |
stream.stop() | |
import atexit | |
atexit.register(cleanup) |