def test_from_strings(self): """Test creation from strings""" strings = ["none", "none", "twoFold"] sym = ObjectSymmetry.from_strings(strings) expected_sym = ObjectSymmetry(SymmetryType.none, SymmetryType.none, SymmetryType.twoFold) self.assertEqual(sym, expected_sym)
def test_is_valid(self): """Test the is_valid function""" self.assertTrue( ObjectSymmetry.is_valid(SymmetryType.cylindrical, SymmetryType.twoFold, SymmetryType.twoFold)) self.assertFalse( ObjectSymmetry.is_valid(SymmetryType.none, SymmetryType.cylindrical, SymmetryType.fourFold))
def test_creation(self): """Test basic functionality: default creation, general creation, and attribute access.""" sym = ObjectSymmetry() self.assertEqual(sym.x_symmetry, SymmetryType.none) self.assertEqual(sym.y_symmetry, SymmetryType.none) self.assertEqual(sym.z_symmetry, SymmetryType.none) sym = ObjectSymmetry(SymmetryType.twoFold, SymmetryType.twoFold, SymmetryType.fourFold) self.assertEqual(sym.x_symmetry, SymmetryType.twoFold) self.assertEqual(sym.y_symmetry, SymmetryType.twoFold) self.assertEqual(sym.z_symmetry, SymmetryType.fourFold)
def __init__(self, id, project_type="bounding_box", bounds=None, voxels=None, meshes=None, pose=None, category="unknown", symmetry=None, score=-1, evaluated=True): """ Constructor. Preferred method of creation is one of the factory methods: gen_bounding_box_object, gen_voxels_object, or gen_meshes_object. For this constructor, representation is selected based on the <project_type>: bounding box track: bounds is used voxels track: voxels and bounds are used mesh track: meshes and bounds are used Inputs: id (string) - Unique identifier for the object project_type (string) - Specifies the project type to construct (valid values are "bounding_box", "voxels", or "meshes") bounds (Box3d) - Object bounding box in local coordinates voxels (VoxelGrid) - Object voxel shape in local coordinates meshes (GltfModel) - Object mesh shape and appearance in local coordinates pose (Pose3) - Transforms object from local coordinates to world coordinates category (string) - Object category (e.g., chair, bookcase, etc.) symmetry (ObjectSymmetry) - Object symmetry description score (float) - Detection score evaluated (Boolean) - Indicates whether this object will be used in evaluation metric. Only relevant for ground truth scenes. Exceptions: ValueError - if project_type is not one of the allowed values. """ # ensure id is unicode string, idiom below is python2/3 compatible self._id = id.decode('UTF-8') if hasattr(id, 'decode') else id self._project_type = project_type self.pose = pose if pose is not None else Pose3() self.category = category self.symmetry = symmetry if symmetry is not None else ObjectSymmetry() self.score = score self.evaluated = evaluated if project_type == "bounding_box": self.bounds = bounds self.voxels = None self.meshes = None elif project_type == "voxels": self.bounds = bounds self.voxels = voxels self.meshes = None elif project_type == "meshes": self.bounds = bounds self.voxels = None self.meshes = meshes else: raise ValueError("Invalid project_type: " + project_type)
def test_xml(self): """Test converting to and from xml.""" s = b"<symmetry><x>twoFold</x><y>cylindrical</y><z>twoFold</z></symmetry>" expected_sym = ObjectSymmetry(SymmetryType.twoFold, SymmetryType.cylindrical, SymmetryType.twoFold) # xml -> object xml = ET.fromstring(s) sym = ObjectSymmetry.from_xml(xml) print("sym=", sym) print("expected=", expected_sym) self.assertEqual(sym, expected_sym) # object -> xml xml = sym.to_xml() self.assertEqual(ET.tostring(xml), s)
def test_setting(self): """Test setting elements.""" po = ProjectObject.gen_meshes_object(id="-1", meshes=self.meshes) po.pose = self.pose po.category = "table" po.symmetry = ObjectSymmetry.example() po.score = 0.23 self.assertTrue(isinstance(po.pose, Pose3)) self.assertAlmostEqual(po.pose, self.pose) self.assertTrue(isinstance(po.meshes, GltfModel)) self.assertTrue(isinstance(po.category, "".__class__)) self.assertTrue(isinstance(po.symmetry, ObjectSymmetry)) self.assertEqual(po.category, "table") self.assertEqual(po.id, "-1") self.assertEqual(po.symmetry, ObjectSymmetry.example()) self.assertAlmostEqual(po.score, 0.23)
def example(cls, id="1"): """Create a simple ProjectObject of project_type = meshes.""" meshes = GltfModel.example() pose = Pose3(t=Vector3(1, 2, 3)) symmetry = ObjectSymmetry.example() return cls.gen_meshes_object(id=id, pose=pose, category="chair", meshes=meshes, symmetry=symmetry, score=0.57)
def _parse_xml(base_elem): """ Parse the xml of an <element> tag, extracting the ProjectObject attributes. Inputs: base_elem (ET.Element) - An Element with tag "element" and appropriate sub-elements. Return: tuple (id, pose, category, bounds, symmetry, score, evaluated) ProjectObject attributes (see constructor for details). Exceptions: ValueError - If base_elem is not <element> or if none of its children is <id>. """ # verify base_elem tag is 'element' if base_elem.tag != "element": raise ValueError('Expected tag to be "element"') # defaults proxy = ProjectObject(1) category = proxy.category pose = proxy.pose symmetry = proxy.symmetry score = proxy.score evaluated = proxy.evaluated for elem in base_elem: if elem.tag == "id": id = elem.text elif elem.tag == "pose": pose = Pose3.from_xml(elem) elif elem.tag == "category": category = elem.text elif (elem.tag == "bounds"): # Note: Boxe3d.from_xml expects tag to be 'box3d' elem_temp = deepcopy(elem) elem_temp.tag = "box3d" bounds = Box3d.from_xml(elem_temp) elif elem.tag == "symmetry": symmetry = ObjectSymmetry.from_xml(elem) elif elem.tag == "detectionScore": score = float(elem.text) elif elem.tag == "evaluated": evaluated = bool(elem.text) if id is None: raise ValueError("XML is missing required <id> tag.") return (id, pose, category, bounds, symmetry, score, evaluated)
def test_constructor(self): """ Test constructor. """ po = ProjectObject(id="1", project_type="bounding_box", bounds=self.bounds) self.assertTrue(isinstance(po.pose, Pose3)) self.assertTrue(isinstance(po.category, "".__class__)) po.pose.assert_almost_equal(Pose3()) self.assertTrue(po.bounds.almost_equal(self.bounds)) self.assertIs(po.meshes, None) self.assertIs(po.voxels, None) self.assertTrue(po.category == "unknown") self.assertEqual(po.id, "1") self.assertEqual(po.symmetry, ObjectSymmetry()) self.assertAlmostEqual(po.score, -1)