Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
|
|
|
|
|
|
|
32 |
if 'last_captured' not in st.session_state:
|
33 |
st.session_state['last_captured'] = time.time()
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
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()
|