File size: 2,207 Bytes
ae8a7b7
43097e0
 
40280ea
 
57034fc
5cb1e06
43097e0
 
40280ea
 
 
43097e0
 
40280ea
 
 
5cb1e06
 
 
 
40280ea
 
 
 
edf8ac9
 
 
 
 
57034fc
edf8ac9
 
 
57034fc
 
edf8ac9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43097e0
40280ea
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
59
60
61
62
63
import streamlit as st
import cv2
import numpy as np
import datetime
import os
import time
import base64
from camera_input_live import camera_input_live

def save_image(image):
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"captured_image_{timestamp}.png"
    bytes_data = image.getvalue()
    cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
    cv2.imwrite(filename, cv2_img)
    return filename

def get_image_base64(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode()

def main():
    st.title("Streamlit Camera Input Live Demo")
    st.header("Try holding a QR code in front of your webcam")

    # Slider for snapshot interval
    snapshot_interval = st.sidebar.slider("Snapshot Interval (seconds)", 1, 10, 5)

    # Display container for live image
    image_placeholder = st.empty()

    # Initialize session state for captured images and last captured time
    if 'captured_images' not in st.session_state:
        st.session_state['captured_images'] = []
    if 'last_captured' not in st.session_state:
        st.session_state['last_captured'] = time.time()

    # Capture and display live image
    image = camera_input_live()
    if image is not None:
        image_placeholder.image(image)

        # Save image based on interval
        if time.time() - st.session_state['last_captured'] > snapshot_interval:
            filename = save_image(image)
            st.session_state['captured_images'].append(filename)
            st.session_state['last_captured'] = time.time()

    # Update sidebar with captured images
    sidebar_html = "<div style='display:flex;flex-direction:column;'>"
    for img_file in st.session_state['captured_images']:
        image_base64 = get_image_base64(img_file)
        sidebar_html += f"<img src='data:image/png;base64,{image_base64}' style='width:100px;'><br>"
    sidebar_html += "</div>"
    st.sidebar.markdown("## Captured Images")
    st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)

    # Trigger a rerun of the app to update the live feed
    st.experimental_rerun()

if __name__ == "__main__":
    main()