File size: 2,175 Bytes
ae8a7b7
43097e0
 
40280ea
 
57034fc
43097e0
 
40280ea
 
 
 
43097e0
40280ea
43097e0
 
40280ea
 
 
 
e37b28b
 
 
40280ea
 
 
 
57034fc
 
e37b28b
57034fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e9e1c1
57034fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
import streamlit as st
import cv2
import numpy as np
import datetime
import os
import time
from camera_input_live import camera_input_live

def save_image(image):
    # Generate a timestamped filename
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"captured_image_{timestamp}.png"

    # Convert the image to OpenCV format and save
    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 list_png_files():
    return [f for f in os.listdir('.') if f.endswith('.png')]

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

    # Sidebar for captured images
    st.sidebar.markdown("## Captured Images")
    captured_images = list_png_files()

    # Initialize session state
    if 'last_captured' not in st.session_state:
        st.session_state['last_captured'] = time.time()

    while True:
        image = camera_input_live()

        if image is not None:
            st.image(image)

            # Save image every 5 seconds
            if time.time() - st.session_state['last_captured'] > 5:
                filename = save_image(image)
                captured_images.append(filename)
                st.session_state['last_captured'] = time.time()

            # Update sidebar
            for img_file in captured_images:
                st.sidebar.markdown(f"- [{img_file}]({img_file})")

            # QR Code Detection
            detector = cv2.QRCodeDetector()
            bytes_data = image.getvalue()
            cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
            data, bbox, straight_qrcode = detector.detectAndDecode(cv2_img)

            if data:
                st.write("# Found QR code")
                st.write(data)
                with st.expander("Show details"):
                    st.write("BBox:", bbox)
                    st.write("Straight QR code:", straight_qrcode)

        time.sleep(0.1)  # Add a short delay to reduce CPU usage

if __name__ == "__main__":
    main()