read_depthimage.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 23 Jan 2022
  4. @author: Edward Hage
  5. """
  6. import numpy as np
  7. import cv2
  8. import struct
  9. from dds import Listener, DomainParticipant, Topic, QosProvider, Qos
  10. from dds import PresentationQosPolicy, PartitionQosPolicy, DDSPresentationAccessScopeKind, DurabilityQosPolicy,DDSDurabilityKind
  11. from idlpython.sensor_msgs.Image_.sensor_msgs.msg.dds_ import Image___TypeSupport
  12. class ImageReader():
  13. class ImageListener(Listener):
  14. def __init__(self):
  15. Listener.__init__(self)
  16. def on_data_available(self, entity):
  17. l = entity.take(1)
  18. print("here")
  19. for (sd, si) in l:
  20. current_frame = self.convert(sd)
  21. clipmin = 0.3
  22. clipmax = 2.0
  23. new_frame = current_frame.copy()
  24. np.clip(new_frame, clipmin, clipmax, out=new_frame)
  25. new_frame -= clipmin
  26. new_frame /= (clipmax-clipmin)/255.0
  27. new_frame = new_frame.astype(dtype=np.uint8, copy=False)
  28. im_color = cv2.applyColorMap(new_frame, cv2.COLORMAP_AUTUMN)
  29. cv2.imshow("camera", current_frame) # of new_frame
  30. cv2.waitKey(1) # 1 for automatic update
  31. def convert(self, img_msg):
  32. # dtype, n_channels = "32F", 1 # hardcoded data from Unity
  33. # dtype = np.dtype(dtype)
  34. # dtype = dtype.newbyteorder('>' if img_msg.is_bigendian_ else '<')
  35. # print( dtype)
  36. # print("totsize = " + str(len(img_msg.data_)) + " height = " + str(img_msg.height_) + " width " + str(img_msg.width_))
  37. dt = np.dtype('float32')
  38. dt = dt.newbyteorder('<')
  39. #im = np.ndarray(shape=(img_msg.height_, img_msg.width_, 1), dtype = 'float32', buffer = bytearray(img_msg.data_))
  40. im = np.ndarray(shape=(img_msg.height_, img_msg.width_, 1), dtype = dt, buffer = bytearray(img_msg.data_))
  41. return im
  42. listener = ImageListener()
  43. def __init__(self, topicname):
  44. qp = QosProvider('file://DDS_ROSQoS.xml', 'DDS ROSQosProfile')
  45. dp = DomainParticipant(qos = qp.get_participant_qos())
  46. sub = dp.create_subscriber(Qos([PresentationQosPolicy(DDSPresentationAccessScopeKind.INSTANCE, False, False),
  47. PartitionQosPolicy(['']),
  48. DurabilityQosPolicy(DDSDurabilityKind.VOLATILE)]))
  49. topic = Topic(dp, topicname, Image___TypeSupport(), qp.get_topic_qos(), None)
  50. self.reader = sub.create_datareader(topic, qp.get_reader_qos(), self.listener)
  51. reader = ImageReader("rt/depth_processor")
  52. input("Press a key to stop ...")