Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -4,20 +4,21 @@ import numpy as np
|
|
4 |
import datetime
|
5 |
import os
|
6 |
import time
|
|
|
7 |
from camera_input_live import camera_input_live
|
8 |
|
9 |
def save_image(image):
|
10 |
-
# Generate a timestamped filename
|
11 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
12 |
filename = f"captured_image_{timestamp}.png"
|
13 |
-
|
14 |
-
# Convert the image to OpenCV format and save
|
15 |
bytes_data = image.getvalue()
|
16 |
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
|
17 |
cv2.imwrite(filename, cv2_img)
|
18 |
-
|
19 |
return filename
|
20 |
|
|
|
|
|
|
|
|
|
21 |
def list_png_files():
|
22 |
return [f for f in os.listdir('.') if f.endswith('.png')]
|
23 |
|
@@ -25,11 +26,9 @@ def main():
|
|
25 |
st.title("Streamlit Camera Input Live Demo")
|
26 |
st.header("Try holding a QR code in front of your webcam")
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
captured_images = list_png_files()
|
31 |
|
32 |
-
# Initialize session state
|
33 |
if 'last_captured' not in st.session_state:
|
34 |
st.session_state['last_captured'] = time.time()
|
35 |
|
@@ -39,28 +38,21 @@ def main():
|
|
39 |
if image is not None:
|
40 |
st.image(image)
|
41 |
|
42 |
-
# Save image every 5 seconds
|
43 |
if time.time() - st.session_state['last_captured'] > 5:
|
44 |
filename = save_image(image)
|
45 |
-
captured_images.append(filename)
|
46 |
st.session_state['last_captured'] = time.time()
|
47 |
|
48 |
-
|
49 |
-
for img_file in captured_images:
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
|
56 |
-
data, bbox, straight_qrcode = detector.detectAndDecode(cv2_img)
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
st.write(data)
|
61 |
-
with st.expander("Show details"):
|
62 |
-
st.write("BBox:", bbox)
|
63 |
-
st.write("Straight QR code:", straight_qrcode)
|
64 |
|
65 |
time.sleep(0.1) # Add a short delay to reduce CPU usage
|
66 |
|
|
|
4 |
import datetime
|
5 |
import os
|
6 |
import time
|
7 |
+
import base64
|
8 |
from camera_input_live import camera_input_live
|
9 |
|
10 |
def save_image(image):
|
|
|
11 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
12 |
filename = f"captured_image_{timestamp}.png"
|
|
|
|
|
13 |
bytes_data = image.getvalue()
|
14 |
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
|
15 |
cv2.imwrite(filename, cv2_img)
|
|
|
16 |
return filename
|
17 |
|
18 |
+
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 |
|
|
|
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 |
|
|
|
38 |
if image is not None:
|
39 |
st.image(image)
|
40 |
|
|
|
41 |
if time.time() - st.session_state['last_captured'] > 5:
|
42 |
filename = save_image(image)
|
43 |
+
st.session_state['captured_images'].append(filename)
|
44 |
st.session_state['last_captured'] = time.time()
|
45 |
|
46 |
+
sidebar_html = "<div style='display:flex;flex-direction:column;'>"
|
47 |
+
for img_file in st.session_state['captured_images']:
|
48 |
+
image_base64 = get_image_base64(img_file)
|
49 |
+
sidebar_html += f"<img src='data:image/png;base64,{image_base64}' style='width:100px;'><br>"
|
50 |
+
sidebar_html += "</div>"
|
51 |
+
st.sidebar.markdown("## Captured Images")
|
52 |
+
st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)
|
|
|
|
|
53 |
|
54 |
+
# QR Code Detection (remains unchanged)
|
55 |
+
# ...
|
|
|
|
|
|
|
|
|
56 |
|
57 |
time.sleep(0.1) # Add a short delay to reduce CPU usage
|
58 |
|