awacke1's picture
Update backupapp.py
05ed916 verified
raw
history blame
2.14 kB
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 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")
if 'captured_images' not in st.session_state:
st.session_state['captured_images'] = list_png_files()
if 'last_captured' not in st.session_state:
st.session_state['last_captured'] = time.time()
while True:
try:
image = camera_input_live()
if image is not None:
st.image(image. key='', label='')
if time.time() - st.session_state['last_captured'] > 5:
filename = save_image(image)
st.session_state['captured_images'].append(filename)
st.session_state['last_captured'] = time.time()
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)
time.sleep(0.5) # Add a short delay to reduce CPU usage
except:
st.write('.')
if __name__ == "__main__":
main()