whitphx HF staff commited on
Commit
b744975
·
1 Parent(s): 50e8759

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -12
app.py CHANGED
@@ -81,7 +81,10 @@ def download_file(url, download_to: Path, expected_size=None):
81
 
82
  WEBRTC_CLIENT_SETTINGS = ClientSettings(
83
  rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
84
- media_stream_constraints={"video": True, "audio": True},
 
 
 
85
  )
86
 
87
 
@@ -104,6 +107,7 @@ def main():
104
  "WebRTC is sendonly and audio frames are visualized with matplotlib (sendonly)"
105
  )
106
  loopback_page = "Simple video and audio loopback (sendrecv)"
 
107
  app_mode = st.sidebar.selectbox(
108
  "Choose the app mode",
109
  [
@@ -115,6 +119,7 @@ def main():
115
  video_sendonly_page,
116
  audio_sendonly_page,
117
  loopback_page,
 
118
  ],
119
  )
120
  st.subheader(app_mode)
@@ -135,6 +140,8 @@ def main():
135
  app_sendonly_audio()
136
  elif app_mode == loopback_page:
137
  app_loopback()
 
 
138
 
139
  logger.debug("=== Alive threads ===")
140
  for thread in threading.enumerate():
@@ -441,35 +448,43 @@ def app_object_detection():
441
  def app_streaming():
442
  """ Media streamings """
443
  MEDIAFILES = {
444
- "big_buck_bunny_720p_2mb.mp4": {
445
  "url": "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_2mb.mp4", # noqa: E501
446
  "local_file_path": HERE / "data/big_buck_bunny_720p_2mb.mp4",
447
  "type": "video",
448
  },
449
- "big_buck_bunny_720p_10mb.mp4": {
450
  "url": "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_10mb.mp4", # noqa: E501
451
  "local_file_path": HERE / "data/big_buck_bunny_720p_10mb.mp4",
452
  "type": "video",
453
  },
454
- "file_example_MP3_700KB.mp3": {
455
  "url": "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3", # noqa: E501
456
  "local_file_path": HERE / "data/file_example_MP3_700KB.mp3",
457
  "type": "audio",
458
  },
459
- "file_example_MP3_5MG.mp3": {
460
  "url": "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_5MG.mp3", # noqa: E501
461
  "local_file_path": HERE / "data/file_example_MP3_5MG.mp3",
462
  "type": "audio",
463
  },
 
 
 
 
464
  }
465
  media_file_label = st.radio(
466
- "Select a media file to stream", tuple(MEDIAFILES.keys())
467
  )
468
  media_file_info = MEDIAFILES[media_file_label]
469
- download_file(media_file_info["url"], media_file_info["local_file_path"])
 
470
 
471
  def create_player():
472
- return MediaPlayer(str(media_file_info["local_file_path"]))
 
 
 
473
 
474
  # NOTE: To stream the video from webcam, use the code below.
475
  # return MediaPlayer(
@@ -478,6 +493,49 @@ def app_streaming():
478
  # options={"framerate": "30", "video_size": "1280x720"},
479
  # )
