awacke1 commited on
Commit
3593298
1 Parent(s): 1d5a839

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -33
app.py CHANGED
@@ -5,8 +5,14 @@ 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"
@@ -19,43 +25,67 @@ 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 main():
23
- st.title("Streamlit Camera Real Time Live Streming")
24
- st.header("QR code reader is ready.")
25
-
26
- # Slider for snapshot interval
27
- snapshot_interval = st.sidebar.slider("Snapshot Interval (seconds)", 1, 10, 5)
28
-
29
- # Display container for live image
30
- image_placeholder = st.empty()
31
-
32
- # Initialize session state for captured images and last captured time
33
- if 'captured_images' not in st.session_state:
34
- st.session_state['captured_images'] = []
35
- if 'last_captured' not in st.session_state:
36
- st.session_state['last_captured'] = time.time()
37
-
38
- # Capture and display live image
39
- image = camera_input_live()
40
- if image is not None:
41
- image_placeholder.image(image)
42
-
43
- # Save image based on interval
44
- if time.time() - st.session_state['last_captured'] > snapshot_interval:
45
- filename = save_image(image)
46
- st.session_state['captured_images'].append(filename)
47
  st.session_state['last_captured'] = time.time()
48
 
49
- # Update sidebar with captured images
50
- sidebar_html = "<div style='display:flex;flex-direction:column;'>"
51
- for img_file in st.session_state['captured_images']:
52
- image_base64 = get_image_base64(img_file)
53
- sidebar_html += f"<img src='data:image/png;base64,{image_base64}' style='width:100px;'><br>"
54
- sidebar_html += "</div>"
55
- st.sidebar.markdown("## Captured Images")
56
- st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- # Trigger a rerun of the app to update the live feed
59
  st.experimental_rerun()
60
 
61
  if __name__ == "__main__":
 
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
 
12
+ # Set wide layout
13
+ st.set_page_config(layout="wide")
14
+
15
+ # Function Definitions for Camera Feature
16
  def save_image(image):
17
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
18
  filename = f"captured_image_{timestamp}.png"
 
25
  with open(image_path, "rb") as image_file:
26
  return base64.b64encode(image_file.read()).decode()
27
 
28
+ # Function Definitions for Chord Sheet Feature
29
+ def process_line(line):
30
+ if re.search(r'\b[A-G][#b]?m?\b', line):
31
+ line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\1.png' style='height:20px;'>", line)
32
+ return line
33
+
34
+ def process_chord_sheet(chord_sheet):
35
+ processed_lines = []
36
+ for line in chord_sheet.split('\n'):
37
+ processed_line = process_line(line)
38
+ processed_lines.append(processed_line)
39
+ return '<br>'.join(processed_lines)
40
+
41
+ # Main Function
42
  def main():
43
+ # Layout Configuration
44
+ col1, col2, col3 = st.columns([2, 3, 5])
45
+
46
+ # Camera Section
47
+ with col1:
48
+ st.title("Real-Time Camera Stream")
49
+ st.header("QR code reader ready.")
50
+
51
+ snapshot_interval = st.slider("Snapshot Interval (seconds)", 1, 10, 5)
52
+ image_placeholder = st.empty()
53
+
54
+ if 'captured_images' not in st.session_state:
55
+ st.session_state['captured_images'] = []
56
+ if 'last_captured' not in st.session_state:
 
 
 
 
 
 
 
 
 
 
57
  st.session_state['last_captured'] = time.time()
58
 
59
+ image = camera_input_live()
60
+ if image is not None:
61
+ image_placeholder.image(image)
62
+
63
+ if time.time() - st.session_state['last_captured'] > snapshot_interval:
64
+ filename = save_image(image)
65
+ st.session_state['captured_images'].append(filename)
66
+ st.session_state['last_captured'] = time.time()
67
+
68
+ sidebar_html = "<div style='display:flex;flex-direction:column;'>"
69
+ for img_file in st.session_state['captured_images']:
70
+ image_base64 = get_image_base64(img_file)
71
+ sidebar_html += f"<img src='data:image/png;base64,{image_base64}' style='width:100px;'><br>"
72
+ sidebar_html += "</div>"
73
+ st.sidebar.markdown("## Captured Images")
74
+ st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)
75
+
76
+ # Chord Sheet Section
77
+ with col2:
78
+ st.title("Chord Sheet Manager")
79
+
80
+ all_files = [f for f in glob.glob("*.txt") if ' by ' in f]
81
+ selected_file = st.selectbox("Choose a Song:", all_files)
82
+
83
+ if selected_file:
84
+ with open(selected_file, 'r', encoding='utf-8') as file:
85
+ chord_sheet = file.read()
86
+ st.markdown(process_chord_sheet(chord_sheet), unsafe_allow_html=True)
87
 
88
+ # Trigger a rerun to update the live feed
89
  st.experimental_rerun()
90
 
91
  if __name__ == "__main__":