import gxipy as gx
from PIL import Image
import datetime
import cv2
"""
Author:NoamaNelson
Date:2019-11-21
Discription:Secondary development of pythonsdk of Daheng camera.
"""
def main():
Width_set = 1280 # MAX:1280
Height_set = 1024 # MAX:1080
framerate_set = 80
OffsetX_set = 0
OffsetY_set = 28
WhiteBalance_set = 2 # 0:close 1:continues 2:once
num = 1
print("**********************************************************")
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
else:
print("")
#print("**********************************************************")
print("create success dev_num:%d" % dev_num)
cam = device_manager.open_device_by_sn(dev_info_list[0].get("sn"))
if cam.PixelColorFilter.is_implemented() is False:
print("no support")
cam.close_device()
return
else:
print("")
#print("**********************************************************")
print("success open colored cam, sn:%s" % dev_info_list[0].get("sn"))
cam.Width.set(Width_set)
cam.Height.set(Height_set)
cam.OffsetX.set(OffsetX_set)
cam.OffsetY.set(OffsetY_set)
print("")
print("**********************************************************")
print("Resolution Setting: (Width_MAX:1280 Height_MAX:1080)")
print("")
print("Width:%d Height:%d" % (Width_set,Height_set))
print("")
print("Offset X:%d Offset Y:%d" % (OffsetX_set,OffsetY_set))
print("**********************************************************")
cam.BalanceWhiteAuto.set(WhiteBalance_set)
print("")
print("WhiteBalance Setting: (0:close 1:continues 2:once)")
print("")
print("Mode:%d" % WhiteBalance_set)
cam.AcquisitionFrameRateMode.set(gx.GxSwitchEntry.ON)
cam.AcquisitionFrameRate.set(framerate_set)
print("")
print("**********************************************************")
print("frame per sec:%d fps"%framerate_set)
framerate_get = cam.CurrentAcquisitionFrameRate.get()
print("capture fps:%d fps"%framerate_get)
print("")
print("**********************************************************")
print("start capture")
print("")
cam.stream_on()
while True:
for i in range(num):
raw_image = cam.data_stream[0].get_image()
if raw_image is None:
print("fail to get colored image")
continue
rgb_image = raw_image.convert("RGB")
if rgb_image is None:
continue
#rgb_image.image_improvement(color_correction_param, contrast_lut, gamma_lut)
numpy_image = rgb_image.get_numpy_array()
if numpy_image is None:
continue
numpy_image = cv2.cvtColor(numpy_image,cv2.COLOR_RGB2BGR)
#img = Image.fromarray(numpy_image, 'RGB')
cv2.imshow("frame",numpy_image)
if cv2.waitKey(1) & 0xFF == ord('s'):
mtime = datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%S')
cv2.imwrite('/home/nvidia/Desktop/photos/'+ str(mtime) + '.jpg', numpy_image)
print("Frame ID: %d Height: %d Width: %d framerate_set:%dfps framerate_get:%dfps"
% (raw_image.get_frame_id(), raw_image.get_height(), raw_image.get_width(), framerate_set, framerate_get))
if cv2.waitKey(1) & 0xFF == 27:
break
print("")
print("**********************************************************")
print("cam stop")
cam.stream_off()
print("")
print("device closed")
print("**********************************************************")
cam.close_device()
if __name__ == "__main__":
main()