Spaces:
Runtime error
Runtime error
File size: 5,770 Bytes
fbc3a6c 0346279 fbc3a6c 0346279 fbc3a6c 0346279 fbc3a6c 0346279 fbc3a6c |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
import streamlit as st
import cv2
import numpy as np
import datetime
import os
import time
import base64
import re
import glob
from camera_input_live import camera_input_live
import face_recognition
# Set wide layout
st.set_page_config(layout="wide")
# Decorator for caching images
def get_image_count():
return {'count': 0}
# Function Definitions for Camera Feature
def save_image(image, image_count):
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"captured_image_{timestamp}_{image_count['count']}.png"
image_count['count'] += 1
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()
# Function Definitions for Chord Sheet Feature
def process_line(line):
if re.search(r'\b[A-G][#b]?m?\b', line):
line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\1.png' style='height:20px;'>", line)
return line
def process_sheet(sheet):
processed_lines = []
for line in sheet.split('\n'):
processed_line = process_line(line)
processed_lines.append(processed_line)
return '<br>'.join(processed_lines)
# Main Function
def main():
# Layout Configuration
col1, col2 = st.columns([2, 3])
# Camera Section
with col1:
st.markdown("✨ Magic Lens: Real-Time Camera Stream 🌈")
snapshot_interval = st.slider("Snapshot Interval (seconds)", 1, 10, 5)
image_placeholder = st.empty()
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()
image = camera_input_live()
if image is not None:
# Convert the image to RGB format for face_recognition
rgb_image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
# Detect faces in the image
face_locations = face_recognition.face_locations(rgb_image)
face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
# Check if the known face image exists
if os.path.isfile("known_face.jpg"):
known_image = face_recognition.load_image_file("known_face.jpg")
known_encoding = face_recognition.face_encodings(known_image)[0]
else:
known_encoding = None
# Iterate over detected faces and compare with known face
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
if known_encoding is not None:
matches = face_recognition.compare_faces([known_encoding], face_encoding)
if True in matches:
# If a match is found, draw a green rectangle and label
cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(rgb_image, "Known Face", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
else:
# If no match, draw a red rectangle
cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 0, 255), 2)
else:
# If no known face is registered, draw a blue rectangle
cv2.rectangle(rgb_image, (left, top), (right, bottom), (255, 0, 0), 2)
# Convert the RGB image back to BGR format for display
bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
image_placeholder.image(bgr_image, channels="BGR")
if time.time() - st.session_state['last_captured'] > snapshot_interval:
image_count = get_image_count()
filename = save_image(image, image_count)
st.session_state['captured_images'].append(filename)
st.session_state['last_captured'] = time.time()
if st.button("Register Known Face"):
if image is not None:
cv2.imwrite("known_face.jpg", np.array(image))
st.success("Known face registered successfully!")
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)
# JavaScript Timer
st.markdown(f"<script>setInterval(function() {{ document.getElementById('timer').innerHTML = new Date().toLocaleTimeString(); }}, 1000);</script><div>Current Time: <span id='timer'></span></div>", unsafe_allow_html=True)
# Chord Sheet Section
with col2:
st.markdown("## 🎬 Action! Real-Time Camera Stream Highlights 📽️")
all_files = [f for f in glob.glob("*.png") if ' by ' in f]
selected_file = st.selectbox("Choose a Dataset:", all_files)
if selected_file:
with open(selected_file, 'r', encoding='utf-8') as file:
sheet = file.read()
st.markdown(process_sheet(sheet), unsafe_allow_html=True)
# Trigger a rerun only when the snapshot interval is reached
if 'last_captured' in st.session_state and time.time() - st.session_state['last_captured'] > snapshot_interval:
st.experimental_rerun()
if __name__ == "__main__":
main() |