OpenCV从URL加载视频 [英] OpenCV load video from url

查看:676
本文介绍了OpenCV从URL加载视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视频文件(即https://www.example.com/myvideo.mp4),需要用OpenCV加载它.

I have a video file (i.e. https://www.example.com/myvideo.mp4) and need to load it with OpenCV.

对图像进行等效处理相当简单:

Doing the equivalent with an image is fairly trivial:

imgReq = requests.get("https://www.example.com/myimage.jpg")
imageBytes = np.asarray(bytearray(data), dtype=np.uint8)
loadedImage = cv2.imdecode(image, cv2.IMREAD_COLOR)

我想做类似以下的事情(其中loadedVideo将类似于OpenCV从cv2.VideoCapture返回的内容)

I would like to do something similar to the following (where loadedVideo will be similar to what OpenCV returns from cv2.VideoCapture):

videoReq = requests.get("https://www.example.com/myimage.mp4")
videoBytes = np.asarray(bytearray(data), dtype=np.uint8)
loadedVideo = cv2.videodecode(image, cv2.IMREAD_COLOR)

但是cv2.videodecode不存在.有什么想法吗?

But cv2.videodecode does not exist. Any ideas?

鉴于只有OpenCV可能是一个死胡同,因此我对在将其他映像库加载到OpenCV之前结合其他映像库的解决方案表示欢迎. >

Seeing as this may be a dead end with only OpenCV, I'm open for solutions that combine other imaging libraries before loading into OpenCV...if such a solution exists.

推荐答案

OpenCV 2.xOpenCV 3.x中,看来cv2.videocode都不是有效的OpenCV API.

It seems that cv2.videocode is not a valid OpenCV API either in OpenCV 2.x or OpenCV 3.x.

下面是一个示例代码,它在使用cv2.VideoCapture类的OpenCV 3中工作.

Below is a sample code it works in OpenCV 3 which uses cv2.VideoCapture class.

import numpy as np
import cv2

# Open a sample video available in sample-videos
vcap = cv2.VideoCapture('https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_2mb.mp4')
#if not vcap.isOpened():
#    print "File Cannot be Opened"

while(True):
    # Capture frame-by-frame
    ret, frame = vcap.read()
    #print cap.isOpened(), ret
    if frame is not None:
        # Display the resulting frame
        cv2.imshow('frame',frame)
        # Press q to close the video windows before it ends if you want
        if cv2.waitKey(22) & 0xFF == ord('q'):
            break
    else:
        print "Frame is None"
        break

# When everything done, release the capture
vcap.release()
cv2.destroyAllWindows()
print "Video stop"

您可以查看视频入门教程更多信息.

You may check this Getting Started with Videos tutorial for more information.

希望获得帮助.

这篇关于OpenCV从URL加载视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