OpenCV 从 url 加载视频 [英] OpenCV load video from 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.
推荐答案
cv2.videocode
似乎在 OpenCV 2 中都不是有效的
或 OpenCV API
.xOpenCV 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屋!