UPDATE Vision 更新图像漾相机

This commit is contained in:
HJW
2024-09-19 18:16:44 +08:00
parent 4fb97c49ff
commit 41cf46e71d
2 changed files with 97 additions and 39 deletions

View File

@ -7,6 +7,10 @@ class Error_Code(Enum):
class VisionError_Code(Enum):
CAMERA_SUCCESS = 200
CAMERA_NO_DEVICE = 201
CAMERA_NO_DEVICE_FOUND = 202

View File

@ -7,6 +7,7 @@
'''
from Vision.tool.percipio.win import pcammls
from Vision.tool.percipio.win.pcammls import *
from Expection import VisionError_Code
import cv2
@ -25,12 +26,13 @@ class PythonPercipioDeviceEvent(pcammls.DeviceEvent):
def IsOffline(self):
return self.Offline
class camera:
class camera():
def __init__(self):
super().__init__()
self.caminit_isok = False
cl = PercipioSDK()
dev_list = cl.ListDevice()
self.cl = PercipioSDK()
dev_list = self.cl.ListDevice()
for idx in range(len(dev_list)):
dev = dev_list[idx]
print('{} -- {} \t {}'.format(idx, dev.id, dev.iface.id))
@ -46,72 +48,84 @@ class camera:
sn = dev_list[selected_idx].id
# 设备ID
handle = cl.Open(sn)
if not cl.isValidHandle(handle):
err = cl.TYGetLastErrorCodedescription()
self.handle = self.cl.Open(sn)
if not self.cl.isValidHandle(self.handle):
err = self.cl.TYGetLastErrorCodedescription()
print('no device found : ', end='')
print(err)
return
event = PythonPercipioDeviceEvent()
cl.DeviceRegiststerCallBackEvent(event)
self.event = PythonPercipioDeviceEvent()
self.cl.DeviceRegiststerCallBackEvent(self.event)
color_fmt_list = cl.DeviceStreamFormatDump(handle, PERCIPIO_STREAM_COLOR)
color_fmt_list = self.cl.DeviceStreamFormatDump(self.handle, PERCIPIO_STREAM_COLOR)
if len(color_fmt_list) == 0:
print('device has no color stream.')
return
print('color image format list:')
for idx in range(len(color_fmt_list)):
fmt = color_fmt_list[idx]
print('\t{} -size[{}x{}]\t-\t desc:{}'.format(idx, cl.Width(fmt), cl.Height(fmt), fmt.getDesc()))
cl.DeviceStreamFormatConfig(handle, PERCIPIO_STREAM_COLOR, color_fmt_list[2])
# print('color image format list:')
# for idx in range(len(color_fmt_list)): # 查看图像分辨率
# fmt = color_fmt_list[idx]
# print('\t{} -size[{}x{}]\t-\t desc:{}'.format(idx, cl.Width(fmt), cl.Height(fmt), fmt.getDesc()))
self.cl.DeviceStreamFormatConfig(self.handle, PERCIPIO_STREAM_COLOR, color_fmt_list[2])
depth_fmt_list = cl.DeviceStreamFormatDump(handle, PERCIPIO_STREAM_DEPTH)
depth_fmt_list = self.cl.DeviceStreamFormatDump(self.handle, PERCIPIO_STREAM_DEPTH)
if len(depth_fmt_list) == 0:
print('device has no depth stream.')
return
print('depth image format list:')
for idx in range(len(depth_fmt_list)):
fmt = depth_fmt_list[idx]
print('\t{} -size[{}x{}]\t-\t desc:{}'.format(idx, cl.Width(fmt), cl.Height(fmt), fmt.getDesc()))
cl.DeviceStreamFormatConfig(handle, PERCIPIO_STREAM_DEPTH, depth_fmt_list[2])
# print('depth image format list:') # 查看深度图分辨率
# for idx in range(len(depth_fmt_list)):
# fmt = depth_fmt_list[idx]
# print('\t{} -size[{}x{}]\t-\t desc:{}'.format(idx, cl.Width(fmt), cl.Height(fmt), fmt.getDesc()))
self.cl.DeviceStreamFormatConfig(self.handle, PERCIPIO_STREAM_DEPTH, depth_fmt_list[2])
err = cl.DeviceLoadDefaultParameters(handle)
err = self.cl.DeviceLoadDefaultParameters(self.handle)
if err:
print('Load default parameters fail: ', end='')
print(cl.TYGetLastErrorCodedescription())
print(self.cl.TYGetLastErrorCodedescription())
else:
print('Load default parameters successful')
scale_unit = cl.DeviceReadCalibDepthScaleUnit(handle)
print('depth image scale unit :{}'.format(scale_unit))
self.scale_unit = self.cl.DeviceReadCalibDepthScaleUnit(self.handle)
#print('depth image scale unit :{}'.format(scale_unit))
depth_calib = cl.DeviceReadCalibData(handle, PERCIPIO_STREAM_DEPTH)
color_calib = cl.DeviceReadCalibData(handle, PERCIPIO_STREAM_COLOR)
self.depth_calib = self.cl.DeviceReadCalibData(self.handle, PERCIPIO_STREAM_DEPTH)
self.color_calib = self.cl.DeviceReadCalibData(self.handle, PERCIPIO_STREAM_COLOR)
pointcloud_data_arr = pointcloud_data_list()
self.pointcloud_data_arr = pointcloud_data_list()
self.img_registration_depth = image_data()
self.img_registration_render = image_data()
self.img_parsed_color = image_data()
self.img_undistortion_color = image_data()
err = cl.DeviceStreamEnable(handle, PERCIPIO_STREAM_COLOR | PERCIPIO_STREAM_DEPTH)
err = self.cl.DeviceStreamEnable(self.handle, PERCIPIO_STREAM_COLOR | PERCIPIO_STREAM_DEPTH)
if err:
print('device stream enable err:{}'.format(err))
return
cl.DeviceStreamOn(handle)
self.cl.DeviceStreamOn(self.handle)
self.caminit_isok = True
print(VisionError_Code.CAMERA_SUCCESS)
def get_img(self):
""
'''
:param api: None
:return: ret ,img
'''
if self.caminit_isok == False:
if self.caminit_isok == False or self.event.IsOffline():
return 0, None
else:
pass
image_list = self.cl.DeviceStreamRead(self.handle, 2000)
if len(image_list) == 2:
for i in range(len(image_list)):
frame = image_list[i]
if frame.streamID == PERCIPIO_STREAM_COLOR:
img_color = frame
self.cl.DeviceStreamImageDecode(frame, self.img_undistortion_color)
img = self.img_undistortion_color.as_nparray()
return 1, img
return 0, None
def get_point_map(self):
""
@ -119,7 +133,7 @@ class camera:
:param api: None
:return: img
'''
if self.caminit_isok == False:
if self.caminit_isok == False or self.event.IsOffline():
return 0, None
else:
pass
@ -130,13 +144,53 @@ class camera:
:param api: None
:return: ret , img, point_map
'''
if self.caminit_isok == False:
return 0, None, None
if self.caminit_isok == False or self.event.IsOffline():
return 0, None
else:
pass
image_list = self.cl.DeviceStreamRead(self.handle, 2000)
if len(image_list) == 2:
for i in range(len(image_list)):
frame = image_list[i]
if frame.streamID == PERCIPIO_STREAM_DEPTH:
img_depth = frame
img_depth2p3d = frame
if frame.streamID == PERCIPIO_STREAM_COLOR:
img_color = frame
self.cl.DeviceStreamMapDepthImageToColorCoordinate(self.depth_calib, img_depth.width, img_depth.height,
self.scale_unit, img_depth, self.color_calib, img_color.width,
img_color.height, self.img_registration_depth)
# self.cl.DeviceStreamDepthRender(self.img_registration_depth, self.img_registration_render)
# mat_depth_render = self.img_registration_render.as_nparray()
# cv2.imshow('registration', mat_depth_render)
self.cl.DeviceStreamMapDepthImageToPoint3D(self.img_registration_depth, self.depth_calib, self.scale_unit,
self.pointcloud_data_arr)
# show p3d arr data
p3d_nparray = self.pointcloud_data_arr.as_nparray()
#cv2.imshow('p3d2', p3d_nparray)
self.cl.DeviceStreamImageDecode(img_color, self.img_parsed_color)
self.cl.DeviceStreamDoUndistortion(self.color_calib, self.img_parsed_color, self.img_undistortion_color)
mat_undistortion_color = self.img_undistortion_color.as_nparray()
return 1, mat_undistortion_color, p3d_nparray
else:
return 0, None, None
def release(self):
if self.caminit_isok == False:
pass
else:
self.cl.DeviceStreamOff(self.handle)
self.cl.Close(self.handle)
pass
# my_camera = camera()
# while True:
# ret, img, p3d_nparray = my_camera.get_img_and_point_map()
# cv2.imshow('img', img)
# cv2.imshow('3d',p3d_nparray)
# cv2.waitKey(1)