Download dates.py from dimun/dates: direct link, hf CLI and curl.
- Browser
- Download file 4.82 kB
-
https://huggingface.co/datasets/dimun/dates/resolve/main/dates.py
- Command line
-
hf download hf://datasets/dimun/dates/dates.py
-
curl -L -o dates.py https://huggingface.co/datasets/dimun/dates/resolve/main/dates.py
4.82 kB
| import json | |
| import os | |
| import datasets | |
| # Find for instance the citation on arxiv or on the dataset repo/website | |
| _CITATION = """\ | |
| @article{seker2022generalized, title={A generalized framework for recognition of expiration dates on product packages using fully convolutional networks}, author={Seker, Ahmet Cagatay and Ahn, Sang Chul}, journal={Expert Systems with Applications}, pages={117310}, year={2022}, publisher={Elsevier} } | |
| """ | |
| _DESCRIPTION = """\ | |
| The dataset for Date recognition in the proposed framework aims to provide annotated images that are relevant for training and evaluating models tasked with detecting dates within product labels or similar contexts. | |
| """ | |
| _HOMEPAGE = "https://acseker.github.io/ExpDateWebsite/" | |
| _LICENSE = "https://www.apache.org/licenses/LICENSE-2.0.html" | |
| _URLs = { | |
| "dates_synth": "https://huggingface.co/datasets/dimun/dates/resolve/main/Date-Synth.zip?download=true", | |
| "dates_real": "https://huggingface.co/datasets/dimun/dates/resolve/main/Date-Real.zip?download=true", | |
| } | |
| logger = datasets.logging.get_logger(__name__) | |
| def has_extension(file_path, extensions): | |
| _, file_extension = os.path.splitext(file_path) | |
| return file_extension.lower() in extensions | |
| class ExpirationDate(datasets.GeneratorBasedBuilder): | |
| VERSION = datasets.Version("0.0.1") | |
| CATEGORIES = ["day", "month", "year"] | |
| def _info(self): | |
| features = datasets.Features( | |
| { | |
| "id": datasets.Value("string"), | |
| "transcriptions": datasets.Sequence(datasets.Value("string")), | |
| "bboxes_block": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))), | |
| "categories": datasets.Sequence(datasets.features.ClassLabel(names=self.CATEGORIES)), | |
| "image_path": datasets.Value("string"), | |
| "width": datasets.Value("int32"), | |
| "height": datasets.Value("int32") | |
| } | |
| ) | |
| return datasets.DatasetInfo( | |
| # This is the description that will appear on the datasets page. | |
| description=_DESCRIPTION, | |
| # Features/targets of the dataset | |
| features=features, | |
| # Homepage of the dataset for documentation | |
| homepage=_HOMEPAGE, | |
| # License for the dataset if available | |
| license=_LICENSE, | |
| # Citation for the dataset | |
| citation=_CITATION, | |
| ) | |
| def _split_generators(self, dl_manager): | |
| """Returns SplitGenerators.""" | |
| # dl_manager is a datasets.download.DownloadManager that can be used to download and extract files | |
| # based on the provided URLs | |
| archive_path = dl_manager.download_and_extract(_URLs) | |
| return [ | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TRAIN, | |
| # These kwargs will be passed to _generate_examples | |
| gen_kwargs={ | |
| "filepath": os.path.join(archive_path["dates_synth"], "Date-Synth/"), | |
| "split": "train" | |
| }, | |
| ), | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TEST, | |
| # These kwargs will be passed to _generate_examples | |
| gen_kwargs={ | |
| "filepath": os.path.join(archive_path["dates_real"], "Date-Real/"), | |
| "split": "test" | |
| }, | |
| ) | |
| ] | |
| def _generate_examples(self, filepath, split): | |
| logger.info( | |
| f"⏳ Generating examples from = {filepath} to the split {split}") | |
| ann_file = os.path.join(filepath, "annotations.json") | |
| # get json | |
| with open(ann_file, "r", encoding="utf8") as f: | |
| features_map = json.load(f) | |
| img_dir = os.path.join(filepath, "images") | |
| img_listdir = os.listdir(img_dir) | |
| for guid, filename in enumerate(img_listdir): | |
| if filename.endswith(".jpg"): | |
| image_features = features_map[filename] | |
| image_ann = image_features.get("ann") | |
| transcriptions = [box.get("transcription", "") for box in image_ann] | |
| bboxes_block = [box.get("bbox") for box in image_ann] | |
| categories = [box.get("cls") if box.get( | |
| "cls") in self.CATEGORIES else "invalid" for box in image_ann] | |
| # get image | |
| image_path = os.path.join(img_dir, filename) | |
| yield guid, { | |
| "id": filename, | |
| "transcriptions": transcriptions, | |
| "bboxes_block": bboxes_block, | |
| "categories": categories, | |
| "image_path": image_path, | |
| "width": image_features.get("width"), | |
| "height": image_features.get("height"), | |
| } | |