osheina commited on
Commit
d837ce1
·
verified ·
1 Parent(s): e6de67d

Update pages/Camera.py

Browse files
Files changed (1) hide show
  1. pages/Camera.py +26 -21
pages/Camera.py CHANGED
@@ -2,13 +2,13 @@ import logging
2
  import queue
3
  from collections import deque
4
  import asyncio
5
-
 
 
6
  import streamlit as st
7
  from streamlit_webrtc import WebRtcMode, webrtc_streamer
8
-
9
  from utils import SLInference
10
 
11
-
12
  logger = logging.getLogger(__name__)
13
 
14
  def main(config_path):
@@ -18,15 +18,10 @@ def main(config_path):
18
  inference_thread = SLInference(config_path)
19
  inference_thread.start()
20
 
21
- webrtc_ctx = webrtc_streamer(
22
- key="video-sendonly",
23
- mode=WebRtcMode.SENDONLY,
24
- media_stream_constraints={"video": True},
25
- )
26
-
27
  gestures_deque = deque(maxlen=5)
28
 
29
  # Set up Streamlit interface
 
30
  st.title("Sign Language Recognition Demo")
31
  image_place = st.empty()
32
  text_output = st.empty()
@@ -35,22 +30,34 @@ def main(config_path):
35
  """
36
  This application is designed to recognize sign language using a webcam feed.
37
  The model has been trained to recognize various sign language gestures and display the corresponding text in real-time.
38
-
39
  The project is open for collaboration. If you have any suggestions or want to contribute, please feel free to reach out.
40
  """
41
  )
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  while True:
44
- if webrtc_ctx.video_receiver:
45
- try:
46
- video_frame = webrtc_ctx.video_receiver.get_frame(timeout=1)
47
- except queue.Empty:
48
- logger.warning("Queue is empty")
49
- continue
50
 
51
- img_rgb = video_frame.to_ndarray(format="rgb24")
52
  image_place.image(img_rgb)
53
- inference_thread.input_queue.append(video_frame.reformat(224,224).to_ndarray(format="rgb24"))
54
 
55
  gesture = inference_thread.pred
56
  if gesture not in ['no', '']:
@@ -62,11 +69,9 @@ def main(config_path):
62
  text_output.markdown(f'<p style="font-size:20px"> Current gesture: {gesture}</p>',
63
  unsafe_allow_html=True)
64
  last_5_gestures.markdown(f'<p style="font-size:20px"> Last 5 gestures: {" ".join(gestures_deque)}</p>',
65
- unsafe_allow_html=True)
66
  print(gestures_deque)
67
 
68
-
69
-
70
  if __name__ == "__main__":
71
  asyncio.set_event_loop(asyncio.new_event_loop())
72
  main("configs/config.json")
 
2
  import queue
3
  from collections import deque
4
  import asyncio
5
+ import av
6
+ import cv2
7
+ import numpy as np
8
  import streamlit as st
9
  from streamlit_webrtc import WebRtcMode, webrtc_streamer
 
10
  from utils import SLInference
11
 
 
12
  logger = logging.getLogger(__name__)
13
 
14
  def main(config_path):
 
18
  inference_thread = SLInference(config_path)
19
  inference_thread.start()
20
 
 
 
 
 
 
 
21
  gestures_deque = deque(maxlen=5)
22
 
23
  # Set up Streamlit interface
24
+ st.set_page_config(page_title="Gesture Recognition", layout="wide")
25
  st.title("Sign Language Recognition Demo")
26
  image_place = st.empty()
27
  text_output = st.empty()
 
30
  """
31
  This application is designed to recognize sign language using a webcam feed.
32
  The model has been trained to recognize various sign language gestures and display the corresponding text in real-time.
 
33
  The project is open for collaboration. If you have any suggestions or want to contribute, please feel free to reach out.
34
  """
35
  )
36
 
37
+ result_queue = queue.Queue()
38
+
39
+ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
40
+ img_rgb = frame.to_ndarray(format="rgb24")
41
+ result_queue.put(img_rgb)
42
+ return frame
43
+
44
+ webrtc_ctx = webrtc_streamer(
45
+ key="sign-language-recognition",
46
+ mode=WebRtcMode.SENDRECV,
47
+ video_frame_callback=video_frame_callback,
48
+ media_stream_constraints={"video": True, "audio": False},
49
+ async_processing=True,
50
+ )
51
+
52
  while True:
53
+ if not webrtc_ctx.state.playing:
54
+ continue
55
+
56
+ if not result_queue.empty():
57
+ img_rgb = result_queue.get()
 
58
 
 
59
  image_place.image(img_rgb)
60
+ inference_thread.input_queue.append(cv2.resize(img_rgb, (224, 224)))
61
 
62
  gesture = inference_thread.pred
63
  if gesture not in ['no', '']:
 
69
  text_output.markdown(f'<p style="font-size:20px"> Current gesture: {gesture}</p>',
70
  unsafe_allow_html=True)
71
  last_5_gestures.markdown(f'<p style="font-size:20px"> Last 5 gestures: {" ".join(gestures_deque)}</p>',
72
+ unsafe_allow_html=True)
73
  print(gestures_deque)
74
 
 
 
75
  if __name__ == "__main__":
76
  asyncio.set_event_loop(asyncio.new_event_loop())
77
  main("configs/config.json")