|
| 1 | +import logging |
1 | 2 | from pathlib import Path
|
2 |
| -from typing import Dict, Union, Optional, Literal, Callable |
| 3 | +from typing import Dict, List, Union, Optional, Literal, Callable |
3 | 4 |
|
4 | 5 | import yaml
|
5 | 6 |
|
6 | 7 |
|
7 | 8 | from dagshub_annotation_converter.formats.common import ImageType
|
8 | 9 | from dagshub_annotation_converter.formats.yolo.categories import Categories
|
9 | 10 | from dagshub_annotation_converter.ir.image import IRImageAnnotationBase
|
| 11 | +from dagshub_annotation_converter.ir.image.annotations.pose import IRPoseImageAnnotation |
10 | 12 | from dagshub_annotation_converter.util.pydantic_util import ParentModel
|
11 | 13 |
|
12 | 14 | YoloConverterFunction = Callable[
|
|
15 | 17 |
|
16 | 18 | YoloAnnotationTypes = Literal["bbox", "segmentation", "pose"]
|
17 | 19 |
|
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
18 | 22 |
|
19 | 23 | class YoloContext(ParentModel):
|
20 | 24 | annotation_type: YoloAnnotationTypes
|
@@ -106,3 +110,19 @@ def get_yaml_content(self, path_override: Optional[Path] = None) -> str:
|
106 | 110 | content["kpt_shape"] = [self.keypoints_in_annotation, self.keypoint_dim]
|
107 | 111 |
|
108 | 112 | return yaml.dump(content)
|
| 113 | + |
| 114 | + def infer_keypoints_from_annotations(self, annotations: List[IRImageAnnotationBase]): |
| 115 | + if self.annotation_type != "pose": |
| 116 | + raise ValueError("Keypoints in annotation can only be inferred for pose annotations") |
| 117 | + self.keypoints_in_annotation = 0 |
| 118 | + for ann in annotations: |
| 119 | + if isinstance(ann, IRPoseImageAnnotation): |
| 120 | + current_keypoints = len(ann.points) |
| 121 | + if current_keypoints > self.keypoints_in_annotation: |
| 122 | + if self.keypoints_in_annotation > 0: |
| 123 | + logger.warning( |
| 124 | + f"Found annotation with {current_keypoints} keypoints, " |
| 125 | + f"which is more than previously found maximum of {self.keypoints_in_annotation}.\n" |
| 126 | + "Your annotations might be inconsistent." |
| 127 | + ) |
| 128 | + self.keypoints_in_annotation = current_keypoints |
0 commit comments