awacke1 commited on
Commit
775b3fc
1 Parent(s): 0346279

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -19
app.py CHANGED
@@ -10,14 +10,11 @@ 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"
@@ -31,7 +28,6 @@ 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)
@@ -44,12 +40,9 @@ def process_sheet(sheet):
44
  processed_lines.append(processed_line)
45
  return '<br>'.join(processed_lines)
46
 
47
- # Main Function
48
  def main():
49
- # Layout Configuration
50
  col1, col2 = st.columns([2, 3])
51
 
52
- # Camera Section
53
  with col1:
54
  st.markdown("✨ Magic Lens: Real-Time Camera Stream 🌈")
55
 
@@ -63,37 +56,29 @@ def main():
63
 
64
  image = camera_input_live()
65
  if image is not None:
66
- # Convert the image to RGB format for face_recognition
67
- rgb_image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
68
 
69
- # Detect faces in the image
70
  face_locations = face_recognition.face_locations(rgb_image)
71
  face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
72
 
73
- # Check if the known face image exists
74
  if os.path.isfile("known_face.jpg"):
75
  known_image = face_recognition.load_image_file("known_face.jpg")
76
  known_encoding = face_recognition.face_encodings(known_image)[0]
77
  else:
78
  known_encoding = None
79
 
80
- # Iterate over detected faces and compare with known face
81
  for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
82
  if known_encoding is not None:
83
  matches = face_recognition.compare_faces([known_encoding], face_encoding)
84
 
85
  if True in matches:
86
- # If a match is found, draw a green rectangle and label
87
  cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 255, 0), 2)
88
  cv2.putText(rgb_image, "Known Face", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
89
  else:
90
- # If no match, draw a red rectangle
91
  cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 0, 255), 2)
92
  else:
93
- # If no known face is registered, draw a blue rectangle
94
  cv2.rectangle(rgb_image, (left, top), (right, bottom), (255, 0, 0), 2)
95
 
96
- # Convert the RGB image back to BGR format for display
97
  bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
98
  image_placeholder.image(bgr_image, channels="BGR")
99
 
@@ -116,10 +101,8 @@ def main():
116
  st.sidebar.markdown("## Captured Images")
117
  st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)
118
 
119
- # JavaScript Timer
120
  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)
121
 
122
- # Chord Sheet Section
123
  with col2:
124
  st.markdown("## 🎬 Action! Real-Time Camera Stream Highlights 📽️")
125
 
@@ -131,7 +114,6 @@ def main():
131
  sheet = file.read()
132
  st.markdown(process_sheet(sheet), unsafe_allow_html=True)
133
 
134
- # Trigger a rerun only when the snapshot interval is reached
135
  if 'last_captured' in st.session_state and time.time() - st.session_state['last_captured'] > snapshot_interval:
136
  st.experimental_rerun()
137
 
 
10
  from camera_input_live import camera_input_live
11
  import face_recognition
12
 
 
13
  st.set_page_config(layout="wide")
14
 
 
15
  def get_image_count():
16
  return {'count': 0}
17
 
 
18
  def save_image(image, image_count):
19
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
20
  filename = f"captured_image_{timestamp}_{image_count['count']}.png"
 
28
  with open(image_path, "rb") as image_file:
29
  return base64.b64encode(image_file.read()).decode()
30
 
 
31
  def process_line(line):
32
  if re.search(r'\b[A-G][#b]?m?\b', line):
33
  line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\1.png' style='height:20px;'>", line)
 
40
  processed_lines.append(processed_line)
41
  return '<br>'.join(processed_lines)
42
 
 
43
  def main():
 
44
  col1, col2 = st.columns([2, 3])
45
 
 
46
  with col1:
47
  st.markdown("✨ Magic Lens: Real-Time Camera Stream 🌈")
48
 
 
56
 
57
  image = camera_input_live()
58
  if image is not None:
59
+ rgb_image = cv2.cvtColor(cv2.imdecode(np.frombuffer(image.getvalue(), np.uint8), cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)
 
60
 
 
61
  face_locations = face_recognition.face_locations(rgb_image)
62
  face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
63
 
 
64
  if os.path.isfile("known_face.jpg"):
65
  known_image = face_recognition.load_image_file("known_face.jpg")
66
  known_encoding = face_recognition.face_encodings(known_image)[0]
67
  else:
68
  known_encoding = None
69
 
 
70
  for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
71
  if known_encoding is not None:
72
  matches = face_recognition.compare_faces([known_encoding], face_encoding)
73
 
74
  if True in matches:
 
75
  cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 255, 0), 2)
76
  cv2.putText(rgb_image, "Known Face", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
77
  else:
 
78
  cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 0, 255), 2)
79
  else:
 
80
  cv2.rectangle(rgb_image, (left, top), (right, bottom), (255, 0, 0), 2)
81
 
 
82
  bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
83
  image_placeholder.image(bgr_image, channels="BGR")
84
 
 
101
  st.sidebar.markdown("## Captured Images")
102
  st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)
103
 
 
104
  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)
105
 
 
106
  with col2:
107
  st.markdown("## 🎬 Action! Real-Time Camera Stream Highlights 📽️")
108
 
 
114
  sheet = file.read()
115
  st.markdown(process_sheet(sheet), unsafe_allow_html=True)
116
 
 
117
  if 'last_captured' in st.session_state and time.time() - st.session_state['last_captured'] > snapshot_interval:
118
  st.experimental_rerun()
119