class TestOnlyPrimitiveBuilder(unittest.TestCase): def setUp(self): self.builder = XVIZBuilder() def test_polyline(self): verts = [0., 0., 0., 4., 0., 0., 4., 3., 0.] self.builder.timestamp(1.0) self.builder.primitive('/test/polyline')\ .polyline(verts)\ .id('1')\ .style({ 'stroke_color': [255, 0, 0] }) expected = { 'timestamp': 1.0, 'primitives': { '/test/polyline': { 'polylines': [{ 'base': { 'style': { 'stroke_color': [255, 0, 0], }, 'object_id': '1' }, 'vertices': verts }] } } } data = self.builder.get_data().to_object() assert json.dumps(data, sort_keys=True) == json.dumps(expected, sort_keys=True)
class TestPrimitiveBuilder(unittest.TestCase): def setUp(self): self.builder = XVIZBuilder() setup_pose(self.builder) def test_image(self): data = bytes(b'12345') self.builder.primitive('/camera/1')\ .image(data) expected = { 'timestamp': 1.0, 'poses': { PRIMARY_POSE_STREAM: DEFAULT_POSE }, 'primitives': { '/camera/1': { 'images': [{ 'data': 'MTIzNDU=' }] } } } assert self.builder.get_data().to_object() == expected def test_polygon(self): verts = [0., 0., 0., 4., 0., 0., 4., 3., 0.] self.builder.primitive('/test/polygon')\ .polygon(verts)\ .id('1')\ .style({ 'fill_color': [255, 0, 0] }) expected = { 'timestamp': 1.0, 'poses': { PRIMARY_POSE_STREAM: DEFAULT_POSE }, 'primitives': { '/test/polygon': { 'polygons': [{ 'base': { 'style': { 'fill_color': [255, 0, 0], }, 'object_id': '1' }, 'vertices': verts }] } } } data = self.builder.get_data().to_object() assert json.dumps(data, sort_keys=True) == json.dumps(expected, sort_keys=True)
def visualize_detections(builder: XVIZBuilder, visualizer_frame: str, targets: Target3DArray, calib: TransformSet, stream_prefix: str, id_prefix="", tags=None, text_offset=None): ''' Add detection results to xviz builder ''' # change frame to the same if targets.frame != visualizer_frame: targets = calib.transform_objects(targets, frame_to=visualizer_frame) stream_prefix = stream_prefix.rstrip("/") for box in targets: vertices = box.corners[[0, 1, 3, 2, 0]] builder.primitive(stream_prefix + "/objects")\ .polygon(vertices.tolist())\ .id(box.tid64)\ .style({"height": box.dimension[2]})\ .classes([box.tag.mapping(t).name for t in box.tag.labels]) builder.primitive(stream_prefix + "/label")\ .text("#" + box.tid64)\ .position(box.position if text_offset is None else box.position + text_offset) builder.primitive(stream_prefix + "/tracking_point")\ .circle(box.position, 0.2)\ .id(box.tid64)
class TestPrimitiveBuilder(unittest.TestCase): def setUp(self): self.builder = XVIZBuilder() setup_pose(self.builder) def test_image(self): data = np.array([[1, 2], [3, 4]]) data = Image.fromarray(data.astype('u1')).convert('RGB') self.builder.primitive('/camera/1')\ .image(data) expected = { 'timestamp': 1.0, 'poses': { PRIMARY_POSE_STREAM: DEFAULT_POSE }, 'primitives': { '/camera/1': { 'images': [ { 'width_px': 2, 'height_px': 2, 'data': b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x02\x00'\ b'\x00\x00\x02\x08\x02\x00\x00\x00\xfd\xd4\x9as\x00\x00' \ b'\x00\x16IDATx\x9ccdddddddabbbdd\x04\x00\x00\x9b\x00\x15'\ b'\x86\xe2,\xcc\x00\x00\x00\x00IEND\xaeB`\x82' } ] } } } assert self.builder.get_data().to_object() == expected def test_polygon(self): verts = [0., 0., 0., 4., 0., 0., 4., 3., 0.] self.builder.primitive('/test/polygon')\ .polygon(verts)\ .id('1')\ .style({ 'fill_color': [255, 0, 0] }) expected = { 'timestamp': 1.0, 'poses': { PRIMARY_POSE_STREAM: DEFAULT_POSE }, 'primitives': { '/test/polygon': { 'polygons': [{ 'base': { 'style': { 'fill_color': [255, 0, 0], }, 'object_id': '1' }, 'vertices': verts }] } } } data = self.builder.get_data().to_object() assert json.dumps(data, sort_keys=True) == json.dumps(expected, sort_keys=True)