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 # 🌟πŸ”₯ Initialize global state for threading safety! AUTO_CAPTURE_RUNNING = False SELECTED_CAM_IMG = None # 🌟πŸ”₯ Initialize session state like a galactic DJ spinning tracks! if 'file_history' not in st.session_state: st.session_state['file_history'] = [] if 'cam0_file' not in st.session_state: st.session_state['cam0_file'] = None if 'cam1_file' not in st.session_state: st.session_state['cam1_file'] = None # πŸ“œπŸ’Ύ Save to history like a time-traveling scribe! | πŸ“…βœ¨ save_to_history("πŸ–ΌοΈ Image", "pic.jpg", img_data) - Stamps a pic in the history books like a boss! def save_to_history(file_type, file_path, img_data): timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open(file_path, "wb") as f: f.write(img_data) 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(): global AUTO_CAPTURE_RUNNING, SELECTED_CAM_IMG if AUTO_CAPTURE_RUNNING and SELECTED_CAM_IMG: filename = f"auto_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" with open(filename, "wb") as f: f.write(SELECTED_CAM_IMG) st.session_state['file_history'].append({ "Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "Type": "πŸ–ΌοΈ Image", "Path": filename }) threading.Timer(10, auto_capture).start() # πŸŽ›οΈ Sidebar config like a spaceship control panel! with st.sidebar: st.header("πŸŽšοΈπŸ“Έ Snap Shack") camera_choice = st.selectbox("πŸ“· Pick a Cam", ["Camera 0", "Camera 1"]) if st.button("⏰ Start Auto-Snap"): global AUTO_CAPTURE_RUNNING AUTO_CAPTURE_RUNNING = True auto_capture() if st.button("⏹️ Stop Auto-Snap"): global AUTO_CAPTURE_RUNNING 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("πŸ“Έ Streamlit Snap Craze") # πŸ“ΈπŸ“· Camera snap zone! st.header("πŸ“ΈπŸŽ₯ Snap Zone") cols = st.columns(2) with cols[0]: cam0_img = st.camera_input("πŸ“· Camera 0", key="cam0") if cam0_img and camera_choice == "Camera 0": filename = f"cam0_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" if st.session_state['cam0_file'] and os.path.exists(st.session_state['cam0_file']): os.remove(st.session_state['cam0_file']) save_to_history("πŸ–ΌοΈ Image", filename, cam0_img.getvalue()) st.session_state['cam0_file'] = filename global SELECTED_CAM_IMG SELECTED_CAM_IMG = cam0_img.getvalue() st.image(Image.open(BytesIO(cam0_img.getvalue())), caption="Camera 0 Snap", use_container_width=True) with cols[1]: cam1_img = st.camera_input("πŸ“· Camera 1", key="cam1") if cam1_img and camera_choice == "Camera 1": filename = f"cam1_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" if st.session_state['cam1_file'] and os.path.exists(st.session_state['cam1_file']): os.remove(st.session_state['cam1_file']) save_to_history("πŸ–ΌοΈ Image", filename, cam1_img.getvalue()) st.session_state['cam1_file'] = filename global SELECTED_CAM_IMG SELECTED_CAM_IMG = cam1_img.getvalue() st.image(Image.open(BytesIO(cam1_img.getvalue())), caption="Camera 1 Snap", use_container_width=True) # πŸ“‚ 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" save_to_history("πŸ–ΌοΈ Image", file_path, uploaded_file.getvalue()) st.image(Image.open(BytesIO(uploaded_file.getvalue())), 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'πŸ“₯ Snag It!', 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!")