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 cap.read fuction returns a false, thus the frame is empty.
Possible solutions are to cover it by an if statement

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

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

while True:
_, frame = cap.read()
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
    else:
        detection = True
        current_time = datetime.datetime.now().strftime("%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
            out.release()
            print('Stop Recording!')
    else: 
        timer_started = True
        detection_stopped_time = time.time()
if detection:
    out.write(frame)

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

out.release()
cap.release()
cv2.destroyAllWindows()

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
DXVA: YES