在Raspberry Pi上由OpenCV 2.3.1使用Python创建的无视频文件 [英] No video file created by OpenCV 2.3.1 with Python on Raspberry Pi
问题描述
我正在尝试从网络摄像头(Logitech c210)捕获视频并从中创建视频文件.
I'm trying to capture video from a webcam(Logitech c210) and create a video file from it.
这是在2013年5月25日的Raspbian Wheezy上.
This is on Raspbian Wheezy 2013-05-25.
相机上的灯点亮约30秒钟,但未创建文件. 我的摄像头显示在OpenCV窗口中.
The light on the camera comes on for about 30 seconds but no file is created. I've had the webcam displaying in an OpenCV window.
我想知道这是否是编解码器问题,因为脚本可以在带有-1参数的Windows上运行?
I'm wondering if this is a codec problem as the script works on windows with the -1 parameter?
如果是的话,是否有推荐的Raspberry Pi编解码器?
If so are there any recommended codecs for Raspberry Pi?
如果我购买了mpeg2编解码器,那行得通吗?
If I buy the mpeg2 codec would that work?
我已经列出了我尝试过的编解码器,但是没有用.
I've listed the codecs that I've tried though none work.
脚本如下:
import cv2
import time
def InitialiseCamera():
camera = cv2.VideoCapture(0)
if camera is None:
print('Warning: unable to access camera')
else:
print('initialized camera')
return camera
def InitialiseWriter():
fps = 5
size = (640, 480)
destinationFile = 'video.avi'
# These are the codecs I've tried so far
codec = cv2.cv.CV_FOURCC('I','4','2','0')
#codec = cv2.cv.CV_FOURCC('A','V','C','1')
#codec = cv2.cv.CV_FOURCC('Y','U','V','1')
#codec = cv2.cv.CV_FOURCC('P','I','M','1')
#codec = cv2.cv.CV_FOURCC('M','J','P','G')
#codec = cv2.cv.CV_FOURCC('M','P','4','2')
#codec = cv2.cv.CV_FOURCC('D','I','V','3')
#codec = cv2.cv.CV_FOURCC('D','I','V','X')
#codec = cv2.cv.CV_FOURCC('U','2','6','3')
#codec = cv2.cv.CV_FOURCC('I','2','6','3')
#codec = cv2.cv.CV_FOURCC('F','L','V','1')
#codec = cv2.cv.CV_FOURCC('H','2','6','4')
#codec = cv2.cv.CV_FOURCC('A','Y','U','V')
#codec = cv2.cv.CV_FOURCC('I','U','Y','V')
#codec = -1
video = cv2.VideoWriter(destinationFile, codec, fps, size, True);
if video is None:
print('Warning: unable to create video writer')
else:
print('initialized writer')
return video
def CaptureVideo(c,w):
i = 0
while i<150:
i+=1
f,img = c.read()
try:
w.write(img)
except:
print "Unexpected error: ", sys.exec_info()[0]
print('complete')
c.release()
if __name__ == '__main__':
cam = InitialiseCamera()
writer = InitialiseWriter()
CaptureVideo(cam,writer)
推荐答案
最终,我尝试了所有的fourcc编解码器,但都没有成功.
I eventually went through and tried all of the fourcc codecs and none worked.
与mpeg2编解码器相同.
Same with the mpeg2 codec.
我最终使用avconv创建并捕获了此行的视频:
I ended up using avconv to create and capture the video with this line:
import os
os.system("avconv -f video4linux2 -input_format mjpeg -i /dev/video0 output.avi")
os.system()用于运行终端命令.
os.system() is used to run a terminal command.
以后仍可以使用OpenCV处理视频文件.
OpenCV can still be used to process the video files at a later time.
希望这对某人有帮助.
这篇关于在Raspberry Pi上由OpenCV 2.3.1使用Python创建的无视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!