Hi I'm just trying to run YOLO11 object detection using opencv's cv2.VideoCapture(source) with source = 'http://{ip_address}:8889/cam1' . I modified the mediamtx.yml file according to a YouTube tutorial :- https://www.youtube.com/watch?v=rxtcyxV32nc adding this bit at the end as he says to :- . I can access the stream from my web browser, however when I try to access it in pycharm using opencv I get errors saying unable to connect and access the stream. my python code is as follows:- however I get the following errors:- [tcp @ 0x60000251eb00] Connection to tcp://{ip_address}:8889 failed: No route to host
OpenCV: Couldn't read video stream from file "http://{ip_address}:8889/cam1"
Thanks for all the help and advice.
Code:
cam1:runnInit: bash -c 'rpicam-vid -t 0 --camera 0 -nopreview —-codec yuv420 -width 1280 -height 720 -inline -listen -0 - | - ffmpeg -f rawvideo -pix_fmt yuv420p -s:v -i /dev/stdin -c:v libx264-preset ultrafast -tune zerolatency -f rtsp rtsp://localhost: $RTSP_PORT/$MTX_PATH'runOnInitRestart: yesCode:
import mathimport cv2from ultralytics import YOLOimport ultralytics.utils.plotting as plotting#import motioncontrolimport numpy as npfrom collections import defaultdict# Load a pretrained YOLOv11n modelmodel = YOLO("yolo11n.pt")source = 'http://{ipaddress}:8889/cam1'cap = cv2.VideoCapture(source)#colors= plotting.colors()track_history = defaultdict(lambda: [])distance=[]while cap.isOpened(): success, frame = cap.read() if success: results = model.track(frame, persist=True) for result in results: xyxy = result.boxes.xyxy.cpu().numpy() xywh=result.boxes.xywh.cpu().numpy() for box in xywh: if xywh.size >0: raw_centre_x = (box[0]).astype(int) #centre_x = raw_centre_x.astype(int) raw_centre_y = (box[1]).astype(int) #centre_y = raw_centre_y.astype(int) # Draw circles at the center coordinates cv2.circle(frame, (raw_centre_x,raw_centre_y), 5, color=(255, 255, 255), thickness=2) #try to use normalized coordinate values that way you can very simply use the sign to tell wether the object is to the right or to the left. # Centre of frame is 640 width and 360 height cv2.circle(frame, (640,360), 5, color=(255, 27, 108), thickness=2) annotated_frame = results[0].plot() cv2.imshow("YOLO11 Tracking", annotated_frame) if cv2.waitKey(1) & 0xFF == ord("q"): break else: breakcap.release()cv2.destroyAllWindows()OpenCV: Couldn't read video stream from file "http://{ip_address}:8889/cam1"
Thanks for all the help and advice.
Statistics: Posted by Joraspberry — Sun Aug 03, 2025 1:14 pm