VzenseDS_api.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import math
  2. from SDK.Vzense.VzenseDS_define import *
  3. import platform
  4. import os
  5. import ctypes
  6. from ctypes import *
  7. gCallbackFuncList = []
  8. class VzenseTofCam():
  9. device_handle = c_void_p(0)
  10. def __init__(self):
  11. system_ = platform.system().lower()
  12. machine_ = platform.machine().lower()
  13. architecture_ = platform.architecture()[0]
  14. # print('system:',system_)
  15. # print('machine_:',machine_)
  16. # print('architecture:',architecture_)
  17. if system_ == 'linux':
  18. if machine_ == 'x86_64':
  19. os_info = os.uname()
  20. # print('os_info:',type(os_info))
  21. system_info = os_info.version
  22. # print('version:',system_info)
  23. if system_info.find('18.04') != -1 or system_info.find('20.04') != -1:
  24. libpath = os.path.dirname(os.path.abspath(__file__)) + "/Ubuntu18.04/Lib/libNebula_api.so"
  25. # print("load lib from ", libpath)
  26. self.vz_cam_lib = cdll.LoadLibrary(libpath)
  27. else:
  28. libpath = os.path.dirname(os.path.abspath(__file__)) + "/Ubuntu16.04/Lib/libNebula_api.so"
  29. # print(libpath)
  30. self.vz_cam_lib = cdll.LoadLibrary(libpath)
  31. elif machine_ == 'aarch64':
  32. libpath = os.path.dirname(os.path.abspath(__file__)) + "/AArch64/Lib/libNebula_api.so"
  33. # print(libpath)
  34. self.vz_cam_lib = cdll.LoadLibrary(libpath)
  35. else:
  36. # print('do not supported OS', system_, machine_)
  37. exit()
  38. elif platform.system() == 'Windows':
  39. if machine_ == 'amd64':
  40. if architecture_ == '64bit':
  41. libpath = os.path.dirname(os.path.abspath(__file__)) + "/Windows/Bin/x64/Nebula_api.dll"
  42. # print(libpath)
  43. self.vz_cam_lib = cdll.LoadLibrary(libpath)
  44. else:
  45. libpath = os.path.dirname(os.path.abspath(__file__)) + "/Windows/Bin/x86/Nebula_api.dll"
  46. # print(libpath)
  47. self.vz_cam_lib = cdll.LoadLibrary(libpath)
  48. else:
  49. # print('do not supported OS', system_, machine_)
  50. exit()
  51. else:
  52. # print('do not supported OS', system_, machine_)
  53. exit()
  54. self.device_handle = c_void_p(0)
  55. self.vz_cam_lib.VZ_Initialize()
  56. def __del__(self):
  57. self.vz_cam_lib.VZ_Shutdown()
  58. def VZ_GetSDKVersion(self):
  59. tmp = c_char * 64
  60. version = tmp()
  61. return self.vz_cam_lib.VZ_GetSDKVersion(version), version.value
  62. def VZ_GetDeviceCount(self):
  63. count = c_int()
  64. self.vz_cam_lib.VZ_GetDeviceCount(byref(count))
  65. return count.value
  66. def VZ_GetDeviceInfoList(self, cam_count=1):
  67. tmp = VzDeviceInfo * cam_count
  68. device_infolist = tmp()
  69. return self.vz_cam_lib.VZ_GetDeviceInfoList(cam_count, device_infolist), device_infolist
  70. def VZ_GetDeviceInfo(self, cam_index=0):
  71. device_info = VzDeviceInfo()
  72. return self.vz_cam_lib.VZ_GetDeviceInfo(cam_index, byref(device_info)), device_info
  73. def VZ_OpenDeviceByUri(self, uri=c_char_p()):
  74. if uri:
  75. return self.vz_cam_lib.VZ_OpenDeviceByUri(uri, byref(self.device_handle))
  76. else:
  77. return VzReturnStatus.VzRetInputPointerIsNull
  78. def VZ_OpenDeviceByAlias(self, alias=c_char_p()):
  79. if alias:
  80. return self.vz_cam_lib.VZ_OpenDeviceByAlias(alias, byref(self.device_handle))
  81. else:
  82. return VzReturnStatus.VzRetInputPointerIsNull
  83. def VZ_OpenDeviceByIP(self, ip=c_char_p()):
  84. if ip:
  85. return self.vz_cam_lib.VZ_OpenDeviceByIP(ip, byref(self.device_handle))
  86. else:
  87. return VzReturnStatus.VzRetInputPointerIsNull, 0
  88. def VZ_CloseDevice(self):
  89. return self.vz_cam_lib.VZ_CloseDevice(byref(self.device_handle))
  90. def VZ_StartStream(self):
  91. return self.vz_cam_lib.VZ_StartStream(self.device_handle)
  92. def VZ_StopStream(self):
  93. return self.vz_cam_lib.VZ_StopStream(self.device_handle)
  94. def VZ_GetFrameReady(self, waitTime=c_uint16(33)):
  95. frameready = VzFrameReady()
  96. return self.vz_cam_lib.VZ_GetFrameReady(self.device_handle, waitTime, byref(frameready)), frameready
  97. def VZ_GetFrame(self, frametype=VzFrameType.VzDepthFrame):
  98. Vzframe = VzFrame()
  99. return self.vz_cam_lib.VZ_GetFrame(self.device_handle, frametype.value, byref(Vzframe)), Vzframe
  100. def VZ_SetWorkMode(self, mode=VzWorkMode.ActiveMode):
  101. return self.vz_cam_lib.VZ_SetWorkMode(self.device_handle, mode.value)
  102. def VZ_GetWorkMode(self):
  103. mode = VzWorkMode(0)
  104. return self.vz_cam_lib.VZ_GetWorkMode(self.device_handle, byref(mode)), mode
  105. def VZ_SetSoftwareSlaveTrigger(self):
  106. return self.vz_cam_lib.VZ_SetSoftwareSlaveTrigger(self.device_handle)
  107. def VZ_RebootDevie(self):
  108. return self.vz_cam_lib.VZ_RebootDevie(self.device_handle)
  109. def VZ_GetSensorIntrinsicParameters(self, sensorType=VzSensorType.VzToFSensor):
  110. CameraParameters = VzSensorIntrinsicParameters()
  111. return self.vz_cam_lib.VZ_GetSensorIntrinsicParameters(self.device_handle, sensorType.value,
  112. byref(CameraParameters)), CameraParameters
  113. def VZ_GetSensorExtrinsicParameters(self):
  114. CameraExtrinsicParameters = VzSensorExtrinsicParameters()
  115. return self.vz_cam_lib.VZ_GetSensorExtrinsicParameters(self.device_handle, byref(
  116. CameraExtrinsicParameters)), CameraExtrinsicParameters
  117. def VZ_GetFirmwareVersion(self):
  118. tmp = c_char * 64
  119. fw = tmp()
  120. return self.vz_cam_lib.VZ_GetFirmwareVersion(self.device_handle, fw, 63), fw.value
  121. def VZ_GetDeviceMACAddress(self):
  122. tmp = c_char * 18
  123. mac = tmp()
  124. return self.vz_cam_lib.VZ_GetDeviceMACAddress(self.device_handle, mac), mac.value
  125. def VZ_SetIRGMMGain(self, gmmgain=c_uint8(20)):
  126. return self.vz_cam_lib.VZ_SetIRGMMGain(self.device_handle, gmmgain)
  127. def VZ_GetIRGMMGain(self):
  128. gmmgain = c_uint8(1)
  129. return self.vz_cam_lib.VZ_GetIRGMMGain(self.device_handle, byref(gmmgain)), gmmgain.value
  130. def VZ_SetColorPixelFormat(self, pixelFormat=VzPixelFormat.VzPixelFormatBGR888):
  131. return self.vz_cam_lib.VZ_SetColorPixelFormat(self.device_handle, pixelFormat)
  132. def VZ_SetColorResolution(self, w=c_int32(1600), h=c_int32(1200)):
  133. return self.vz_cam_lib.VZ_SetColorResolution(self.device_handle, w, h)
  134. def VZ_GetColorResolution(self):
  135. w = c_int32(1600)
  136. h = c_int32(1200)
  137. return self.vz_cam_lib.VZ_GetColorResolution(self.device_handle, byref(w), byref(h)), w, h
  138. def VZ_SetFrameRate(self, value=c_uint8(30)):
  139. return self.vz_cam_lib.VZ_SetFrameRate(self.device_handle, value)
  140. def VZ_GetFrameRate(self):
  141. value = c_uint8(1)
  142. return self.vz_cam_lib.VZ_GetFrameRate(self.device_handle, byref(value)), value.value
  143. def VZ_SetExposureControlMode(self, sensorType=VzSensorType.VzToFSensor,
  144. mode=VzExposureControlMode.VzExposureControlMode_Manual):
  145. return self.vz_cam_lib.VZ_SetExposureControlMode(self.device_handle, sensorType.value, mode.value)
  146. def VZ_SetExposureTime(self, sensorType=VzSensorType.VzToFSensor, params=c_int32(0)):
  147. ExposureTime = VzExposureTimeParams(VzExposureControlMode.VzExposureControlMode_Manual.value, params)
  148. return self.vz_cam_lib.VZ_SetExposureTime(self.device_handle, sensorType.value, ExposureTime)
  149. def VZ_GetExposureTime(self, sensorType=VzSensorType.VzToFSensor):
  150. params = VzExposureTimeParams(VzExposureControlMode.VzExposureControlMode_Manual.value, 0)
  151. return self.vz_cam_lib.VZ_GetExposureTime(self.device_handle, sensorType.value, byref(params)), params
  152. def VZ_SetTimeFilterParams(self, params=VzTimeFilterParams()):
  153. return self.vz_cam_lib.VZ_SetTimeFilterParams(self.device_handle, params)
  154. def VZ_GetTimeFilterParams(self):
  155. params = VzTimeFilterParams()
  156. return self.vz_cam_lib.VZ_GetTimeFilterParams(self.device_handle, byref(params)), params
  157. def VZ_SetConfidenceFilterParams(self, params=VzConfidenceFilterParams()):
  158. return self.vz_cam_lib.VZ_SetConfidenceFilterParams(self.device_handle, params)
  159. def VZ_GetConfidenceFilterParams(self):
  160. params = VzConfidenceFilterParams()
  161. return self.vz_cam_lib.VZ_GetConfidenceFilterParams(self.device_handle, byref(params)), params
  162. def VZ_SetFlyingPixelFilterParams(self, params=VzFlyingPixelFilterParams()):
  163. return self.vz_cam_lib.VZ_SetFlyingPixelFilterParams(self.device_handle, params)
  164. def VZ_GetFlyingPixelFilterParams(self):
  165. params = VzFlyingPixelFilterParams()
  166. return self.vz_cam_lib.VZ_GetFlyingPixelFilterParams(self.device_handle, byref(params)), params
  167. def VZ_SetFillHoleFilterEnabled(self, enable=c_bool(True)):
  168. return self.vz_cam_lib.VZ_SetFillHoleFilterEnabled(self.device_handle, enable)
  169. def VZ_GetFillHoleFilterEnabled(self):
  170. enable = c_bool(True)
  171. return self.vz_cam_lib.VZ_GetFillHoleFilterEnabled(self.device_handle, byref(enable)), enable.value
  172. def VZ_SetSpatialFilterEnabled(self, enable=c_bool(True)):
  173. return self.vz_cam_lib.VZ_SetSpatialFilterEnabled(self.device_handle, enable)
  174. def VZ_GetSpatialFilterEnabled(self):
  175. enable = c_bool(True)
  176. return self.vz_cam_lib.VZ_GetSpatialFilterEnabled(self.device_handle, byref(enable)), enable.value
  177. def VZ_SetTransformColorImgToDepthSensorEnabled(self, enabled=c_bool(True)):
  178. return self.vz_cam_lib.VZ_SetTransformColorImgToDepthSensorEnabled(self.device_handle, enabled)
  179. def VZ_GetTransformColorImgToDepthSensorEnabled(self):
  180. enabled = c_bool(True)
  181. return self.vz_cam_lib.VZ_GetTransformColorImgToDepthSensorEnabled(self.device_handle, byref(enabled)), enabled
  182. def VZ_SetTransformDepthImgToColorSensorEnabled(self, enabled=c_bool(True)):
  183. return self.vz_cam_lib.VZ_SetTransformDepthImgToColorSensorEnabled(self.device_handle, enabled)
  184. def VZ_GetTransformDepthImgToColorSensorEnabled(self):
  185. enabled = c_bool(True)
  186. return self.vz_cam_lib.VZ_GetTransformDepthImgToColorSensorEnabled(self.device_handle, byref(enabled)), enabled
  187. def VZ_ConvertDepthFrameToPointCloudVector(self, depthFrame=VzFrame()):
  188. len = depthFrame.width * depthFrame.height
  189. tmp = VzVector3f * len
  190. pointlist = tmp()
  191. return self.vz_cam_lib.VZ_ConvertDepthFrameToPointCloudVector(self.device_handle, byref(depthFrame),
  192. pointlist), pointlist
  193. def VZ_ConvertDepthToPointCloud(self, depth = VzDepthVector3()):
  194. point = VzVector3f()
  195. ret, params = self.VZ_GetSensorIntrinsicParameters()
  196. ret = self.vz_cam_lib.VZ_ConvertDepthToPointCloud(self.device_handle, id(depth), id(point), 1, params)
  197. print("-------------------------")
  198. return ret, point
  199. def VZ_SetHotPlugStatusCallback(self, callbackfunc=c_void_p):
  200. callbackFunc_ = ctypes.CFUNCTYPE(c_void_p, POINTER(VzDeviceInfo), c_int32)(callbackfunc)
  201. gCallbackFuncList.append(callbackFunc_)
  202. return self.vz_cam_lib.VZ_SetHotPlugStatusCallback(callbackFunc_)
  203. def VZ_GetManaulMaxExposureTime(self):
  204. tmp = r"Py_ToFExposureTimeMax"
  205. propertyKey = (c_char * 100)(*bytes(tmp, 'utf-8'))
  206. maxExposureTime = VzExposureTimeParams(VzExposureControlMode.VzExposureControlMode_Manual.value)
  207. return self.vz_cam_lib.VZ_GetProperty(self.device_handle, byref(propertyKey), byref(maxExposureTime),
  208. 8), maxExposureTime.exposureTime
  209. def VZ_SetParamsByJson(self, imgpath):
  210. pimgpath = (c_char * 1000)(*bytes(imgpath, 'utf-8'))
  211. return self.vz_cam_lib.VZ_SetParamsByJson(self.device_handle, byref(pimgpath))
  212. def VZ_SetColorGain(self, params=c_float(1.0)):
  213. return self.vz_cam_lib.VZ_SetColorGain(self.device_handle, params)
  214. def VZ_GetColorGain(self):
  215. tmp = c_float * 1
  216. params = tmp()
  217. return self.vz_cam_lib.VZ_GetColorGain(self.device_handle, params), params
  218. def VZ_GetProperty(self, propertyKey=c_char_p(), pData=c_void_p, dataSize=c_int32(8)):
  219. return self.vz_cam_lib.VZ_GetProperty(self.device_handle, propertyKey, pData, dataSize), pData
  220. def VZ_GetAutoMaxExposureTime(self):
  221. tmp = r"Py_RGBExposureTimeMax"
  222. propertyKey = (c_char * 100)(*bytes(tmp, 'utf-8'))
  223. maxExposureTime = VzExposureTimeParams(VzExposureControlMode.VzExposureControlMode_Auto.value)
  224. return self.vz_cam_lib.VZ_GetProperty(self.device_handle, byref(propertyKey), byref(maxExposureTime),
  225. 8), maxExposureTime.exposureTime
  226. def VZ_GetManualMaxExposureTime(self):
  227. tmp = r"Py_RGBExposureTimeMax"
  228. propertyKey = (c_char * 100)(*bytes(tmp, 'utf-8'))
  229. maxExposureTime = VzExposureTimeParams(VzExposureControlMode.VzExposureControlMode_Manual.value)
  230. return self.vz_cam_lib.VZ_GetProperty(self.device_handle, byref(propertyKey), byref(maxExposureTime),
  231. 8), maxExposureTime.exposureTime
  232. def VZ_SetAutoExposureTime(self, sensorType=VzSensorType.VzColorSensor, params=c_int32(0)):
  233. ExposureTime = VzExposureTimeParams(VzExposureControlMode.VzExposureControlMode_Auto.value, params)
  234. return self.vz_cam_lib.VZ_SetExposureTime(self.device_handle, sensorType.value, ExposureTime)
  235. def VZ_GetAutoExposureTime(self, sensorType=VzSensorType.VzColorSensor):
  236. params = VzExposureTimeParams(VzExposureControlMode.VzExposureControlMode_Auto.value, 0)
  237. return self.vz_cam_lib.VZ_GetExposureTime(self.device_handle, sensorType.value, byref(params)), params