| | import os |
| | from xml.etree import ElementTree as ET |
| |
|
| | import datasets |
| |
|
| | _CITATION = """\ |
| | @InProceedings{huggingface:dataset, |
| | title = {fights-segmentation}, |
| | author = {TrainingDataPro}, |
| | year = {2023} |
| | } |
| | """ |
| |
|
| | _DESCRIPTION = """\ |
| | The dataset consists of a collection of photos extracted from **videos of fights**. |
| | It includes **segmentation masks** for **fighters, referees, mats, and the background**. |
| | The dataset offers a resource for *object detection, instance segmentation, |
| | action recognition, or pose estimation*. |
| | It could be useful for **sport community** in identification and detection of |
| | the violations, dispute resolution and general optimisation of referee's work using |
| | computer vision. |
| | """ |
| | _NAME = "fights-segmentation" |
| |
|
| | _HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}" |
| |
|
| | _LICENSE = "" |
| |
|
| | _DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/" |
| |
|
| | _LABELS = ["referee", "background", "wrestling", "human"] |
| |
|
| |
|
| | class FightsSegmentation(datasets.GeneratorBasedBuilder): |
| | BUILDER_CONFIGS = [ |
| | datasets.BuilderConfig(name="video_01", data_dir=f"{_DATA}video_01.zip"), |
| | datasets.BuilderConfig(name="video_02", data_dir=f"{_DATA}video_02.zip"), |
| | datasets.BuilderConfig(name="video_03", data_dir=f"{_DATA}video_03.zip"), |
| | ] |
| |
|
| | DEFAULT_CONFIG_NAME = "video_01" |
| |
|
| | def _info(self): |
| | return datasets.DatasetInfo( |
| | description=_DESCRIPTION, |
| | features=datasets.Features( |
| | { |
| | "id": datasets.Value("int32"), |
| | "name": datasets.Value("string"), |
| | "image": datasets.Image(), |
| | "mask": datasets.Image(), |
| | "width": datasets.Value("uint16"), |
| | "height": datasets.Value("uint16"), |
| | "shapes": datasets.Sequence( |
| | { |
| | "label": datasets.ClassLabel( |
| | num_classes=len(_LABELS), |
| | names=_LABELS, |
| | ), |
| | "type": datasets.Value("string"), |
| | "points": datasets.Sequence( |
| | datasets.Sequence( |
| | datasets.Value("float"), |
| | ), |
| | ), |
| | "rotation": datasets.Value("float"), |
| | "occluded": datasets.Value("uint8"), |
| | "z_order": datasets.Value("int16"), |
| | "attributes": datasets.Sequence( |
| | { |
| | "name": datasets.Value("string"), |
| | "text": datasets.Value("string"), |
| | } |
| | ), |
| | } |
| | ), |
| | } |
| | ), |
| | supervised_keys=None, |
| | homepage=_HOMEPAGE, |
| | citation=_CITATION, |
| | ) |
| |
|
| | def _split_generators(self, dl_manager): |
| | data = dl_manager.download_and_extract(self.config.data_dir) |
| | return [ |
| | datasets.SplitGenerator( |
| | name=datasets.Split.TRAIN, |
| | gen_kwargs={ |
| | "data": data, |
| | }, |
| | ), |
| | ] |
| |
|
| | @staticmethod |
| | def parse_shape(shape: ET.Element) -> dict: |
| | label = shape.get("label") |
| | shape_type = shape.tag |
| | rotation = shape.get("rotation", 0.0) |
| | occluded = shape.get("occluded", 0) |
| | z_order = shape.get("z_order", 0) |
| |
|
| | points = None |
| |
|
| | if shape_type == "points": |
| | points = tuple(map(float, shape.get("points").split(","))) |
| |
|
| | elif shape_type == "box": |
| | points = [ |
| | (float(shape.get("xtl")), float(shape.get("ytl"))), |
| | (float(shape.get("xbr")), float(shape.get("ybr"))), |
| | ] |
| |
|
| | elif shape_type == "polygon": |
| | points = [ |
| | tuple(map(float, point.split(","))) |
| | for point in shape.get("points").split(";") |
| | ] |
| |
|
| | attributes = [] |
| |
|
| | for attr in shape: |
| | attr_name = attr.get("name") |
| | attr_text = attr.text |
| | attributes.append({"name": attr_name, "text": attr_text}) |
| |
|
| | shape_data = { |
| | "label": label, |
| | "type": shape_type, |
| | "points": points, |
| | "rotation": rotation, |
| | "occluded": occluded, |
| | "z_order": z_order, |
| | "attributes": attributes, |
| | } |
| |
|
| | return shape_data |
| |
|
| | def _generate_examples(self, data): |
| | tree = ET.parse(os.path.join(data, "annotations.xml")) |
| | root = tree.getroot() |
| |
|
| | for idx, file in enumerate(sorted(os.listdir(os.path.join(data, "images")))): |
| | image_name = file.split("/")[-1] |
| | img = root.find(f"./image[@name='images/{image_name}']") |
| |
|
| | image_id = img.get("id") |
| | name = img.get("name") |
| | width = img.get("width") |
| | height = img.get("height") |
| | shapes = [self.parse_shape(shape) for shape in img] |
| |
|
| | yield idx, { |
| | "id": image_id, |
| | "name": name, |
| | "image": os.path.join(data, "images", file), |
| | "mask": os.path.join(data, "masks", file), |
| | "width": width, |
| | "height": height, |
| | "shapes": shapes, |
| | } |
| |
|