2

Before asking this question I searched the site for a similar problem for the last 2 days but I couldn't find the one specific to me.

I have an IP camera whose IP address, username, etc. I have been given full access to. I can actually open the stream and watch it live by writing the IP into VLC Player >> Open Network Stream >> Network.

But what I want is to be able to watch the same live stream with python. Here's my code:

import urllib.request
import cv2
import numpy

url = 'rtsp://10.10.111.200/profile2/media.smp'
while True:
    resp = urllib.request.urlopen(url)
    b_array = bytearray(resp.read())
    img_np = numpy.array(b_array, dtype=numpy.uint8)
    img = cv2.imdecode(img_np, -1)
    cv2.imshow('test', img)
    if cv2.waitkey(10) == ord('q'):
        exit(0)

When I run this code, it gives me the following error:

urllib.error.URLError: .

Then I figured that maybe I should change rtsp to http in url, but when I do, it gives me the following error,

cv2.error: OpenCV(3.4.3) D:\Build\OpenCV\opencv-3.4.3\modules\imgcodecs\src\loadsave.cpp:737: error: (-215:Assertion failed) !buf.empty() && buf.isContinuous() in function 'cv::imdecode_' in the line img = cv2.imdecode(img_np, -1)

which I think is because there's no data coming from the (very likely wrong since I changed to http) source.

I'm on Windows 10 64bit.

1 Answer 1

1

The library you are using to read the data stream does not support the rtsp protocol so it will never work, maybe you can use the following instead:

import cv2

capture_video = cv2.VideoCapture('rtsp://10.10.111.200/profile2/media.smp')
while(True):
    ret, img = capture_video.read()
    cv2.imshow('Test', img)
    if cv2.waitKey(10) == ord('q'):
        exit(0)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer and thanks for the edit. It works. But it takes a lot of time to start and once it starts, it freezes constantly. Can you suggest something to speed up the process (of bringing frames)?
I tried the same code with this demo stream rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov and it is working fine so if it freezes for you maybe it could be a network issue?
Also, check this example, it uses several threads to increase the FPS rate: pyimagesearch.com/2015/12/21/…
I also thought it was a network issue at first but network seems fine. I'm gonna use threading to try to boost the FPS rate, though. Thanks a lot.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.