cppe-5 / cppe_5_backup.py
qgyd2021's picture
[update]add dataset.tar.gz to speed up download
c53e62d
raw
history blame contribute delete
No virus
3.66 kB
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from glob import glob
import json
import os
from pathlib import Path
import datasets
from PIL import Image
# _URL = "https://drive.google.com/uc?id=1MGnaAfbckUmigGUvihz7uiHGC6rBIbvr"
_HOMEPAGE = "https://sites.google.com/view/cppe5"
_LICENSE = "Unknown"
_CATEGORIES = ["Coverall", "Face_Shield", "Gloves", "Goggles", "Mask"]
_CITATION = """\
@misc{dagli2021cppe5,
title={CPPE-5: Medical Personal Protective Equipment Dataset},
author={Rishit Dagli and Ali Mustufa Shaikh},
year={2021},
eprint={2112.09569},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
"""
_DESCRIPTION = """\
CPPE - 5 (Medical Personal Protective Equipment) is a new challenging dataset with the goal
to allow the study of subordinate categorization of medical personal protective equipments,
which is not possible with other popular data sets that focus on broad level categories.
"""
class CPPE5(datasets.GeneratorBasedBuilder):
"""CPPE - 5 dataset."""
VERSION = datasets.Version("1.0.0")
def _info(self):
features = datasets.Features(
{
"image_id": datasets.Value("int64"),
"image": datasets.Image(),
"width": datasets.Value("int32"),
"height": datasets.Value("int32"),
"objects": datasets.Sequence(
feature=datasets.Features({
"id": datasets.Value("int64"),
"area": datasets.Value("int64"),
"bbox": datasets.Sequence(datasets.Value("float32"), length=4),
"category": datasets.ClassLabel(names=_CATEGORIES),
})
),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
train_json = dl_manager.download("data/annotations/train.jsonl")
test_json = dl_manager.download("data/annotations/test.jsonl")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"archive_path": train_json,
"dl_manager": dl_manager,
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"archive_path": test_json,
"dl_manager": dl_manager,
},
),
]
def _generate_examples(self, archive_path, dl_manager):
"""Yields examples."""
archive_path = Path(archive_path)
idx = 0
with open(archive_path, "r", encoding="utf-8") as f:
for row in f:
sample = json.loads(row)
file_path = sample["image"]
file_path = dl_manager.download(file_path)
with open(file_path, "rb") as image_f:
image_bytes = image_f.read()
# image = Image.open(image_f)
yield idx, {
"image_id": sample["image_id"],
"image": {"path": file_path, "bytes": image_bytes},
# "image": image,
"width": sample["width"],
"height": sample["height"],
"objects": sample["objects"],
}
idx += 1
if __name__ == '__main__':
pass