import streamlit as st from streamlitwebrtc import webrtcstreamer, VideoProcessorBase,…
- User
import streamlit as st from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, RTCConfiguration import av import threading
RTC_CONFIGURATION = RTCConfiguration( {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]} )
st.set_page_config(page_title="Streamlit WebRTC Demo", page_icon="🤖") task_list = ["Video Stream"]
with st.sidebar: st.title('Task Selection') task_name = st.selectbox("Select your tasks:", task_list) st.title(task_name)
if task_name == task_list[0]: style_list = ['color', 'black and white']
st.sidebar.header('Style Selection') style_selection = st.sidebar.selectbox("Choose your style:", style_list) class VideoProcessor(VideoProcessorBase): def __init__(self): self.model_lock = threading.Lock() self.style = style_list[0] def update_style(self, new_style): if self.style != new_style: with self.model_lock: self.style = new_style def recv(self, frame): # img = frame.to_ndarray(format="bgr24") img = frame.to_image() if self.style == style_list[1]: img = img.convert("L") # return av.VideoFrame.from_ndarray(img, format="bgr24") return av.VideoFrame.from_image(img) ctx = webrtc_streamer( key="example", video_processor_factory=VideoProcessor, rtc_configuration=RTC_CONFIGURATION, media_stream_constraints={ "video": True, "audio": False } ) if ctx.video_processor: ctx.video_transformer.update_style(style_selection)'''
上記コードを、映像ではなく配信側PCのシステム音声のみを配信するよう改修し、改修したコード全文を表示してください
- Assistant
This code only streams the computer's system audio and does not stream video:
import streamlit as st from streamlit_webrtc import webrtc_streamer, AudioManagerBase, RTCConfiguration import threading RTC_CONFIGURATION = RTCConfiguration( {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]} ) st.set_page_config(page_title="Streamlit WebRTC Demo", page_icon="🤖") task_list = ["Audio Stream"] with st.sidebar: st.title('Task Selection') task_name = st.selectbox("Select your tasks:", task_list) st.title(task_name) if task_name == task_list[0]: class AudioManager(AudioManagerBase): def __init__(self): self.lock = threading.Lock() def recv(self, frame: av.AudioFrame): with self.lock: return frame ctx = webrtc_streamer( key="example", audio_processor_factory=AudioManager, rtc_configuration=RTC_CONFIGURATION, media_stream_constraints={ "video": False, "audio": True } )Please note that the audio streaming capabilities provided by
streamlit_webrtcmight not work correctly in all browsers and environments. - User
上記のコードを実行すると、下記のエラーが表示されます。説明部分は省略して、インデントを付与して改修部分のコードのみ表示してください
'''
Traceback (most recent call last): File "/home/walhalax/anaconda3/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script exec(code, module.dict) File "/home/walhalax/PycharmProjects/pythonProject/その他/AudioStreaming/audiostreaming.py", line 2, in from streamlit_webrtc import webrtc_streamer, AudioManagerBase, RTCConfiguration ImportError: cannot import name 'AudioManagerBase' from 'streamlit_webrtc' (/home/walhalax/anaconda3/lib/python3.9/site-packages/streamlit_webrtc/init.py)
- Assistant
Apologies for the confusion. It seems
AudioManagerBaseis not present in thestreamlit_webrtcpackage. Instead, you can useAudioProcessorBaseto achieve audio streaming. Here's the updated code snippet:import streamlit as st from streamlit_webrtc import webrtc_streamer, AudioProcessorBase, RTCConfiguration import threading RTC_CONFIGURATION = RTCConfiguration( {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]} ) st.set_page_config(page_title="Streamlit WebRTC Demo", page_icon="🤖") task_list = ["Audio Stream"] with st.sidebar: st.title('Task Selection') task_name = st.selectbox("Select your tasks:", task_list) st.title(task_name) if task_name == task_list[0]: class AudioManager(AudioProcessorBase): def __init__(self): self.lock = threading.Lock() def recv(self, frame: av.AudioFrame): with self.lock: return frame ctx = webrtc_streamer( key="example", audio_processor_factory=AudioManager, rtc_configuration=RTC_CONFIGURATION, media_stream_constraints={ "video": False, "audio": True } )