Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,19 +8,19 @@ 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:
|
| 13 |
st.session_state['file_history'] = []
|
| 14 |
-
if 'auto_capture_running' not in st.session_state:
|
| 15 |
-
st.session_state['auto_capture_running'] = False
|
| 16 |
if 'cam0_file' not in st.session_state:
|
| 17 |
st.session_state['cam0_file'] = None
|
| 18 |
if 'cam1_file' not in st.session_state:
|
| 19 |
st.session_state['cam1_file'] = None
|
| 20 |
-
if 'selected_cam_img' 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:
|
|
@@ -33,10 +33,16 @@ def save_to_history(file_type, file_path, img_data):
|
|
| 33 |
|
| 34 |
# πΈβ° Auto-capture every 10 secs like a sneaky shutterbug!
|
| 35 |
def auto_capture():
|
| 36 |
-
|
| 37 |
-
|
| 38 |
filename = f"auto_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
threading.Timer(10, auto_capture).start()
|
| 41 |
|
| 42 |
# ποΈ Sidebar config like a spaceship control panel!
|
|
@@ -44,10 +50,12 @@ with st.sidebar:
|
|
| 44 |
st.header("ποΈπΈ Snap Shack")
|
| 45 |
camera_choice = st.selectbox("π· Pick a Cam", ["Camera 0", "Camera 1"])
|
| 46 |
if st.button("β° Start Auto-Snap"):
|
| 47 |
-
|
|
|
|
| 48 |
auto_capture()
|
| 49 |
if st.button("βΉοΈ Stop Auto-Snap"):
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
# π Sidebar file outline with emoji flair!
|
| 53 |
st.subheader("π Snap Stash")
|
|
@@ -74,7 +82,8 @@ with cols[0]:
|
|
| 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 |
-
|
|
|
|
| 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")
|
|
@@ -84,7 +93,8 @@ with cols[1]:
|
|
| 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 |
-
|
|
|
|
| 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!
|
|
@@ -92,8 +102,9 @@ st.header("π₯π Drop Zone")
|
|
| 92 |
uploaded_files = st.file_uploader("πΈ Toss Pics", accept_multiple_files=True, type=['jpg', 'png'])
|
| 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")
|
|
|
|
| 8 |
from io import BytesIO
|
| 9 |
import base64
|
| 10 |
|
| 11 |
+
# ππ₯ Initialize global state for threading safety!
|
| 12 |
+
AUTO_CAPTURE_RUNNING = False
|
| 13 |
+
SELECTED_CAM_IMG = None
|
| 14 |
+
|
| 15 |
# ππ₯ Initialize session state like a galactic DJ spinning tracks!
|
| 16 |
if 'file_history' not in st.session_state:
|
| 17 |
st.session_state['file_history'] = []
|
|
|
|
|
|
|
| 18 |
if 'cam0_file' not in st.session_state:
|
| 19 |
st.session_state['cam0_file'] = None
|
| 20 |
if 'cam1_file' not in st.session_state:
|
| 21 |
st.session_state['cam1_file'] = None
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# ππΎ 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!
|
| 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:
|
|
|
|
| 33 |
|
| 34 |
# πΈβ° Auto-capture every 10 secs like a sneaky shutterbug!
|
| 35 |
def auto_capture():
|
| 36 |
+
global AUTO_CAPTURE_RUNNING, SELECTED_CAM_IMG
|
| 37 |
+
if AUTO_CAPTURE_RUNNING and SELECTED_CAM_IMG:
|
| 38 |
filename = f"auto_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 39 |
+
with open(filename, "wb") as f:
|
| 40 |
+
f.write(SELECTED_CAM_IMG)
|
| 41 |
+
st.session_state['file_history'].append({
|
| 42 |
+
"Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 43 |
+
"Type": "πΌοΈ Image",
|
| 44 |
+
"Path": filename
|
| 45 |
+
})
|
| 46 |
threading.Timer(10, auto_capture).start()
|
| 47 |
|
| 48 |
# ποΈ Sidebar config like a spaceship control panel!
|
|
|
|
| 50 |
st.header("ποΈπΈ Snap Shack")
|
| 51 |
camera_choice = st.selectbox("π· Pick a Cam", ["Camera 0", "Camera 1"])
|
| 52 |
if st.button("β° Start Auto-Snap"):
|
| 53 |
+
global AUTO_CAPTURE_RUNNING
|
| 54 |
+
AUTO_CAPTURE_RUNNING = True
|
| 55 |
auto_capture()
|
| 56 |
if st.button("βΉοΈ Stop Auto-Snap"):
|
| 57 |
+
global AUTO_CAPTURE_RUNNING
|
| 58 |
+
AUTO_CAPTURE_RUNNING = False
|
| 59 |
|
| 60 |
# π Sidebar file outline with emoji flair!
|
| 61 |
st.subheader("π Snap Stash")
|
|
|
|
| 82 |
os.remove(st.session_state['cam0_file'])
|
| 83 |
save_to_history("πΌοΈ Image", filename, cam0_img.getvalue())
|
| 84 |
st.session_state['cam0_file'] = filename
|
| 85 |
+
global SELECTED_CAM_IMG
|
| 86 |
+
SELECTED_CAM_IMG = cam0_img.getvalue()
|
| 87 |
st.image(Image.open(BytesIO(cam0_img.getvalue())), caption="Camera 0 Snap", use_container_width=True)
|
| 88 |
with cols[1]:
|
| 89 |
cam1_img = st.camera_input("π· Camera 1", key="cam1")
|
|
|
|
| 93 |
os.remove(st.session_state['cam1_file'])
|
| 94 |
save_to_history("πΌοΈ Image", filename, cam1_img.getvalue())
|
| 95 |
st.session_state['cam1_file'] = filename
|
| 96 |
+
global SELECTED_CAM_IMG
|
| 97 |
+
SELECTED_CAM_IMG = cam1_img.getvalue()
|
| 98 |
st.image(Image.open(BytesIO(cam1_img.getvalue())), caption="Camera 1 Snap", use_container_width=True)
|
| 99 |
|
| 100 |
# π Upload zone like a media drop party!
|
|
|
|
| 102 |
uploaded_files = st.file_uploader("πΈ Toss Pics", accept_multiple_files=True, type=['jpg', 'png'])
|
| 103 |
if uploaded_files:
|
| 104 |
for uploaded_file in uploaded_files:
|
| 105 |
+
file_path = f"uploaded_{uploaded_file.name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 106 |
save_to_history("πΌοΈ Image", file_path, uploaded_file.getvalue())
|
| 107 |
+
st.image(Image.open(BytesIO(uploaded_file.getvalue())), caption=uploaded_file.name, use_container_width=True)
|
| 108 |
|
| 109 |
# πΌοΈ Gallery like a media circus!
|
| 110 |
st.header("πͺ Snap Show")
|