awacke1 commited on
Commit
edf8ac9
1 Parent(s): 05ed916

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -31
app.py CHANGED
@@ -19,44 +19,44 @@ def get_image_base64(image_path):
19
  with open(image_path, "rb") as image_file:
20
  return base64.b64encode(image_file.read()).decode()
21
 
22
- def list_png_files():
23
- return [f for f in os.listdir('.') if f.endswith('.png')]
24
-
25
  def main():
26
  st.title("Streamlit Camera Input Live Demo")
27
  st.header("Try holding a QR code in front of your webcam")
28
 
29
- if 'captured_images' not in st.session_state:
30
- st.session_state['captured_images'] = list_png_files()
 
 
 
31
 
 
 
 
32
  if 'last_captured' not in st.session_state:
33
  st.session_state['last_captured'] = time.time()
34
-
35
-
36
- while True:
37
- try:
38
- image = camera_input_live()
39
-
40
- if image is not None:
41
- st.image(image)
42
-
43
- if time.time() - st.session_state['last_captured'] > 5:
44
- filename = save_image(image)
45
- st.session_state['captured_images'].append(filename)
46
- st.session_state['last_captured'] = time.time()
47
-
48
- sidebar_html = "<div style='display:flex;flex-direction:column;'>"
49
- for img_file in st.session_state['captured_images']:
50
- image_base64 = get_image_base64(img_file)
51
- sidebar_html += f"<img src='data:image/png;base64,{image_base64}' style='width:100px;'><br>"
52
- sidebar_html += "</div>"
53
- st.sidebar.markdown("## Captured Images")
54
- st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)
55
-
56
- time.sleep(0.1) # Add a short delay to reduce CPU usage
57
-
58
- except:
59
- st.write('.')
60
 
61
  if __name__ == "__main__":
62
  main()
 
19
  with open(image_path, "rb") as image_file:
20
  return base64.b64encode(image_file.read()).decode()
21
 
 
 
 
22
  def main():
23
  st.title("Streamlit Camera Input Live Demo")
24
  st.header("Try holding a QR code in front of your webcam")
25
 
26
+ # Slider for snapshot interval
27
+ snapshot_interval = st.sidebar.slider("Snapshot Interval (seconds)", 1, 10, 5)
28
+
29
+ # Display container for live image
30
+ image_placeholder = st.empty()
31
 
32
+ # Initialize session state for captured images and last captured time
33
+ if 'captured_images' not in st.session_state:
34
+ st.session_state['captured_images'] = []
35
  if 'last_captured' not in st.session_state:
36
  st.session_state['last_captured'] = time.time()
37
+
38
+ # Capture and display live image
39
+ image = camera_input_live()
40
+ if image is not None:
41
+ image_placeholder.image(image)
42
+
43
+ # Save image based on interval
44
+ if time.time() - st.session_state['last_captured'] > snapshot_interval:
45
+ filename = save_image(image)
46
+ st.session_state['captured_images'].append(filename)
47
+ st.session_state['last_captured'] = time.time()
48
+
49
+ # Update sidebar with captured images
50
+ sidebar_html = "<div style='display:flex;flex-direction:column;'>"
51
+ for img_file in st.session_state['captured_images']:
52
+ image_base64 = get_image_base64(img_file)
53
+ sidebar_html += f"<img src='data:image/png;base64,{image_base64}' style='width:100px;'><br>"
54
+ sidebar_html += "</div>"
55
+ st.sidebar.markdown("## Captured Images")
56
+ st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)
57
+
58
+ # Trigger a rerun of the app to update the live feed
59
+ st.experimental_rerun()
 
 
 
60
 
61
  if __name__ == "__main__":
62
  main()