简体   繁体   English

使用basler相机和python时保存视频而不是保存图像

[英]Save video instead of saving images while using basler camera and python

I'm using Basler camera and python to record some video. 我正在使用Basler相机和python录制一些视频。 I can successfully capture individual frames, but I don't know how to record a video. 我可以成功捕获单个帧,但是我不知道如何录制视频。

Following is my code: 以下是我的代码:

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

I haven't found a way to record a video using pypylon ; 我还没有找到使用pypylon录制视频的方法; it seems to be a pretty light wrapper around Pylon. 它似乎是围绕Pylon的漂亮包装纸。 However, I have found a way to save a video using imageio : 但是,我找到了一种使用imageio保存视频的方法:

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

The above can be used with .mov , .avi , .mpg , .mpeg , .mp4 , .mkv or .wmv , so long as the FFmpeg program is available. 只要FFmpeg程序可用,以上内容就可以与.mov.avi.mpg.mpeg.mp4.mkv.wmv使用。 How you will install this program depends on your operating system. 如何安装此程序取决于您的操作系统。 See this link for details on the parameters you can use . 有关可以使用的参数的详细信息,请参见此链接。

Then, simply replace the call to imwrite with: 然后,只需使用以下命令替换对imwrite的调用:

writer.append_data(image)

ensuring that this occurs in the with block. 确保在with块中发生这种情况。

An example implementation: 一个示例实现:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Basler Ace 相机与 Python 的集成 - Basler Ace Camera Integration with Python 无法使用 cam.StartGrabbing() 保存来自 Basler acA5472-17um 相机的图片 - Can't save a picture from a Basler acA5472-17um Camera using cam.StartGrabbing() 使用带有 pylonsrc 的 Python3 gstreamer1.0 使用 basler 相机和 pylon5 在处理循环中给出错误 - Using Python3 gstreamer1.0 with pylonsrc using basler camera and pylon5 gives error in the processing loop 无法使用 python 中的 opencv 和 xvid 编解码器和轻子相机保存视频 - unable to save video using opencv in python with xvid codec and lepton camera 使用 python-openCV 读取视频并保存为 pgm 格式的图像 - Reading video and saving in pgm format images using python-openCV python opencv在使用第三方相机时无法显示视频 - python opencv could not display video while using third party camera 从摄像机录制视频时节省TTL脉冲输入时间 - Save TTL pulse input time while recording video from camera Basler pylon ip camera TimoutException 在 python 中抛出(文件“tlfactory.cpp”,第 694 行) - Basler pylon ip camera TimoutException thrown (file 'tlfactory.cpp', line 694) in python Python 请求 - 在有页面时获取和保存图像 - Python requests - fetch and save images while there are pages 为什么我在使用 Pillow 将图像保存在 python 中时出现错误? - Why i'm getting an error while saving images in python using Pillow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM