Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import threading
|
|
| 6 |
import time
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
|
|
|
| 9 |
|
| 10 |
# ππ₯ Initialize session state like a galactic DJ spinning tracks!
|
| 11 |
if 'file_history' not in st.session_state:
|
|
@@ -20,8 +21,10 @@ if 'selected_cam_img' not in st.session_state:
|
|
| 20 |
st.session_state['selected_cam_img'] = None
|
| 21 |
|
| 22 |
# ππΎ Save to history like a time-traveling scribe! | π
β¨ save_to_history("πΌοΈ Image", "pic.jpg") - Stamps a pic in the history books like a boss!
|
| 23 |
-
def save_to_history(file_type, file_path):
|
| 24 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
|
|
| 25 |
st.session_state['file_history'].append({
|
| 26 |
"Timestamp": timestamp,
|
| 27 |
"Type": file_type,
|
|
@@ -33,9 +36,7 @@ def auto_capture():
|
|
| 33 |
if st.session_state['auto_capture_running'] and st.session_state['selected_cam_img']:
|
| 34 |
cam_img = st.session_state['selected_cam_img']
|
| 35 |
filename = f"auto_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 36 |
-
|
| 37 |
-
f.write(cam_img.getvalue())
|
| 38 |
-
save_to_history("πΌοΈ Image", filename)
|
| 39 |
threading.Timer(10, auto_capture).start()
|
| 40 |
|
| 41 |
# ποΈ Sidebar config like a spaceship control panel!
|
|
@@ -71,24 +72,20 @@ with cols[0]:
|
|
| 71 |
filename = f"cam0_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 72 |
if st.session_state['cam0_file'] and os.path.exists(st.session_state['cam0_file']):
|
| 73 |
os.remove(st.session_state['cam0_file'])
|
| 74 |
-
|
| 75 |
-
f.write(cam0_img.getvalue())
|
| 76 |
st.session_state['cam0_file'] = filename
|
| 77 |
st.session_state['selected_cam_img'] = cam0_img
|
| 78 |
-
|
| 79 |
-
st.image(Image.open(filename), caption="Camera 0 Snap", use_column_width=True)
|
| 80 |
with cols[1]:
|
| 81 |
cam1_img = st.camera_input("π· Camera 1", key="cam1")
|
| 82 |
if cam1_img and camera_choice == "Camera 1":
|
| 83 |
filename = f"cam1_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 84 |
if st.session_state['cam1_file'] and os.path.exists(st.session_state['cam1_file']):
|
| 85 |
os.remove(st.session_state['cam1_file'])
|
| 86 |
-
|
| 87 |
-
f.write(cam1_img.getvalue())
|
| 88 |
st.session_state['cam1_file'] = filename
|
| 89 |
st.session_state['selected_cam_img'] = cam1_img
|
| 90 |
-
|
| 91 |
-
st.image(Image.open(filename), caption="Camera 1 Snap", use_column_width=True)
|
| 92 |
|
| 93 |
# π Upload zone like a media drop party!
|
| 94 |
st.header("π₯π Drop Zone")
|
|
@@ -96,9 +93,7 @@ uploaded_files = st.file_uploader("πΈ Toss Pics", accept_multiple_files=True,
|
|
| 96 |
if uploaded_files:
|
| 97 |
for uploaded_file in uploaded_files:
|
| 98 |
file_path = f"uploaded_{uploaded_file.name}"
|
| 99 |
-
|
| 100 |
-
f.write(uploaded_file.read())
|
| 101 |
-
save_to_history("πΌοΈ Image", file_path)
|
| 102 |
|
| 103 |
# πΌοΈ Gallery like a media circus!
|
| 104 |
st.header("πͺ Snap Show")
|
|
@@ -109,10 +104,13 @@ if st.session_state['file_history']:
|
|
| 109 |
cols = st.columns(3)
|
| 110 |
for i, img in enumerate(images):
|
| 111 |
with cols[i % 3]:
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
else:
|
| 117 |
st.write("π« No snaps yet!")
|
| 118 |
|
|
|
|
| 6 |
import time
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
+
import base64
|
| 10 |
|
| 11 |
# ππ₯ Initialize session state like a galactic DJ spinning tracks!
|
| 12 |
if 'file_history' not in st.session_state:
|
|
|
|
| 21 |
st.session_state['selected_cam_img'] = None
|
| 22 |
|
| 23 |
# ππΎ Save to history like a time-traveling scribe! | π
β¨ save_to_history("πΌοΈ Image", "pic.jpg") - Stamps a pic in the history books like a boss!
|
| 24 |
+
def save_to_history(file_type, file_path, img_data):
|
| 25 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 26 |
+
with open(file_path, "wb") as f:
|
| 27 |
+
f.write(img_data)
|
| 28 |
st.session_state['file_history'].append({
|
| 29 |
"Timestamp": timestamp,
|
| 30 |
"Type": file_type,
|
|
|
|
| 36 |
if st.session_state['auto_capture_running'] and st.session_state['selected_cam_img']:
|
| 37 |
cam_img = st.session_state['selected_cam_img']
|
| 38 |
filename = f"auto_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 39 |
+
save_to_history("πΌοΈ Image", filename, cam_img.getvalue())
|
|
|
|
|
|
|
| 40 |
threading.Timer(10, auto_capture).start()
|
| 41 |
|
| 42 |
# ποΈ Sidebar config like a spaceship control panel!
|
|
|
|
| 72 |
filename = f"cam0_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 73 |
if st.session_state['cam0_file'] and os.path.exists(st.session_state['cam0_file']):
|
| 74 |
os.remove(st.session_state['cam0_file'])
|
| 75 |
+
save_to_history("πΌοΈ Image", filename, cam0_img.getvalue())
|
|
|
|
| 76 |
st.session_state['cam0_file'] = filename
|
| 77 |
st.session_state['selected_cam_img'] = cam0_img
|
| 78 |
+
st.image(Image.open(BytesIO(cam0_img.getvalue())), caption="Camera 0 Snap", use_container_width=True)
|
|
|
|
| 79 |
with cols[1]:
|
| 80 |
cam1_img = st.camera_input("π· Camera 1", key="cam1")
|
| 81 |
if cam1_img and camera_choice == "Camera 1":
|
| 82 |
filename = f"cam1_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 83 |
if st.session_state['cam1_file'] and os.path.exists(st.session_state['cam1_file']):
|
| 84 |
os.remove(st.session_state['cam1_file'])
|
| 85 |
+
save_to_history("πΌοΈ Image", filename, cam1_img.getvalue())
|
|
|
|
| 86 |
st.session_state['cam1_file'] = filename
|
| 87 |
st.session_state['selected_cam_img'] = cam1_img
|
| 88 |
+
st.image(Image.open(BytesIO(cam1_img.getvalue())), caption="Camera 1 Snap", use_container_width=True)
|
|
|
|
| 89 |
|
| 90 |
# π Upload zone like a media drop party!
|
| 91 |
st.header("π₯π Drop Zone")
|
|
|
|
| 93 |
if uploaded_files:
|
| 94 |
for uploaded_file in uploaded_files:
|
| 95 |
file_path = f"uploaded_{uploaded_file.name}"
|
| 96 |
+
save_to_history("πΌοΈ Image", file_path, uploaded_file.getvalue())
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# πΌοΈ Gallery like a media circus!
|
| 99 |
st.header("πͺ Snap Show")
|
|
|
|
| 104 |
cols = st.columns(3)
|
| 105 |
for i, img in enumerate(images):
|
| 106 |
with cols[i % 3]:
|
| 107 |
+
if os.path.exists(img['Path']):
|
| 108 |
+
st.image(img['Path'], caption=img['Path'], use_container_width=True)
|
| 109 |
+
with open(img['Path'], "rb") as f:
|
| 110 |
+
img_data = f.read()
|
| 111 |
+
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)
|
| 112 |
+
else:
|
| 113 |
+
st.warning(f"π¨ Missing file: {img['Path']}")
|
| 114 |
else:
|
| 115 |
st.write("π« No snaps yet!")
|
| 116 |
|