简体   繁体   中英

How to view video stream in OpenCV2 python

I'm starting to play with Opencv. I am using the python bindings for opencv2 on Linux. I wrote a quick test program but it seems to hang indefinitely.

import cv2

weblink = "http://continuous-video-stream-here"
cv2.namedWindow("video")

vid = cv2.VideoCapture(weblink)
key = -1

while (key < 0):
    success, img = vid.read()
    cv2.imshow("video", img)

But it hangs on this output:

(video:14388): GStreamer-CRITICAL **: gst_caps_unref: assertion `caps != NULL' failed

I have also tried reading from urllib2:

vid = cv2.VideoCapture(urllib2.urlopen(weblink).read())

But that didn't work either.

I am using Opencv 2.4.2, ffmpeg-0.11.2

EDIT : The video feed uses realplayer to display the video over http in the browser.

Code safely and test the return of the method:

vid = cv2.VideoCapture(weblink)
if not vid:
    print("!!! Failed VideoCapture: invalid parameter!")

The address you are using is probably not supported by OpenCV.

The same practice should be used whenever a method can fail:

while (key < 0):
    success, img = vid.read()
    if not img:
        print("!!! Failed vid.read()")
        break

    cv2.imshow("video", img)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM