Esempio n. 1
0
 def _convert_camera_frame_to_image_dict(
         self, camera_frame: imagery_pb2.CameraFrame):
     """Converts the camera frame to an image dictionary."""
     # Each camera frame might contain multiple image channels, such as rgb and
     # depth.
     images = {}
     for image_name, image_proto in camera_frame.images.items():
         image_array = imagery_utils.convert_image_to_array(image_proto)
         images[image_name] = image_array
     return images
    def test_convert_gray_16bit_images(self):
        image = imagery_pb2.Image(
            height_px=3,
            width_px=2,
            image_format=imagery_pb2.Image.IMAGE_FORMAT_GRAY_HW_16U,
            content=b'AABBCCAABBCC',
        )

        image_array = imagery_utils.convert_image_to_array(image)

        self.assertEqual(image_array.dtype, np.uint16)
        self.assertEqual(image_array.shape, (image.height_px, image.width_px))
        self.assertEqual(image_array[0, 1], struct.unpack(b'<H', b'BB'))
        self.assertEqual(image_array[2, 1], struct.unpack(b'<H', b'CC'))
    def test_convert_gray_32bit_images(self):
        image = imagery_pb2.Image(
            height_px=2,
            width_px=3,
            image_format=imagery_pb2.Image.IMAGE_FORMAT_GRAY_HW_32F,
            content=b'AAAABBBBCCCCAAAABBBBCCCC',
        )

        image_array = imagery_utils.convert_image_to_array(image)

        self.assertEqual(image_array.dtype, np.float32)
        self.assertEqual(image_array.shape, (image.height_px, image.width_px))
        self.assertEqual(image_array[0, 2], struct.unpack(b'<f', b'CCCC'))
        self.assertEqual(image_array[1, 1], struct.unpack(b'<f', b'BBBB'))
    def test_convert_rgb_images(self):
        image = imagery_pb2.Image(
            height_px=2,
            width_px=2,
            image_format=imagery_pb2.Image.IMAGE_FORMAT_RGB_HWC_8U,
            content=b'ABCABCABCABC',
        )

        image_array = imagery_utils.convert_image_to_array(image)

        self.assertEqual(image_array.dtype, np.uint8)
        self.assertEqual(image_array.shape,
                         (image.height_px, image.width_px, 3))
        self.assertEqual(image_array[0, 0, 0], ord('A'))
        self.assertEqual(image_array[1, 1, 2], ord('C'))
    def test_convert_bgra_images(self):
        image = imagery_pb2.Image(
            height_px=2,
            width_px=2,
            image_format=imagery_pb2.Image.IMAGE_FORMAT_BGRA_HWC_8U,
            content=b'ABCDABCDABCDABCD',
        )

        image_array = imagery_utils.convert_image_to_array(image)

        self.assertEqual(image_array.dtype, np.uint8)
        self.assertEqual(image_array.shape,
                         (image.height_px, image.width_px, 4))
        self.assertEqual(image_array[0, 0, 0], ord('A'))
        self.assertEqual(image_array[1, 0, 3], ord('D'))
 def test_unspecified_image_format(self):
     image = imagery_pb2.Image()
     with self.assertRaises(ValueError):
         imagery_utils.convert_image_to_array(image)