awacke1 commited on
Commit
e50e82a
Β·
verified Β·
1 Parent(s): 19de063

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
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
- 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!
@@ -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
- st.session_state['auto_capture_running'] = True
 
48
  auto_capture()
49
  if st.button("⏹️ Stop Auto-Snap"):
50
- st.session_state['auto_capture_running'] = False
 
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
- 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")
@@ -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
- 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!
@@ -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")