|
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 |
|
|
|
|
|
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 |
|
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 |
|
if 'selected_cam_img' not in st.session_state: |
|
st.session_state['selected_cam_img'] = None |
|
|
|
|
|
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 |
|
}) |
|
|
|
|
|
def auto_capture(): |
|
if st.session_state['auto_capture_running'] and st.session_state['selected_cam_img']: |
|
cam_img = st.session_state['selected_cam_img'] |
|
filename = f"auto_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" |
|
save_to_history("πΌοΈ Image", filename, cam_img.getvalue()) |
|
threading.Timer(10, auto_capture).start() |
|
|
|
|
|
with st.sidebar: |
|
st.header("ποΈπΈ Snap Shack") |
|
camera_choice = st.selectbox("π· Pick a Cam", ["Camera 0", "Camera 1"]) |
|
if st.button("β° Start Auto-Snap"): |
|
st.session_state['auto_capture_running'] = True |
|
auto_capture() |
|
if st.button("βΉοΈ Stop Auto-Snap"): |
|
st.session_state['auto_capture_running'] = False |
|
|
|
|
|
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!") |
|
|
|
|
|
st.title("πΈ Streamlit Snap Craze") |
|
|
|
|
|
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 |
|
st.session_state['selected_cam_img'] = cam0_img |
|
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 |
|
st.session_state['selected_cam_img'] = cam1_img |
|
st.image(Image.open(BytesIO(cam1_img.getvalue())), caption="Camera 1 Snap", use_container_width=True) |
|
|
|
|
|
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}" |
|
save_to_history("πΌοΈ Image", file_path, uploaded_file.getvalue()) |
|
|
|
|
|
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!") |
|
|
|
|
|
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!") |