osheina commited on
Commit
96b7eb6
·
verified ·
1 Parent(s): 2525e3e

Update pages/Camera.py

Browse files
Files changed (1) hide show
  1. pages/Camera.py +57 -39
pages/Camera.py CHANGED
@@ -1,37 +1,52 @@
1
  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
- import av
9
  from utils import SLInference
10
 
11
 
12
  logger = logging.getLogger(__name__)
13
 
14
- def video_frame_callback(frame: av.VideoFrame, inference_thread, gestures_deque):
15
- img = frame.to_ndarray(format="rgb24")
16
-
17
- inference_thread.input_queue.append(img)
18
- gesture = inference_thread.pred
19
-
20
- if gesture not in ['no', '']:
21
- if not gestures_deque:
22
- gestures_deque.append(gesture)
23
- elif gesture != gestures_deque[-1]:
24
- gestures_deque.append(gesture)
25
-
26
- return av.VideoFrame.from_ndarray(img, format="rgb24")
27
 
28
- def main(config_path):
29
  """
30
  Main function of the app.
31
  """
32
- inference_thread = SLInference(config_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  inference_thread.start()
34
 
 
 
 
 
 
 
 
35
  gestures_deque = deque(maxlen=5)
36
 
37
  # Set up Streamlit interface
@@ -43,34 +58,37 @@ def main(config_path):
43
  """
44
  This application is designed to recognize sign language using a webcam feed.
45
  The model has been trained to recognize various sign language gestures and display the corresponding text in real-time.
46
-
 
47
  The project is open for collaboration. If you have any suggestions or want to contribute, please feel free to reach out.
48
  """
49
  )
50
 
51
- webrtc_ctx = webrtc_streamer(
52
- key="sign-language-recognition",
53
- mode=WebRtcMode.SENDRECV,
54
- video_frame_callback=lambda frame: video_frame_callback(frame, inference_thread, gestures_deque),
55
- media_stream_constraints={"video": True, "audio": False},
56
- async_processing=True,
57
- )
58
 
59
- while webrtc_ctx.state.playing:
60
- gesture = inference_thread.pred
 
61
 
62
- if gesture not in ['no', '']:
63
- if not gestures_deque:
64
- gestures_deque.append(gesture)
65
- elif gesture != gestures_deque[-1]:
66
- gestures_deque.append(gesture)
 
67
 
68
- text_output.markdown(f'<p style="font-size:20px"> Current gesture: {gesture}</p>',
69
- unsafe_allow_html=True)
70
- last_5_gestures.markdown(f'<p style="font-size:20px"> Last 5 gestures: {" ".join(gestures_deque)}</p>',
71
  unsafe_allow_html=True)
72
- print(gestures_deque)
 
 
73
 
74
  if __name__ == "__main__":
75
- asyncio.set_event_loop(asyncio.new_event_loop())
76
- main("configs/config.json")
 
1
  import logging
2
  import queue
3
  from collections import deque
4
+ import json
5
+ import tempfile
6
 
7
  import streamlit as st
8
+ from streamlit_webrtc import WebRtcMode, webrtc_streamer, RTCConfiguration
9
+
10
  from utils import SLInference
11
 
12
 
13
  logger = logging.getLogger(__name__)
14
 
15
+ RTC_CONFIGURATION = RTCConfiguration({
16
+ "iceServers": [
17
+ {"urls": ["stun:stun.l.google.com:19302"]},
18
+ {"urls": ["turn:TURN_SERVER_URL"], "username": "USERNAME", "credential": "CREDENTIAL"}
19
+ ]
20
+ })
 
 
 
 
 
 
 
21
 
22
+ def main():
23
  """
24
  Main function of the app.
25
  """
26
+ config = {
27
+ "path_to_model": "S3D.onnx",
28
+ "threshold": 0.3,
29
+ "topk": 5,
30
+ "path_to_class_list": "RSL_class_list.txt",
31
+ "window_size": 32,
32
+ "provider": "OpenVINOExecutionProvider"
33
+ }
34
+
35
+ # Сохранение конфигурации во временный файл
36
+ with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.json') as config_file:
37
+ json.dump(config, config_file)
38
+ config_file_path = config_file.name
39
+
40
+ inference_thread = SLInference(config_file_path)
41
  inference_thread.start()
42
 
43
+ webrtc_ctx = webrtc_streamer(
44
+ key="video-sendonly",
45
+ mode=WebRtcMode.SENDONLY,
46
+ rtc_configuration=RTC_CONFIGURATION,
47
+ media_stream_constraints={"video": True, "audio": False},
48
+ )
49
+
50
  gestures_deque = deque(maxlen=5)
51
 
52
  # Set up Streamlit interface
 
58
  """
59
  This application is designed to recognize sign language using a webcam feed.
60
  The model has been trained to recognize various sign language gestures and display the corresponding text in real-time.
61
+
62
+ This demo app is based on code here: https://github.com/ai-forever/easy_sign
63
  The project is open for collaboration. If you have any suggestions or want to contribute, please feel free to reach out.
64
  """
65
  )
66
 
67
+ while True:
68
+ if webrtc_ctx.video_receiver:
69
+ try:
70
+ video_frame = webrtc_ctx.video_receiver.get_frame(timeout=1)
71
+ except queue.Empty:
72
+ logger.warning("Queue is empty")
73
+ continue
74
 
75
+ img_rgb = video_frame.to_ndarray(format="rgb24")
76
+ image_place.image(img_rgb)
77
+ inference_thread.input_queue.append(video_frame.reformat(224, 224).to_ndarray(format="rgb24"))
78
 
79
+ gesture = inference_thread.pred
80
+ if gesture not in ['no', '']:
81
+ if not gestures_deque:
82
+ gestures_deque.append(gesture)
83
+ elif gesture != gestures_deque[-1]:
84
+ gestures_deque.append(gesture)
85
 
86
+ text_output.markdown(f'<p style="font-size:20px"> Current gesture: {gesture}</p>',
 
 
87
  unsafe_allow_html=True)
88
+ last_5_gestures.markdown(f'<p style="font-size:20px"> Last 5 gestures: {" ".join(gestures_deque)}</p>',
89
+ unsafe_allow_html=True)
90
+ print(gestures_deque)
91
 
92
  if __name__ == "__main__":
93
+ main()
94
+