OpenAi-google资料写出的相机视频录制

该代码示例展示了如何使用GXIP库初始化设备,开启自动增益和自动白平衡功能,进行连续的彩色图像捕获,并使用OpenCV将图像数据写入视频文件。程序会检查设备是否支持所需功能,并调整曝光时间、增益等参数,最后将视频保存为AVI格式。
摘要由CSDN通过智能技术生成

# 主要进行视频录制的功能


import gxipy as gx
from PIL import Image
import cv2
import time

from gxipy import GxAutoEntry


def main():
    # print the demo information
    print("")
    print("-------------------------------------------------------------")
    print("Sample to show how to acquire color image continuously and show acquired image.")
    print("-------------------------------------------------------------")
    print("")
    print("Initializing......")
    print("")

    # create a device manager
    device_manager = gx.DeviceManager()
    dev_num, dev_info_list = device_manager.update_device_list()
    if dev_num is 0:
        print("Number of enumerated devices is 0")
        return

    # open the first device
    cam = device_manager.open_device_by_index(1)

    # exit when the camera is a mono camera
    if cam.PixelColorFilter.is_implemented() is False:
        print("This sample does not support mono camera.")
        cam.close_device()
        return
    if cam.GainAuto.is_implemented() is False:
        print("不支持自动白平衡")
    else:
        cam.GainAuto.set(GxAutoEntry.CONTINUOUS)
        print("设置自动增益成功")

    if cam.BalanceWhiteAuto.is_implemented() is False:
        print("不支持自动白平衡")
    else:
        cam.BalanceWhiteAuto.set(GxAutoEntry.CONTINUOUS)
        print("设置自动白平衡成功")

    # set continuous acquisition
    cam.TriggerMode.set(gx.GxSwitchEntry.OFF)

    # set exposure
    cam.ExposureTime.set(10000.0)

    # set gain
    cam.Gain.set(10.0)

    # get param of improving image quality
    if cam.GammaParam.is_readable():
        gamma_value = cam.GammaParam.get()
        gamma_lut = gx.Utility.get_gamma_lut(gamma_value)
    else:
        gamma_lut = None
    if cam.ContrastParam.is_readable():
        contrast_value = cam.ContrastParam.get()
        contrast_lut = gx.Utility.get_contrast_lut(contrast_value)
    else:
        contrast_lut = None
    if cam.ColorCorrectionParam.is_readable():
        color_correction_param = cam.ColorCorrectionParam.get()
    else:
        color_correction_param = 0

    # start data acquisition
    cam.stream_on()

    # 视频存储的格式
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    # 帧率
    fps = cam.AcquisitionFrameRate.get()
    bin_write = cam.BinningHorizontal.is_writable()
    bin_write_get = cam.BinningHorizontal.get()
    width_write = cam.Width.is_writable()
    width_write_get = cam.Width.get()
    print("bin_write: {}--{}".format(bin_write, bin_write_get))
    print("width_write: {}---{}".format(width_write, width_write_get))
    # 视频的宽高
    width = int(cam.Width.get()/1)
    height = int(cam.Height.get()/1)
    # size = (cam.Width.get(), cam.Height.get())
    size = (width, height)
    print(cam.Width.get(), cam.Height.get())
    print(size)
    # 文件名定义
    # filename = r"D:\DahenImaging\avi_rec\\" + time.strftime("%Y%m%d_%H%M%S", time.localtime()) + '.avi'
    import os
    filename = os.path.join("D:\\DahenImaging\\avi_rec", time.strftime("%Y%m%d_%H%M%S", time.localtime()) + '.avi')
    # 视频存储
    out = cv2.VideoWriter(filename, fourcc, fps, size)

    # cv2.WINDOW_AUTOSIZE
    cv2.namedWindow('video', cv2.WINDOW_NORMAL)  # 创建一个名为video的窗口
    while out.isOpened():
        raw_image = cam.data_stream[0].get_image()  # 使用相机采集一张图片
        rgb_image = raw_image.convert("RGB")  # 从彩色原始图像获取q RGB 图像
        numpy_image = rgb_image.get_numpy_array()  # 从 RGB 图像数据创建 numpy 数组
        if rgb_image is None:
            continue
        numpy_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)  # opencv采用的是BGR图像, 讲RGB转为BGR
        str_show = "time : {}".format(str(time.strftime("%Y%m%d_%H:%M:%S", time.localtime())))
        numpy_image = numpy_image[0:height, 0:width, :]
        cv2.putText(numpy_image, str_show, (10, 1200), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 3)
        cv2.putText(numpy_image, "Screen black times:", (10, 1300), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 3)
        cv2.imshow('video', numpy_image)  # 将捕捉到的图像在video窗口显示
        print(numpy_image.shape)
        out.write(numpy_image)  # 将捕捉到的图像存储
        # 按esc键退出程序
        # if cv2.waitKey(1) & 0xFF == 27:
        #     break
        wait_key = cv2.waitKey(1)
        if wait_key & 0xFF == ord('q'):
            print("Exit")
            print(wait_key)
            break
    # stop data acquisition
    out.release()
    cam.stream_off()
    # close device
    cam.close_device()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    main()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值