NB1991
NB1991

Reputation: 33

Problem streaming from IP camera with python openCV

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.

Upvotes: 2

Views: 2740

Answers (1)

Isma
Isma

Reputation: 15209

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)

Upvotes: 1

Related Questions