tofManager.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import etc_pb2 as etc
  2. import os, glog, numpy, cv2, SDK.ZX.tool
  3. from tofDevice import *
  4. import SDK.Vzense.VzenseDS_api as tof
  5. class TofManager:
  6. def saveIrFrame2Image(self, path, name, frame):
  7. frametmp = numpy.ctypeslib.as_array(frame.pFrameData, (1, frame.dataLen))
  8. frametmp.dtype = numpy.uint8
  9. frametmp.shape = (frame.height, frame.width)
  10. if not os.path.exists(path):
  11. os.makedirs(path)
  12. filename = path + name
  13. cv2.imwrite(filename, frametmp, [cv2.IMWRITE_JPEG_QUALITY, 100])
  14. def saveDepthFrame2Image(self, path, name, frame):
  15. frametmp = numpy.ctypeslib.as_array(frame.pFrameData, (1, frame.width * frame.height * 2))
  16. frametmp.dtype = numpy.uint16
  17. frametmp.shape = (frame.height, frame.width)
  18. # convert ushort value to 0xff is just for display
  19. img = numpy.int32(frametmp)
  20. img = img * 255 / c_uint16(7495)
  21. img = numpy.clip(img, 0, 255)
  22. img = numpy.uint8(img)
  23. frametmp = cv2.applyColorMap(img, cv2.COLORMAP_RAINBOW)
  24. if not os.path.exists(path):
  25. print("not exists")
  26. os.makedirs(path)
  27. filename = path + name
  28. cv2.imwrite(filename, frametmp, [cv2.IMWRITE_JPEG_QUALITY, 100])
  29. def __init__(self):
  30. self.etc_file = os.path.dirname(os.path.abspath(__file__)) + "/etc/device.json"
  31. self.tofs_etc = {}
  32. self.updateTofsEtc()
  33. self.camera = tof.VzenseTofCam()
  34. self.camera_list = {}
  35. self.search_flag = False
  36. def exec(self, req_data):
  37. print(req_data.statu)
  38. def updateTofsEtc(self):
  39. etc_proto = SDK.ZX.tool.getProtobufJsonConfig(self.etc_file, etc.DevicesConfig())
  40. for tof_etc in etc_proto.devices:
  41. self.tofs_etc[tof_etc.id] = tof_etc
  42. glog.info(self.tofs_etc)
  43. self.search_flag = False
  44. def getCameraEtc(self, id):
  45. return self.tofs_etc[id]
  46. def getAllCameraEtc(self):
  47. return self.tofs_etc
  48. def getCameraList(self):
  49. return self.camera_list
  50. def searchAllCamera(self):
  51. # 搜索相机,会确保配置中启用的相机全部搜索到
  52. camera_count = self.camera.VZ_GetDeviceCount()
  53. retry_count = 60
  54. device_ip_list = []
  55. while camera_count <= len(self.tofs_etc) and retry_count > 0:
  56. ret, deviceInfoList = self.camera.VZ_GetDeviceInfoList()
  57. retry_count = retry_count - 1
  58. camera_count = self.camera.VZ_GetDeviceCount()
  59. if camera_count > 0:
  60. device_ip_list = []
  61. for i in range(camera_count):
  62. device_ip_list.append(deviceInfoList[i].ip)
  63. find_device_count = 0
  64. for tof_etc in self.tofs_etc:
  65. if self.tofs_etc[tof_etc].enable is False:
  66. find_device_count = find_device_count + 1
  67. glog.info(self.tofs_etc[tof_etc].ipv4 + " not enable.")
  68. if find_device_count == len(self.tofs_etc):
  69. self.search_flag = True
  70. return self.search_flag
  71. continue
  72. if self.tofs_etc[tof_etc].ipv4.encode() not in device_ip_list:
  73. glog.info("find a device " + self.tofs_etc[tof_etc].ipv4 + " fulture.")
  74. break
  75. else:
  76. find_device_count = find_device_count + 1
  77. glog.info("find a device " + self.tofs_etc[tof_etc].ipv4 + " success.")
  78. if find_device_count == len(self.tofs_etc):
  79. self.search_flag = True
  80. return self.search_flag
  81. time.sleep(0.5)
  82. glog.info("scaning...... " + str(retry_count) + " already scanned" + str(camera_count) + " devices.")
  83. return self.search_flag
  84. def openCamera(self, id):
  85. if id in self.camera_list:
  86. glog.info("camera " + self.tofs_etc[id].ipv4 + " already opend.")
  87. return tof.VzReturnStatus.VzRetOK
  88. if self.tofs_etc[id].enable is False:
  89. glog.warning(
  90. "this camera not enabled in etc fiile, if you want to enable it, please update the file, and then use func updateTofsEtc.")
  91. return tof.VzReturnStatus.VzRetOthers
  92. cam = tof.VzenseTofCam()
  93. ret = cam.VZ_OpenDeviceByIP(self.tofs_etc[id].ipv4.encode())
  94. if ret == 0:
  95. glog.info(self.tofs_etc[id].ipv4 + " open successful")
  96. self.camera_list[id] = cam
  97. else:
  98. glog.info(self.tofs_etc[id].ipv4 + ' VZ_OpenDeviceByIP failed: ' + str(ret))
  99. def openAllCamera(self):
  100. for tof_etc in self.tofs_etc:
  101. if self.tofs_etc[tof_etc].enable is False:
  102. continue
  103. cam = tof.VzenseTofCam()
  104. ret = cam.VZ_OpenDeviceByIP(self.tofs_etc[tof_etc].ipv4.encode())
  105. if ret == 0:
  106. glog.info(self.tofs_etc[tof_etc].ipv4 + " open successful")
  107. self.camera_list[tof_etc] = cam
  108. else:
  109. glog.info(self.tofs_etc[tof_etc].ipv4 + ' VZ_OpenDeviceByIP failed: ' + str(ret))
  110. def closeCamera(self, id):
  111. if (id not in self.camera_list):
  112. glog.info("camera " + self.tofs_etc[id].ipv4 + " already closed.")
  113. return tof.VzReturnStatus.VzRetOK
  114. ret = self.camera_list[id].VZ_CloseDevice()
  115. if ret == 0:
  116. del self.camera_list[id]
  117. glog.info("close device " + str(self.tofs_etc[id].ipv4) + " successful")
  118. else:
  119. glog.warning("VZ_CloseDevice " + str(self.tofs_etc[id].ipv4) + " failed: " + str(ret))
  120. return ret
  121. def closeAllCamera(self):
  122. for id in self.camera_list:
  123. ret = self.camera_list[id].VZ_CloseDevice()
  124. if ret == 0:
  125. glog.info("close device " + str(self.tofs_etc[id].ipv4) + " successful")
  126. else:
  127. glog.warning("VZ_CloseDevice " + str(self.tofs_etc[id].ipv4) + " failed: " + str(ret))
  128. self.camera_list.clear()
  129. def startCameraStream(self, id):
  130. if id in self.camera_list:
  131. ret = self.camera_list[id].VZ_StartStream()
  132. if ret == 0:
  133. glog.info(self.tofs_etc[id].ipv4 + " start stream successful")
  134. else:
  135. glog.info(self.tofs_etc[id].ipv4 + ' VZ_StartStream failed: ' + str(ret))
  136. else:
  137. glog.warning("camera " + self.tofs_etc[id].ip + "not open, please open this camera")
  138. def startAllCameraStream(self):
  139. for id in self.camera_list:
  140. ret = self.camera_list[id].VZ_StartStream()
  141. if ret == 0:
  142. glog.info(self.tofs_etc[id].ipv4 + " start stream successful")
  143. else:
  144. glog.info(self.tofs_etc[id].ipv4 + ' VZ_StartStream failed: ' + str(ret))
  145. def stopCamera(self, id):
  146. if id in self.camera_list:
  147. ret = self.camera_list[id].VZ_StopStream()
  148. if ret == 0:
  149. glog.info(self.tofs_etc[id].ipv4 + " stop stream successful")
  150. else:
  151. glog.info(self.tofs_etc[id].ipv4 + ' VZ_StopStream failed: ' + str(ret))
  152. else:
  153. glog.warning("camera " + self.tofs_etc[id].ip + "not open, please open this camera")
  154. def stopAllCameraStream(self):
  155. for id in self.camera_list:
  156. ret = self.camera_list[id].VZ_StopStream()
  157. if ret == 0:
  158. glog.info(self.tofs_etc[id].ipv4 + " stop stream successful")
  159. else:
  160. glog.info(self.tofs_etc[id].ipv4 + ' VZ_StopStream failed: ' + str(ret))
  161. def getCameraFrame(self, id):
  162. if id in self.camera_list:
  163. ret, frameready = self.camera_list[id].VZ_GetFrameReady(c_uint16(1000))
  164. if ret != 0:
  165. glog.error("VZ_GetFrameReady failed: %d", ret)
  166. return
  167. if frameready.depth:
  168. ret, depthframe = self.camera_list[id].VZ_GetFrame(tof.VzFrameType.VzDepthFrame)
  169. if ret == 0:
  170. self.saveDepthFrame2Image(os.getcwd() + "/save/", "depthframe.jpg", depthframe)
  171. glog.info(self.tofs_etc[id].ipv4 + " depth frameindex: " + str(depthframe.frameIndex))
  172. else:
  173. glog.warning("VZ_GetFrame error %d", ret)
  174. if frameready.ir:
  175. ret, irframe = self.camera_list[id].VZ_GetFrame(tof.VzFrameType.VzIRFrame)
  176. if ret == 0:
  177. self.saveIrFrame2Image(os.getcwd() + "/save/", "irframe.jpg", irframe)
  178. glog.info(self.tofs_etc[id].ipv4 + " ir frameindex: " + str(irframe.frameIndex))
  179. else:
  180. glog.warning("VZ_GetFrame error %d", ret)
  181. def setCameraEtc(self, ip, tof_etc=etc.VzenseTofDevices()):
  182. glog.info("=======================")