Hi. I’m new with streamlit-webrtc. I used to deploy face recognition model with cv2 but streamlit doesn’t fit well with cv2 so I decided to move to streamlit-webrtc. My laptop doesn’t have camera so I have to connect cv2 with droidcam and it works ok but when I migrate to streamlit-webrtc I don’t find out any document about connecting between droidcam and streamlit-webrtc.
Can you give me some way to connect? Thanks.
Hello @hungpham3112 ,
To connect DroidCam with Streamlit-WebRTC, you can use the cv2
library in combination with the streamlit-webrtc
library, follow the below steps:
- Start DroidCam on your mobile device and ensure that your mobile device and laptop are connected to the same network.
- Run the following code in a Python file (e.g.,
app.py
) using Streamlit:
import cv2
import numpy as np
import streamlit as st
import streamlit_webrtc as webrtc
def main():
st.title(“DroidCam Stream”)
# Create a VideoTransformer class that handles video frames
class VideoTransformer(webrtc.VideoTransformerBase):
def transform(self, frame):
# Convert the frame to RGB format
img_rgb = cv2.cvtColor(frame.to_ndarray(), cv2.COLOR_BGR2RGB)
# Process the frame (e.g., perform face recognition, etc.)
# ...
# Return the processed frame in BGR format
return cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
# Configure the WebRTC stream
webrtc_ctx = webrtc.Streamer(
key="example",
video_transformer_factory=VideoTransformer,
async_transform=True,
)
# Connect to the DroidCam video stream
cap = cv2.VideoCapture('http://your_droidcam_ip:port/video')
# Start the Streamlit app
while webrtc_ctx.video_transformer:
# Read a frame from the DroidCam video stream
ret, frame = cap.read()
if not ret:
break
# Push the frame to the Streamlit-WebRTC app
webrtc_ctx.video_transformer.frame_queue.put(frame)
# Cleanup resources
cap.release()
if name == “main”:
main()
I hope your doubt gets resolved!
Hi. thanks for your supporting, but when I use your code, it raised
AttributeError: module 'streamlit_webrtc' has no attribute 'Streamer'
. It seems some update in new streamlit_webrtc accroding to streamlit_rtc README
For users since versions
<0.20
VideoTransformerBase
and itstransform
method have been marked as deprecated in v0.20.0. Please useVideoProcessorBase#recv()
instead. Note that the signature of therecv
method is different from thetransform
in that therecv
has to return an instance ofav.VideoFrame
orav.AudioFrame
.Also,
webrtc_streamer()
'svideo_transformer_factory
andasync_transform
arguments are deprecated, so usevideo_processor_factory
andasync_processing
respectively.See the samples in app.py for their usage.