|
|
import json |
|
|
import os |
|
|
import datasets |
|
|
from datasets import Features, Image, Value, Sequence |
|
|
|
|
|
class HockeyPoseConfig(datasets.BuilderConfig): |
|
|
"""BuilderConfig for HockeyPose dataset.""" |
|
|
def __init__(self, **kwargs): |
|
|
super(HockeyPoseConfig, self).__init__(**kwargs) |
|
|
|
|
|
class HockeyPose(datasets.GeneratorBasedBuilder): |
|
|
"""HockeyPose dataset: Hockey player images with pose keypoints.""" |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
BUILDER_CONFIGS = [ |
|
|
HockeyPoseConfig( |
|
|
name="hockey_pose", |
|
|
version=VERSION, |
|
|
description="Hockey pose dataset with keypoints", |
|
|
), |
|
|
] |
|
|
|
|
|
def _info(self): |
|
|
return datasets.DatasetInfo( |
|
|
description="HockeyRink keypoint dataset", |
|
|
features=Features({ |
|
|
'image': Image(), |
|
|
'image_id': Value('string'), |
|
|
'annotations': Sequence({ |
|
|
'class_id': Value('int64'), |
|
|
'bbox': Sequence(Value('float32'), length=4), |
|
|
'keypoints': Sequence( |
|
|
Sequence(Value('float32'), length=3), |
|
|
length=56 |
|
|
), |
|
|
}) |
|
|
}), |
|
|
supervised_keys=None, |
|
|
homepage="https://huggingface.co/datasets/SimulaMet-HOST/HockeyRink", |
|
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
"""Returns SplitGenerators.""" |
|
|
return [ |
|
|
datasets.SplitGenerator( |
|
|
name=datasets.Split.TRAIN, |
|
|
gen_kwargs={ |
|
|
"split": "train", |
|
|
} |
|
|
), |
|
|
] |
|
|
|
|
|
def _generate_examples(self, split): |
|
|
"""Yields examples.""" |
|
|
frames_dir = os.path.join(os.path.dirname(__file__), "..", "frames") |
|
|
annotations_dir = os.path.join(os.path.dirname(__file__), "..", "annotations") |
|
|
|
|
|
|
|
|
image_files = [f for f in os.listdir(frames_dir) |
|
|
if f.lower().endswith(('.png', '.jpg', '.jpeg'))] |
|
|
|
|
|
for idx, image_file in enumerate(sorted(image_files)): |
|
|
image_id = os.path.splitext(image_file)[0] |
|
|
image_path = os.path.join(frames_dir, image_file) |
|
|
annotation_path = os.path.join(annotations_dir, f"{image_id}.txt") |
|
|
|
|
|
if not os.path.exists(annotation_path): |
|
|
continue |
|
|
|
|
|
|
|
|
with open(annotation_path, 'r') as f: |
|
|
annotations = [] |
|
|
for line in f: |
|
|
values = [float(x) for x in line.strip().split()] |
|
|
class_id = int(values[0]) |
|
|
bbox = values[1:5] |
|
|
keypoints = [] |
|
|
|
|
|
|
|
|
for i in range(5, len(values), 3): |
|
|
keypoints.append([ |
|
|
values[i], |
|
|
values[i + 1], |
|
|
values[i + 2] |
|
|
]) |
|
|
|
|
|
annotations.append({ |
|
|
'class_id': class_id, |
|
|
'bbox': bbox, |
|
|
'keypoints': keypoints |
|
|
}) |
|
|
|
|
|
yield idx, { |
|
|
'image': image_path, |
|
|
'image_id': image_id, |
|
|
'annotations': annotations |
|
|
} |