def _to_xml(self): """ Convert object to xml. Return: Element whose tag is "object" with appropriate sub-elements (id, category, bounds, and pose) """ base_elem = ET.Element("element") id_elem = ET.SubElement(base_elem, "id") id_elem.text = self.id category_elem = ET.SubElement(base_elem, "category") category_elem.text = self.category if self.project_type == "meshes": compute_bbox_alg = ComputeBbox() bounds = compute_bbox_alg.from_meshes( self.meshes.primitive_meshes()) elif self.project_type == "voxels": bounds = self.voxels.bounds() elif self.project_type == "bounding_box": bounds = self.bounds bounds_xml = bounds.to_xml() bounds_xml.tag = "bounds" base_elem.append(bounds_xml) base_elem.append(self.pose.to_xml()) base_elem.append(self.symmetry.to_xml()) score_elem = ET.SubElement(base_elem, "detectionScore") score_elem.text = str(self.score) return base_elem
def __init__(self, submission, ground_truth, settings=None): """ Constructor. Computes similarity between all elements in the submission and ground_truth and also computes data association caches. Inputs: submission (ProjectScene) - Submitted scene to be evaluated ground_truth (ProjectScene) - The ground truth scene settings (dict) - configuration for the evaluator. See Evaluator.py for recognized keys and values. """ # sample submission and GT and store in points field # also compute bounding box of each posed object for e in submission.elements.values(): e.posed_points = sample_element(e, settings["density"]) e.posed_bbox = ComputeBbox().from_point_cloud( e.posed_points[:, 0:3].T) for e in ground_truth.elements.values(): e.posed_points = sample_element(e, settings["density"]) e.posed_bbox = ComputeBbox().from_point_cloud( e.posed_points[:, 0:3].T) super(MeshEvaluator, self).__init__(submission, ground_truth, settings)
def test_from_mesh(self): """Make a mesh and verify the bounding box's corners are correct.""" indices = np.array([0, 1, 2, 1, 3, 2, 2, 3, 4], dtype=np.uint32) vertices = np.column_stack( [ Vector3f(0, 0, 0), Vector3f(1, 0, 0), Vector3f(0, 1, 0), Vector3f(1, 1, 0), Vector3f(1, 1, 1), ] ) normals = np.column_stack([Vector3f(0, 0, 1)] * 5) self.assertEqual(vertices.shape, normals.shape) self.assertEqual(indices.shape, (9,)) mesh = Mesh(indices, vertices, normals) box = ComputeBbox().from_mesh(mesh) corners = box.corners() corners_target = np.column_stack( [ [0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0], ] ) np.testing.assert_array_equal(corners, corners_target)
def test_from_gltf_model(self): """Verify bounding box computed from a GltfModel.""" object = GltfModel.example() box = ComputeBbox().from_gltf_object(object) np.testing.assert_array_equal(box.min_corner, Vector3(-1, -1, -1)) np.testing.assert_array_equal(box.max_corner, Vector3(1, 1, 1))
def test_empty(self): """ Test computing bbox for an empty point cloud. This should produce a default bbox with corners at (0,0,0). """ points = np.ndarray(shape=(3, 0)) box = ComputeBbox().from_point_cloud(points) np.testing.assert_array_equal(box.min_corner, np.array([0, 0, 0])) np.testing.assert_array_equal(box.max_corner, np.array([0, 0, 0]))
def test_random_points(self): """ Make three random points and verify the bounding box's corners are correct """ point1 = Vector3f(-1, -2, -3) point2 = Vector3f(0, 2, 1) point3 = Vector3f(1, 2, 3) points = np.column_stack([point1, point2, point3]) box = ComputeBbox().from_point_cloud(points) corners = box.corners() corners_target = np.column_stack([ [-1.0, -2.0, 3.0], [1.0, -2.0, 3.0], [1.0, 2.0, 3.0], [-1.0, 2.0, 3.0], [-1.0, -2.0, -3.0], [1.0, -2.0, -3.0], [1.0, 2.0, -3.0], [-1.0, 2.0, -3.0], ]) np.testing.assert_array_equal(corners, corners_target)
def test_unit_cube(self): """ Make three points inside a unit cube and verify the bounding box's corners are correct """ point1 = Vector3(1, 0, 0) point2 = Vector3(0, 1, 0) point3 = Vector3(0, 0, 1) points = np.column_stack([point1, point2, point3]) box = ComputeBbox().from_point_cloud(points) corners = box.corners() corners_target = np.column_stack([ [0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0], ]) np.testing.assert_array_equal(corners, corners_target)
def convert_element(self, element, target_type): """ Convert <element> to <target_type> track. Makes a copy of the element. Inputs: element (ProjectObject) - element to convert target_type (string) - destination project type Return new_element (ProjectObject) - converted element See above for allowed conversions. """ if (element.project_type, target_type) not in self.allowed_conversions: raise ValueError("Invalid target_type ({}) for element with type \ {}".format(target_type, element.project_type)) source_type = element.project_type if target_type == "bounding_box": if source_type == "voxels": bounds = element.voxels.bounds() elif source_type == "meshes": bounds = ComputeBbox().from_gltf_object(element.meshes) else: raise ValueError( "Invalid target type") # this should not be possible new_element = ProjectObject.gen_bounding_box_object( id=element.id, bounds=bounds, pose=deepcopy(element.pose), category=element.category, symmetry=element.symmetry, score=element.score, evaluated=element.evaluated) elif target_type == "voxels": voxelizer = Voxelizer() voxels = voxelizer.run(element.meshes) new_element = ProjectObject.gen_voxels_object( id=element.id, bounds=voxels.bounds(), voxels=voxels, pose=deepcopy(element.pose), category=element.category, symmetry=element.symmetry, score=element.score, evaluated=element.evaluated) else: raise ValueError( "Invalid target type") # this should not be possible return new_element
def __init__(self, submission, ground_truth, settings=None): """ Constructor. Computes similarity between all elements in the submission and ground_truth and also computes data association caches. Inputs: submission (ProjectScene) - Submitted scene to be evaluated ground_truth (ProjectScene) - The ground truth scene settings (dict) - configuration for the evaluator. See Evaluator.py for recognized keys and values. """ # extract posed bounds and save # (used for IoU calcs) for e in submission.elements.values(): posed_corners = e.pose.transform_all_from(e.bounds.corners()) e.posed_bbox = ComputeBbox().from_point_cloud(posed_corners) for e in ground_truth.elements.values(): posed_corners = e.pose.transform_all_from(e.bounds.corners()) e.posed_bbox = ComputeBbox().from_point_cloud(posed_corners) super(BBEvaluator, self).__init__(submission, ground_truth, settings)