Your issue is that the video capture failed to start. As a result, you’re reading void frames.
For a rtsp stream you would need gstreamer or FFMPEG support in opencv.
Try:
import cv2
# Check for gstreamer or FFMPEG support from opencv
import re
print('GStreamer support: %s' % re.search(r'GStreamer\:\s+(.*)', cv2.getBuildInformation()).group(1))
print('FFMPEG support: %s' % re.search(r'FFMPEG\:\s+(.*)', cv2.getBuildInformation()).group(1))
# Create capture (you would edit for setting your RTSP stream URI)
# Using Gstreamer
cap = cv2.VideoCapture('uridecodebin uri=rtsp://127.0.0.1:8554/test ! queue ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! queue ! appsink drop=1', cv2.CAP_GSTREAMER)
# Using FFMPEG
#cap = cv2.VideoCapture('rtsp://127.0.0.1:8554/test', cv2.CAP_FFMPEG);
# Always test that capture is successfully created and ready:
if not cap.isOpened():
print('Failed to open capture');
exit(-1)
# Get some properties of the video stream
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
print('capture opened, framing %dx%d@%f fps' % (w,h,fps))
# Loop reading frame and displaying
while True:
ret,frame = cap.read()
if not ret:
print('Failed to read from capture')
cap.release()
exit(-2)
cv2.imshow('Test', frame)
cv2.waitKey(1)
cap.release()