Trouble opening video stream in Python using Axis 212 PTZ IP camera and openCV

asked 2020-03-11 11:59:34 -0500

updated 2020-03-11 21:34:28 -0500

supra56 gravatar image

I have been trying for some time now to connect my Axis 212 PTZ IP camera to a Python window using the openCV package, but so far have been unsuccessful. Most examples i have found to this point use the following code, specifically the .VideoCapture function with the URL of the camera to call it into python and subsequently display it.

import cv2

# cap = cv2.VideoCapture('rtsp://')
# cap = cv2.VideoCapture('http://username_password@')
# cap = cv2.VideoCapture("rtsp://root:root@")
# cap = cv2.VideoCapture("")
# cap = cv2.VideoCapture("http://root:root@")
# cap = cv2.VideoCapture("")
# cap = cv2.VideoCapture("http://root:root@")
# cap = cv2.VideoCapture("http://root:root@")
# cap =
# cap = cv2.VideoCapture(1)
# cap = cv2.VideoCapture("http://root:root@")
# cap = cv2.VideoCapture("http://%IP%/axis-cgi/mjpg/video.cgi?camera=%C%")
# cap = cv2.VideoCapture("axrtpu://%IP%/axis-media/media.amp?resolution=4CIF&fps=15&videocodec=jpeg")
# cap = cv2.VideoCapture(" rtsp://%IP%/axis-media/media.amp?resolution=4CIF&fps=15&videocodec=h264")
# cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture("")

    ret, frame =

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

When i run this code, i get the following error message:

Traceback (most recent call last): File "C:/ABWorkingFiles/Kairos/2020_Mar1/", line 25, in cv2.imshow('frame',frame) cv2.error: **OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' [tcp @ 00bd9dc0] Connection to tcp:// failed: Error number -138 occurred**

The error occurs on the line with .imshow(); breakpoints show this variable is null, which leads me to believe the .VideoCapture line of code is not accessing the video data stream and bringing it into python to be displayed.

I have tried using rtsp instead of http, i have tried using the set username and password on the device, i have also tried calling in a /video as well as a /mjpeg, and so far i have not been able to connect the video stream to python. It will run in a browser window when i enter the URL, and i was even able to capture a single still image with a different sample of code, so it is communicating properly, but still no video.

Any help on this would be greatly appreciated, i am very new to openCV and i feel i may be missing something simple in my syntax is all. I felt i was pretty specific with my issue, but if i need to be any more specific just let me know.

Thank you!

edit retag flag offensive close merge delete


please check cap.isOpened() before entering your loop

berak gravatar imageberak ( 2020-03-13 05:44:13 -0500 )edit