使用basler camera和python时保存视频而不是保存图像 [英] Save video instead of saving images while using basler camera and python

查看:1765
本文介绍了使用basler camera和python时保存视频而不是保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Basler相机和python来录制一些视频。我可以成功捕获单个帧,但我不知道如何录制视频。

I'm using Basler camera and python to record some video. I can successfully capture individual frames, but I don't know how to record a video.

以下是我的代码:

import os
import pypylon
from imageio import imwrite
import time
start=time.time()

print('Sampling rate (Hz):')
fsamp = input()
fsamp = float(fsamp)

time_exposure = 1000000*(1/fsamp)

available_cameras = pypylon.factory.find_devices()
cam = pypylon.factory.create_device(available_cameras[0])
cam.open()

#cam.properties['AcquisitionFrameRateEnable'] = True
#cam.properties['AcquisitionFrameRate'] = 1000
cam.properties['ExposureTime'] = time_exposure

buffer = tuple(cam.grab_images(2000))
for count, image in enumerate(buffer):
    filename = str('I:/Example/{}.png'.format(count))
    imwrite(filename, image)
del buffer


推荐答案

我没有找到了一种使用 pypylon 录制视频的方法;它似乎是Pylon周围一个非常轻的包装。但是,我找到了一种使用 imageio 保存视频的方法:

I haven't found a way to record a video using pypylon; it seems to be a pretty light wrapper around Pylon. However, I have found a way to save a video using imageio:

from imageio import get_writer
with get_writer('I:/output-filename.mp4', fps=fps) as writer:
    # Some stuff with the frames

上述内容可用于 .mov .avi .mpg .mpeg .mp4 .mkv .wmv 。如何安装此程序取决于您的操作系统。请参阅此链接以获取有关可以使用的参数的详细信息

The above can be used with .mov, .avi, .mpg, .mpeg, .mp4, .mkv or .wmv, so long as the FFmpeg program is available. How you will install this program depends on your operating system. See this link for details on the parameters you can use.

然后,只需将调用替换为 imwrite

Then, simply replace the call to imwrite with:

writer.append_data(image)

确保这发生在带块的中。

ensuring that this occurs in the with block.

示例实现:

import os
import pypylon
from imageio import get_writer

while True:
    try:
        fsamp = float(input('Sampling rate (Hz): '))
        break
    except ValueError:
        print('Invalid input.')

time_exposure = 1000000 / fsamp

available_cameras = pypylon.factory.find_devices()
cam = pypylon.factory.create_device(available_cameras[0])
cam.open()

cam.properties['ExposureTime'] = time_exposure

buffer = tuple(cam.grab_images(2000))
with get_writer(
       'I:/output-filename.mkv',  # mkv players often support H.264
        fps=fsamp,  # FPS is in units Hz; should be real-time.
        codec='libx264',  # When used properly, this is basically
                          # "PNG for video" (i.e. lossless)
        quality=None,  # disables variable compression
        pixelformat='rgb24',  # keep it as RGB colours
        ffmpeg_params=[  # compatibility with older library versions
            '-preset',  # set to faster, veryfast, superfast, ultrafast
            'fast',     # for higher speed but worse compression
            '-crf',  # quality; set to 0 for lossless, but keep in mind
            '11'     # that the camera probably adds static anyway
        ]
) as writer:
    for image in buffer:
        writer.append_data(image)
del buffer

这篇关于使用basler camera和python时保存视频而不是保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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