Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import re
|
9 |
+
import glob
|
10 |
+
from camera_input_live import camera_input_live
|
11 |
+
import face_recognition
|
12 |
+
|
13 |
+
# Set wide layout
|
14 |
+
st.set_page_config(layout="wide")
|
15 |
+
|
16 |
+
# Decorator for caching images
|
17 |
+
def get_image_count():
|
18 |
+
return {'count': 0}
|
19 |
+
|
20 |
+
# Function Definitions for Camera Feature
|
21 |
+
def save_image(image, image_count):
|
22 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
23 |
+
filename = f"captured_image_{timestamp}_{image_count['count']}.png"
|
24 |
+
image_count['count'] += 1
|
25 |
+
bytes_data = image.getvalue()
|
26 |
+
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
|
27 |
+
cv2.imwrite(filename, cv2_img)
|
28 |
+
return filename
|
29 |
+
|
30 |
+
def get_image_base64(image_path):
|
31 |
+
with open(image_path, "rb") as image_file:
|
32 |
+
return base64.b64encode(image_file.read()).decode()
|
33 |
+
|
34 |
+
# Function Definitions for Chord Sheet Feature
|
35 |
+
def process_line(line):
|
36 |
+
if re.search(r'\b[A-G][#b]?m?\b', line):
|
37 |
+
line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\1.png' style='height:20px;'>", line)
|
38 |
+
return line
|
39 |
+
|
40 |
+
def process_sheet(sheet):
|
41 |
+
processed_lines = []
|
42 |
+
for line in sheet.split('\n'):
|
43 |
+
processed_line = process_line(line)
|
44 |
+
processed_lines.append(processed_line)
|
45 |
+
return '<br>'.join(processed_lines)
|
46 |
+
|
47 |
+
# Load a sample image and learn how to recognize it
|
48 |
+
known_image = face_recognition.load_image_file("known_face.jpg")
|
49 |
+
known_encoding = face_recognition.face_encodings(known_image)[0]
|
50 |
+
|
51 |
+
# Main Function
|
52 |
+
def main():
|
53 |
+
# Layout Configuration
|
54 |
+
col1, col2 = st.columns([2, 3])
|
55 |
+
|
56 |
+
# Camera Section
|
57 |
+
with col1:
|
58 |
+
st.markdown("✨ Magic Lens: Real-Time Camera Stream 🌈")
|
59 |
+
|
60 |
+
snapshot_interval = st.slider("Snapshot Interval (seconds)", 1, 10, 5)
|
61 |
+
image_placeholder = st.empty()
|
62 |
+
|
63 |
+
if 'captured_images' not in st.session_state:
|
64 |
+
st.session_state['captured_images'] = []
|
65 |
+
if 'last_captured' not in st.session_state:
|
66 |
+
st.session_state['last_captured'] = time.time()
|
67 |
+
|
68 |
+
image = camera_input_live()
|
69 |
+
if image is not None:
|
70 |
+
# Convert the image to RGB format for face_recognition
|
71 |
+
rgb_image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
|
72 |
+
|
73 |
+
# Detect faces in the image
|
74 |
+
face_locations = face_recognition.face_locations(rgb_image)
|
75 |
+
face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
|
76 |
+
|
77 |
+
# Iterate over detected faces and compare with known face
|
78 |
+
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
79 |
+
matches = face_recognition.compare_faces([known_encoding], face_encoding)
|
80 |
+
|
81 |
+
if True in matches:
|
82 |
+
# If a match is found, draw a green rectangle and label
|
83 |
+
cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 255, 0), 2)
|
84 |
+
cv2.putText(rgb_image, "Known Face", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
85 |
+
else:
|
86 |
+
# If no match, draw a red rectangle
|
87 |
+
cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 0, 255), 2)
|
88 |
+
|
89 |
+
# Convert the RGB image back to BGR format for display
|
90 |
+
bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
|
91 |
+
image_placeholder.image(bgr_image, channels="BGR")
|
92 |
+
|
93 |
+
if time.time() - st.session_state['last_captured'] > snapshot_interval:
|
94 |
+
image_count = get_image_count()
|
95 |
+
filename = save_image(image, image_count)
|
96 |
+
st.session_state['captured_images'].append(filename)
|
97 |
+
st.session_state['last_captured'] = time.time()
|
98 |
+
|
99 |
+
sidebar_html = "<div style='display:flex;flex-direction:column;'>"
|
100 |
+
for img_file in st.session_state['captured_images']:
|
101 |
+
image_base64 = get_image_base64(img_file)
|
102 |
+
sidebar_html += f"<img src='data:image/png;base64,{image_base64}' style='width:100px;'><br>"
|
103 |
+
sidebar_html += "</div>"
|
104 |
+
st.sidebar.markdown("## Captured Images")
|
105 |
+
st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)
|
106 |
+
|
107 |
+
# JavaScript Timer
|
108 |
+
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)
|
109 |
+
|
110 |
+
# Chord Sheet Section
|
111 |
+
with col2:
|
112 |
+
st.markdown("## 🎬 Action! Real-Time Camera Stream Highlights 📽️")
|
113 |
+
|
114 |
+
all_files = [f for f in glob.glob("*.png") if ' by ' in f]
|
115 |
+
selected_file = st.selectbox("Choose a Dataset:", all_files)
|
116 |
+
|
117 |
+
if selected_file:
|
118 |
+
with open(selected_file, 'r', encoding='utf-8') as file:
|
119 |
+
sheet = file.read()
|
120 |
+
st.markdown(process_sheet(sheet), unsafe_allow_html=True)
|
121 |
+
|
122 |
+
# Trigger a rerun only when the snapshot interval is reached
|
123 |
+
if 'last_captured' in st.session_state and time.time() - st.session_state['last_captured'] > snapshot_interval:
|
124 |
+
st.experimental_rerun()
|
125 |
+
|
126 |
+
if __name__ == "__main__":
|
127 |
+
main()
|