没有由 OpenCV 2.3.1 在 Raspberry Pi 上使用 Python 创建的视频文件 [英] No video file created by OpenCV 2.3.1 with Python on Raspberry Pi

查看:27
本文介绍了没有由 OpenCV 2.3.1 在 Raspberry Pi 上使用 Python 创建的视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网络摄像头 (Logitech c210) 捕获视频并从中创建视频文件.

I'm trying to capture video from a webcam(Logitech c210) and create a video file from it.

这是在 Raspbian Wheezy 2013-05-25 上.

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?

如果有,有没有推荐的树莓派编解码器?

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.

希望这对某人有所帮助.

Hope this helps someone.

这篇关于没有由 OpenCV 2.3.1 在 Raspberry Pi 上使用 Python 创建的视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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