Spaces:
Build error
Build error
Update backupapp.py
Browse files- backupapp.py +55 -28
backupapp.py
CHANGED
|
@@ -1,35 +1,62 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
from camera_input_live import camera_input_live
|
| 4 |
-
|
| 5 |
-
#image = camera_input_live()
|
| 6 |
-
|
| 7 |
-
#if image:
|
| 8 |
-
# st.image(image)
|
| 9 |
-
|
| 10 |
import cv2
|
| 11 |
import numpy as np
|
| 12 |
-
import
|
|
|
|
|
|
|
|
|
|
| 13 |
from camera_input_live import camera_input_live
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
image = camera_input_live()
|
| 19 |
-
|
| 20 |
-
if image is not None:
|
| 21 |
-
st.image(image)
|
| 22 |
bytes_data = image.getvalue()
|
| 23 |
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 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 |
+
|
| 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. key='', label='')
|
| 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.5) # Add a short delay to reduce CPU usage
|
| 57 |
+
|
| 58 |
+
except:
|
| 59 |
+
st.write('.')
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
main()
|