3

I'm trying to connect to an IP Camera (Dahua) using OpenCV and Python using an RTSP URI. I added a sample code below (with the username, password and ip provided in separate variables), which results in [rtsp @ 0x55cc3715c600] method OPTIONS failed: 401 Unauthorized. Using VLC, I'm able to connect, however only after entering the password in a separate dialog even though the password was already provided in the URI.

How should I continue?

address = f'rtsp://{username}:{password}@{ip}'
cap = cv2.VideoCapture(address)
5
  • From my IP camera, I simply format the RTSP URI like this and throw it into cv2.VideoCapture(). I'm also using a Dahua camera but the RTSP link will vary from camera to camera. Here's an example of mine rtsp://username:[email protected]/axis-media/media.amp. If the link works in VLC it should work in OpenCV
    – nathancy
    Commented Nov 22, 2019 at 20:44
  • In VLC I am only prompted in a separate dialog for the password if the received password is incorrect. Try hardcoding the address to what it should be and see if it still gives you problems. Commented Nov 23, 2019 at 5:19
  • Also if you compile opencv with gstreamer you can use any gstreamer pipeline, here is one I use: vc = cv2.VideoCapture(' rtspsrc location=rtsp://user:[email protected]:8554/play1.sdp ! decodebin ! videoconvert ! appsink ') You should be able to test that this pipeline works outside of opencv with gst-launch as follows: gst-launch-1.0 rtspsrc location=rtsp://user:[email protected]:8554/play1.sdp ! decodebin ! videoconvert ! autovideosink Commented Nov 23, 2019 at 5:19
  • rtsp://username:[email protected] Is the format I use, however, it leads to the prompt in VLC, where I have to enter the password again. Perhaps it has something to do with special characters in the password name: % ?
    – sPY4CN
    Commented Nov 23, 2019 at 15:33
  • Using the gst-launch-1.0 command also results in an Unauthorized (401) error.
    – sPY4CN
    Commented Nov 23, 2019 at 15:40

2 Answers 2

3

Found the problem, it had to do with the password having a special character (%), which should be percent encoded (%25).

0

If your password contains characters like !"#$%&'()*+,-./:;<=>?@[\]^_{|}~, then the problem may be with cv2.VideoCapture().

You may try to use imutils library to solve this problem. That works for me.

from imutils.video import VideoStream
cap = VideoStream(video_src).start()
frame = cap.read()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.