Skip to content

Commit 9ab7cf2

Browse files
committed
Move the point counting to the yolo context
1 parent 73b122b commit 9ab7cf2

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ dist/
55

66
.DS_Store
77
.idea/
8+
.vscode/
89
__pycache__/

dagshub_annotation_converter/converters/yolo.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,7 @@ def export_to_fs(
208208

209209
# For pose annotations, if the number of keypoints is not set, guess it from the passed annotations
210210
if context.annotation_type == "pose" and context.keypoints_in_annotation is None:
211-
max_keypoints = 0
212-
for anns in grouped_annotations.values():
213-
for ann in anns:
214-
if isinstance(ann, IRPoseImageAnnotation):
215-
current_keypoints = len(ann.points)
216-
if current_keypoints > max_keypoints:
217-
if max_keypoints > 0:
218-
logger.warning(
219-
f"Found annotation with {current_keypoints} keypoints, "
220-
f"which is more than previously found maximum of {max_keypoints}.\n"
221-
"Your annotations might be inconsistent."
222-
)
223-
max_keypoints = current_keypoints
224-
context.keypoints_in_annotation = max_keypoints
211+
context.infer_keypoints_from_annotations(annotations)
225212

226213
yaml_file_path = export_path / meta_file
227214
with open(yaml_file_path, "w") as yaml_f:

dagshub_annotation_converter/formats/yolo/context.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import logging
12
from pathlib import Path
2-
from typing import Dict, Union, Optional, Literal, Callable
3+
from typing import Dict, List, Union, Optional, Literal, Callable
34

45
import yaml
56

67

78
from dagshub_annotation_converter.formats.common import ImageType
89
from dagshub_annotation_converter.formats.yolo.categories import Categories
910
from dagshub_annotation_converter.ir.image import IRImageAnnotationBase
11+
from dagshub_annotation_converter.ir.image.annotations.pose import IRPoseImageAnnotation
1012
from dagshub_annotation_converter.util.pydantic_util import ParentModel
1113

1214
YoloConverterFunction = Callable[
@@ -15,6 +17,8 @@
1517

1618
YoloAnnotationTypes = Literal["bbox", "segmentation", "pose"]
1719

20+
logger = logging.getLogger(__name__)
21+
1822

1923
class YoloContext(ParentModel):
2024
annotation_type: YoloAnnotationTypes
@@ -106,3 +110,19 @@ def get_yaml_content(self, path_override: Optional[Path] = None) -> str:
106110
content["kpt_shape"] = [self.keypoints_in_annotation, self.keypoint_dim]
107111

108112
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

Comments
 (0)