大恒相机sdk录屏(python+opencv)终于解决了

大恒相机录屏终于解决了(python+sdk+opencv)

一开始拿到大恒相机,就遇上了第一个坑:直接用opencv函数是打不开的,要用他自己sdk的gxipy。用了之后,给的示例只有采集图片的,录屏如下:


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


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

    # 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()



    # # acquisition image: num is the image number
    # num = 3
    # for i in range(num):
    #     # get raw image
    #     raw_image = cam.data_stream[0].get_image()
    #     if raw_image is None:
    #         print("Getting image failed.")
    #         continue
    #
    #     # get RGB image from raw image
    #     rgb_image = raw_image.convert("RGB")
    #     if rgb_image is None:
    #         continue
    #
    #     # improve image quality
    #     rgb_image.image_improvement(color_correction_param, contrast_lut, gamma_lut)
    #
    #     # create numpy array with data from raw image
    #     numpy_image = rgb_image.get_numpy_array()
    #     if numpy_image is None:
    #         continue
    #
    #     # show acquired image
    #     img = Image.fromarray(numpy_image, 'RGB')
    #     img.show()
    #
    #     # print height, width, and frame ID of the acquisition image
    #     print("Frame ID: %d   Height: %d   Width: %d"
    #           % (raw_image.get_frame_id(), raw_image.get_height(), raw_image.get_width()))
    # 视频存储的格式
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    # 帧率
    fps = cam.AcquisitionFrameRate.get()
    # 视频的宽高
    size = (cam.Width.get(), cam.Height.get())
    print(cam.Width.get(), cam.Height.get())
    # 文件名定义
    filename = 'G://' + time.strftime("%Y%m%d_%H%M%S", time.localtime()) + '.avi'
    # 视频存储
    out = cv2.VideoWriter(filename, fourcc, fps, size)

    while out.isOpened():
        raw_image = cam.data_stream[0].get_image()  # 使用相机采集一张图片
        rgb_image = raw_image.convert("RGB")  # 从彩色原始图像获取 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
        cv2.namedWindow('video', cv2.WINDOW_NORMAL)  # 创建一个名为video的窗口
        cv2.imshow('video', numpy_image)  # 将捕捉到的图像在video窗口显示
        out.write(numpy_image)  # 将捕捉到的图像存储

        # 按esc键退出程序
        if cv2.waitKey(1) & 0xFF == 27:
            break

    # stop data acquisition
    cam.stream_off()

    # close device
    cam.close_device()

if __name__ == "__main__":
    main()

亲测效果不错,可以实现录屏

你可能感兴趣的:(大恒相机,opencv,计算机视觉)