480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
481
  WEBRTC_CLIENT_SETTINGS.update(
482
  {
483
  "media_stream_constraints": {
@@ -487,11 +545,23 @@ def app_streaming():
487
  }
488
  )
489
 
490
- webrtc_streamer(
491
  key=f"media-streaming-{media_file_label}",
492
  mode=WebRtcMode.RECVONLY,
493
  client_settings=WEBRTC_CLIENT_SETTINGS,
494
  player_factory=create_player,
 
 
 
 
 
 
 
 
 
 
 
 
495
  )
496
 
497
 
@@ -499,7 +569,7 @@ def app_sendonly_video():
499
  """A sample to use WebRTC in sendonly mode to transfer frames
500
  from the browser to the server and to render frames via `st.image`."""
501
  webrtc_ctx = webrtc_streamer(
502
- key="loopback",
503
  mode=WebRtcMode.SENDONLY,
504
  client_settings=WEBRTC_CLIENT_SETTINGS,
505
  )
@@ -524,9 +594,9 @@ def app_sendonly_video():
524
  def app_sendonly_audio():
525
  """A sample to use WebRTC in sendonly mode to transfer audio frames
526
  from the browser to the server and visualize them with matplotlib
527
- and `st.pyplog`."""
528
  webrtc_ctx = webrtc_streamer(
529
- key="loopback",
530
  mode=WebRtcMode.SENDONLY,
531
  audio_receiver_size=256,
532
  client_settings=WEBRTC_CLIENT_SETTINGS,
@@ -599,6 +669,24 @@ def app_sendonly_audio():
599
  break
600
 
601
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
602
  if __name__ == "__main__":
603
  import os
604
 
 
81
 
82
  WEBRTC_CLIENT_SETTINGS = ClientSettings(
83
  rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
84
+ media_stream_constraints={
85
+ "video": True,
86
+ "audio": True,
87
+ },
88
  )
89
 
90
 
 
107
  "WebRTC is sendonly and audio frames are visualized with matplotlib (sendonly)"
108
  )
109
  loopback_page = "Simple video and audio loopback (sendrecv)"
110
+ media_constraints_page = "Configure media constraints with loopback (sendrecv)"
111
  app_mode = st.sidebar.selectbox(
112
  "Choose the app mode",
113
  [
 
119
  video_sendonly_page,
120
  audio_sendonly_page,
121
  loopback_page,
122
+ media_constraints_page,
123
  ],
124
  )
125
  st.subheader(app_mode)
 
140
  app_sendonly_audio()
141
  elif app_mode == loopback_page:
142
  app_loopback()
143
+ elif app_mode == media_constraints_page:
144
+ app_media_constraints()
145
 
146
  logger.debug("=== Alive threads ===")
147
  for thread in threading.enumerate():
 
448
  def app_streaming():
449
  """ Media streamings """
450
  MEDIAFILES = {
451
+ "big_buck_bunny_720p_2mb.mp4 (local)": {
452
  "url": "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_2mb.mp4", # noqa: E501
453
  "local_file_path": HERE / "data/big_buck_bunny_720p_2mb.mp4",
454
  "type": "video",
455
  },
456
+ "big_buck_bunny_720p_10mb.mp4 (local)": {
457
  "url": "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_10mb.mp4", # noqa: E501
458
  "local_file_path": HERE / "data/big_buck_bunny_720p_10mb.mp4",
459
  "type": "video",
460
  },
461
+ "file_example_MP3_700KB.mp3 (local)": {
462
  "url": "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3", # noqa: E501
463
  "local_file_path": HERE / "data/file_example_MP3_700KB.mp3",
464
  "type": "audio",
465
  },
466
+ "file_example_MP3_5MG.mp3 (local)": {
467
  "url": "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_5MG.mp3", # noqa: E501
468
  "local_file_path": HERE / "data/file_example_MP3_5MG.mp3",
469
  "type": "audio",
470
  },
471
+ "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov": {
472
+ "url": "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov",
473
+ "type": "video",
474
+ },
475
  }
476
  media_file_label = st.radio(
477
+ "Select a media source to stream", tuple(MEDIAFILES.keys())
478
  )
479
  media_file_info = MEDIAFILES[media_file_label]
480
+ if "local_file_path" in media_file_info:
481
+ download_file(media_file_info["url"], media_file_info["local_file_path"])
482
 
483
  def create_player():
484
+ if "local_file_path" in media_file_info:
485
+ return MediaPlayer(str(media_file_info["local_file_path"]))
486
+ else:
487
+ return MediaPlayer(media_file_info["url"])
488
 
489
  # NOTE: To stream the video from webcam, use the code below.
490
  # return MediaPlayer(
 
493
  # options={"framerate": "30", "video_size": "1280x720"},
494
  # )
495
 
496
+ class OpenCVVideoProcessor(VideoProcessorBase):
497
+ type: Literal["noop", "cartoon", "edges", "rotate"]
498
+
499
+ def __init__(self) -> None:
500
+ self.type = "noop"
501
+
502
+ def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
503
+ img = frame.to_ndarray(format="bgr24")
504
+
505
+ if self.type == "noop":
506
+ pass
507
+ elif self.type == "cartoon":
508
+ # prepare color
509
+ img_color = cv2.pyrDown(cv2.pyrDown(img))
510
+ for _ in range(6):
511
+ img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
512
+ img_color = cv2.pyrUp(cv2.pyrUp(img_color))
513
+
514
+ # prepare edges
515
+ img_edges = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
516
+ img_edges = cv2.adaptiveThreshold(
517
+ cv2.medianBlur(img_edges, 7),
518
+ 255,
519
+ cv2.ADAPTIVE_THRESH_MEAN_C,
520
+ cv2.THRESH_BINARY,
521
+ 9,
522
+ 2,
523
+ )
524
+ img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)
525
+
526
+ # combine color and edges
527
+ img = cv2.bitwise_and(img_color, img_edges)
528
+ elif self.type == "edges":
529
+ # perform edge detection
530
+ img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
531
+ elif self.type == "rotate":
532
+ # rotate image
533
+ rows, cols, _ = img.shape
534
+ M = cv2.getRotationMatrix2D((cols / 2, rows / 2), frame.time * 45, 1)
535
+ img = cv2.warpAffine(img, M, (cols, rows))
536
+
537
+ return av.VideoFrame.from_ndarray(img, format="bgr24")
538
+
539
  WEBRTC_CLIENT_SETTINGS.update(
540
  {
541
  "media_stream_constraints": {
 
545
  }
546
  )
547
 
548
+ webrtc_ctx = webrtc_streamer(
549
  key=f"media-streaming-{media_file_label}",
550
  mode=WebRtcMode.RECVONLY,
551
  client_settings=WEBRTC_CLIENT_SETTINGS,
552
  player_factory=create_player,
553
+ video_processor_factory=OpenCVVideoProcessor,
554
+ )
555
+
556
+ if webrtc_ctx.video_processor:
557
+ webrtc_ctx.video_processor.type = st.radio(
558
+ "Select transform type", ("noop", "cartoon", "edges", "rotate")
559
+ )
560
+
561
+ st.markdown(
562
+ "The video filter in this demo is based on "
563
+ "https://github.com/aiortc/aiortc/blob/2362e6d1f0c730a0f8c387bbea76546775ad2fe8/examples/server/server.py#L34. " # noqa: E501
564
+ "Many thanks to the project."
565
  )
566
 
567
 
 
569
  """A sample to use WebRTC in sendonly mode to transfer frames
570
  from the browser to the server and to render frames via `st.image`."""
571
  webrtc_ctx = webrtc_streamer(
572
+ key="video-sendonly",
573
  mode=WebRtcMode.SENDONLY,
574
  client_settings=WEBRTC_CLIENT_SETTINGS,
575
  )
 
594
  def app_sendonly_audio():
595
  """A sample to use WebRTC in sendonly mode to transfer audio frames
596
  from the browser to the server and visualize them with matplotlib
597
+ and `st.pyplot`."""
598
  webrtc_ctx = webrtc_streamer(
599
+ key="sendonly-audio",
600
  mode=WebRtcMode.SENDONLY,
601
  audio_receiver_size=256,
602
  client_settings=WEBRTC_CLIENT_SETTINGS,
 
669
  break
670
 
671
 
672
+ def app_media_constraints():
673
+ """ A sample to configure MediaStreamConstraints object """
674
+ frame_rate = 5
675
+ WEBRTC_CLIENT_SETTINGS.update(
676
+ ClientSettings(
677
+ media_stream_constraints={
678
+ "video": {"frameRate": {"ideal": frame_rate}},
679
+ },
680
+ )
681
+ )
682
+ webrtc_streamer(
683
+ key="media-constraints",
684
+ mode=WebRtcMode.SENDRECV,
685
+ client_settings=WEBRTC_CLIENT_SETTINGS,
686
+ )
687
+ st.write(f"The frame rate is set as {frame_rate}")
688
+
689
+
690
  if __name__ == "__main__":
691
  import os
692