def _get_data(keypoints_info: List[str], image_path: str, parsing_path: str) -> Data: stem = os.path.splitext(keypoints_info[0])[0] data = Data(os.path.join(image_path, f"{stem}.jpg")) label = data.label label.semantic_mask = SemanticMask( os.path.join(parsing_path, f"{stem}.png")) keypoints = LabeledKeypoints2D() for x, y, v in chunked(islice(keypoints_info, 1, None), 3): keypoints.append( Keypoint2D(float(x), float(y), 1 - int(v)) if x.isnumeric() else Keypoint2D(0, 0, 0)) label.keypoints2d = [keypoints] return data
def test_init(self): labeledkeypoints2d = LabeledKeypoints2D( [(1, 2)], category="cat", attributes={"gender": "male"}, instance="12345" ) assert labeledkeypoints2d[0] == Keypoint2D(x=1, y=2) assert labeledkeypoints2d.category == "cat" assert labeledkeypoints2d.attributes == {"gender": "male"} assert labeledkeypoints2d.instance == "12345"
def _get_data(image_path: str, annotation: Dict[str, Any]) -> Data: data = Data(image_path) keypoints = LabeledKeypoints2D() for x, y, v in chunked(annotation["keypoints"], 3): keypoints.append(Keypoint2D(x, y, v if v in (0, 1, 2) else 2)) data.label.keypoints2d = [keypoints] return data
def LeedsSportsPose(path: str) -> Dataset: """`Leeds Sports Pose <http://sam.johnson.io/research/lsp.html>`_ dataset. The folder structure should be like:: <path> joints.mat images/ im0001.jpg im0002.jpg ... Arguments: path: The root directory of the dataset. Raises: ModuleImportError: When the module "scipy" can not be found. Returns: Loaded :class:`~tensorbay.dataset.dataset.Dataset` instance. """ try: from scipy.io import loadmat # pylint: disable=import-outside-toplevel except ModuleNotFoundError as error: raise ModuleImportError(module_name=error.name) from error root_path = os.path.abspath(os.path.expanduser(path)) dataset = Dataset(DATASET_NAME) dataset.load_catalog( os.path.join(os.path.dirname(__file__), "catalog.json")) segment = dataset.create_segment() mat = loadmat(os.path.join(root_path, "joints.mat")) joints = mat["joints"].T image_paths = glob(os.path.join(root_path, "images", "*.jpg")) for image_path in image_paths: data = Data(image_path) data.label.keypoints2d = [] index = int(os.path.basename(image_path) [2:6]) - 1 # get image index from "im0001.jpg" keypoints = LabeledKeypoints2D() for keypoint in joints[index]: keypoints.append( Keypoint2D(keypoint[0], keypoint[1], int(not keypoint[2]))) data.label.keypoints2d.append(keypoints) segment.append(data) return dataset
def _get_label(eye_keypoints_path: str, face_keypoints_path: str) -> List[LabeledKeypoints2D]: eye_keypoints = LabeledKeypoints2D(category="EyePosition") with open(eye_keypoints_path, "r", encoding="utf-8") as fp: fp.readline() # The first line is like: #LX LY RX RY lx, ly, rx, ry = map(int, fp.readline().split()) eye_keypoints.append(Keypoint2D(lx, ly)) eye_keypoints.append(Keypoint2D(rx, ry)) face_keypoints = LabeledKeypoints2D(category="Face") with open(face_keypoints_path, "r", encoding="utf-8") as fp: # The annotation file is like: # 1 version: 1 # 2 n_points: 20 # 3 { # 4 159.128 108.541 # ... # 24 } for line in islice(fp, 3, 23): x, y = map(float, line.split()) face_keypoints.append(Keypoint2D(x, y)) return [eye_keypoints, face_keypoints]
def test_loads(self): contents = { "keypoints2d": [ {"x": 1, "y": 1, "v": 2}, ], "category": "cat", "attributes": {"gender": "male"}, "instance": "12345", } labeledkeypoints2d = LabeledKeypoints2D.loads(contents) assert labeledkeypoints2d[0] == Keypoint2D(x=1, y=1, v=2) assert labeledkeypoints2d.category == "cat" assert labeledkeypoints2d.attributes == {"gender": "male"} assert labeledkeypoints2d.instance == "12345"
def _get_data_part2(root_path: str, aniamls: Iterable[str]) -> Iterator[Data]: try: import xmltodict # pylint: disable=import-outside-toplevel except ModuleNotFoundError as error: raise ModuleImportError(module_name=error.name) from error for animal in aniamls: for image_path in glob( os.path.join(root_path, "animalpose_image_part2", animal, "*.jpeg")): data = Data( image_path, target_remote_path=f"{animal}/{os.path.basename(image_path)}") annotation_path = os.path.join( root_path, "animalpose_anno2", animal, f"{os.path.splitext(os.path.basename(image_path))[0]}.xml", ) with open(annotation_path, encoding="utf-8") as fp: labels: Any = xmltodict.parse(fp.read()) box2d = labels["annotation"]["visible_bounds"] data.label.box2d = [ LabeledBox2D.from_xywh( x=float(box2d["@xmin"]), y=float( box2d["@xmax"]), # xmax means ymin in the annotation width=float(box2d["@width"]), height=float(box2d["@height"]), category=animal, ) ] keypoints2d = LabeledKeypoints2D(category=animal) for keypoint in labels["annotation"]["keypoints"]["keypoint"]: keypoints2d.append( Keypoint2D(float(keypoint["@x"]), float(keypoint["@y"]), int(keypoint["@visible"]))) data.label.keypoints2d = [keypoints2d] yield data
def test_loads(self): keypoints = Keypoints2D.loads(_DATA_KEYPOINTS) assert keypoints._data == [ Keypoint2D(1.0, 1.0, 1), Keypoint2D(2.0, 2.0, 2) ]
def test_init(self): sequence = [[1, 2], [2, 3]] assert Keypoints2D(None) == Keypoints2D([]) result = Keypoints2D([Keypoint2D(1, 2), Keypoint2D(2, 3)]) assert Keypoints2D(sequence) == result assert Keypoints2D(np.array(sequence)) == result
def test_dumps(self): keypoint = Keypoint2D(1, 1, 1) assert keypoint.dumps() == _DATA_KEYPOINT
def test_loads(self): keypoint = Keypoint2D.loads(_DATA_KEYPOINT) assert keypoint._data == (1, 1, 1)
def test_neg(self): keypoint = Keypoint2D(1, 1, 1) assert -keypoint == Vector2D(-1, -1)
def test_init(self): assert Keypoint2D(*[1, 1, 1]) == Keypoint2D(1, 1, 1) assert Keypoint2D(x=1, y=1, v=1) == Keypoint2D(1, 1, 1) keypoint = Keypoint2D(1, 1, 1) assert keypoint.v == 1
def test_init(self): sequence = [[1, 2], [2, 3]] assert Keypoints2D(None) == Keypoints2D([]) assert Keypoints2D(sequence) == Keypoints2D([Keypoint2D(1, 2), Keypoint2D(2, 3)])