VideoCapture can't open RTSP stream [FIXED]

I 'm working on a python script. In this scrypt I try to open my ip camera and try to perform a motion detection.
I have tested my script first with my webcam (cv2.VideoCapture(0)), everything works fine.

But when I’am using my ipcamera I get the following error:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’

please list the resulting materials you found when you searched for that error message.

What I have found is the following:
It can happen because the fuction returns a false, thus the frame is empty.
Possible solutions are to cover it by an if statement

if ret false:
cap = cv2.VideoCapture(‘IP camera’)
ret, frame =

This solution doesn’t work because ret is always false.
this keeps me in an endless loop

while True:
_, frame =
if cap.isOpened():
print(‘Cap is opened!’)

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5) #1.3 ,5
bodies = body_cascade.detectMultiScale(gray, 1.3, 5) #1.3 ,5

if len(faces) + len(bodies) > 0:
    if detection:
        timer_started = False
        detection = True
        current_time ="%d-%m-%Y-%H-%M-%S")
        out = cv2.VideoWriter(f"{current_time}.mp4", fourcc, 20, frame_size)
        print("Started Recording!")
elif detection:
    if timer_started:
        if time.time() - detection_stopped_time >= SECONDS_TO_RECORD_AFTER_DETECTION:
            detection = False
            timer_started = False
            print('Stop Recording!')
        timer_started = True
        detection_stopped_time = time.time()
if detection:

if cv2.waitKey(1) == ord('q'):


The stream that I am using is a rtsp FFMPEG stream. I hope this is enough information for you

print(cv2.getBuildInformation()), find the “video I/O” section, post it here. it needs to include ffmpeg or gstreamer. I don’t know if both support rtsp but at least one of them does.

this is not a valid url

Video I/O:
DC1394: NO
FFMPEG: YES (prebuilt binaries)
avcodec: YES (58.134.100)
avformat: YES (58.76.100)
avutil: YES (56.70.100)
swscale: YES (5.9.100)
avresample: YES (4.0.0)
GStreamer: NO
DirectShow: YES
Media Foundation: YES