RobotsIO
Loading...
Searching...
No Matches
receiver.py
1import cv2
2import numpy
3import robotsio
4import struct
5import yarp
6
7
8def main():
9
10 yarp.Network.init()
11
12 images_in = robotsio.BufferedPortYarpImageOfMonoFloat()
13 images_in.open('/hyperpcr/depth:i')
14
15 width = # set width here
16 height = # set height here
17
18 depth_buffer = bytearray(numpy.zeros((height, width, 1), dtype = numpy.float32))
19 depth_image = yarp.ImageFloat()
20 depth_image.resize(width, height)
21 depth_image.setExternal(depth_buffer, width, height)
22
23 mask_buffer = bytearray(numpy.zeros((height, width, 1), dtype = numpy.uint8))
24 mask_image = yarp.ImageMono()
25 mask_image.resize(width, height)
26 mask_image.setExternal(mask_buffer, width, height)
27
28 images_data = None
29 while images_data is None:
30 images_data = images_in.read(False)
31
32 if images_data is not None:
33 mask_image.copy(images_data.image_mono)
34 depth_image.copy(images_data.image_float)
35
36 depth_frame = numpy.frombuffer(depth_buffer, dtype=numpy.float32).reshape(height, width)
37 mask_frame = numpy.frombuffer(mask_buffer, dtype=numpy.uint8).reshape(height, width)
38
39 # save
40 cv2.imwrite('./mask.png', mask_frame)
41
42 depth_file = open('depth.float', "wb")
43 depth_file.write(struct.pack('=Q', width))
44 depth_file.write(struct.pack('=Q', height))
45 depth_file.write(depth_frame.astype('float32', order='C').tobytes())
46 depth_file.close()
47
48 print('received')
49
50 break
51
52if __name__ == '__main__':
53 main()