# 主要进行视频录制的功能
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()