I’m trying to integrate an IP camera feed into a Gradio interface instead of using the default laptop camera. Despite my attempts, the interface continues to default to the laptop camera. Below are the code snippets I’ve been working with:
api.py:
import cv2
rtsp_url = "rtsp://admin:********@192.168.*.**:554/stream1"
cap = cv2.VideoCapture(rtsp_url)
if not cap.isOpened():
print('can not open rtsp stream')
exit()
> for i in range(100):
ret, frame = cap.read()
if not ret:
print('can not get image')
break
# GET FRAME
cv2.imshow('rtsp stream', frame)
if cv2.waitKey(1) and 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
demo.py:
import cv2
import gradio as gr
from PIL import Image
def ipcam_stream(_):
rtsp_url = "rtsp://admin:********@192.168.*.**:554/stream1"
cap = cv2.VideoCapture(rtsp_url)
if not cap.isOpened():
print('can not open rtsp stream')
return None
while True:
ret, frame = cap.read()
if not ret:
print('can not get image')
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(frame)
yield image
iface = gr.Interface(
fn=ipcam_stream,
inputs="image",
outputs="image",
live=True
)
iface.launch()
I have made sure that the RTSP URL in both api.py
and demo.py
files points to the correct IP camera feed. However, when running the Gradio interface, it still defaults to the laptop camera. I have tried updating the input parameter to "webcam"
, but with no success.
Any guidance on how to properly call the IP camera connected via Ethernet in the Gradio interface would be greatly appreciated. Thank you in advance for your help!
1