This link有一个简洁的小例子,说明如何使用python的OpenCV库cv2
将数据从摄像头传输到你的python shell中。我希望进行一些实验,并希望使用以下YouTube视频Feed:https://www.youtube.com/watch?v=oCUqsPLvYBQ
。
我尝试按如下方式调整示例:
import numpy as np
import cv2
cap = cv2.VideoCapture('https://www.youtube.com/watch?v=oCUqsPLvYBQ')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
产生错误:
WARNING: Couldn't read movie file https://www.youtube.com/watch?v=oCUqsPLvYBQ
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/opencv20160107-29960-t5glvv/opencv-2.4.12/modules/highgui/src/window.cpp, line 261
是否有一个简单的修复程序允许我通过cv2
将此视频Feed流式传输到我的python shell中?如果有其他库可以实现相同的目的,也不会绝对致力于cv2
。
答案 0 :(得分:7)
您需要安装2件东西
安装这两个软件包后,您可以使用youtube url播放视频。 请参考下面的代码
url = 'https://youtu.be/W1yKqFZ34y4'
vPafy = pafy.new(url)
play = vPafy.getbest(preftype="webm")
#start the video
cap = cv2.VideoCapture(play.url)
while (True):
ret,frame = cap.read()
"""
your code....
"""
cv2.imshow('frame',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
答案 1 :(得分:3)
可以使用pafy(https://pypi.python.org/pypi/pafy)
import cv2, pafy
url = "https://www.youtube.com/watch?v=aKX8uaoy9c8"
videoPafy = pafy.new(url)
best = videoPafy.getbest(preftype="webm")
video=cv2.VideoCapture(best.url)
答案 2 :(得分:2)
@ incBrain将youtube视频下载到本地mp4的建议就是去的地方。以下是我在EC2上设置远程服务器环境的步骤,输出通过X11转发传输到我的本地计算机:
ssh -X -i "<ssh_key.pem>" ubuntu@<IP-address>.compute-1.amazonaws.com
(注意-X
选项是一个重要的补充。它是我们用来将输出从EC-2服务器传递到本地X11客户端的方式)sudo pip install --upgrade youtube_dl
(我知道,sudo pip
很糟糕。我责怪网站说明)youtube-dl https://www.youtube.com/watch?v=VUjF1fRw9sA -o motocross.mp4
python demo_cv.py
X11转发可能很棘手。如果您遇到任何挂断,this post也可能对您有所帮助。
答案 3 :(得分:0)
您可以安装我的VidGear Python库,该库仅提供URL,即可将YouTube视频自动流水到OpenCV。这是一个完整的python示例:
# import libraries
from vidgear.gears import CamGear
import cv2
stream = CamGear(source='https://youtu.be/dQw4w9WgXcQ', y_tube =True, time_delay=1, logging=True).start() # YouTube Video URL as input
# infinite loop
while True:
frame = stream.read()
# read frames
# check if frame is None
if frame is None:
#if True break the infinite loop
break
# do something with frame here
cv2.imshow("Output Frame", frame)
# Show output window
key = cv2.waitKey(1) & 0xFF
# check for 'q' key-press
if key == ord("q"):
#if 'q' key-pressed break out
break
cv2.destroyAllWindows()
# close output window
stream.stop()
# safely close video stream.
答案 4 :(得分:0)
#pip install pafy
#sudo pip install --upgrade youtube_dl
import cv2, pafy
url = "https://www.youtube.com/watch?v=qvyTx01ZcQQ"
video = pafy.new(url)
best = video.getbest(preftype="mp4")
capture = cv2.VideoCapture(best.url)
while True:
check, frame = capture.read()
print (check, frame)
cv2.imshow('frame',frame)
cv2.waitKey(10)
capture.release()
cv2.destroyAllWindows()
试试这个,也适用于直播