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| 1 |
+
from xml.etree import ElementTree as ET
|
| 2 |
+
|
| 3 |
+
import datasets
|
| 4 |
+
|
| 5 |
+
_CITATION = """\
|
| 6 |
+
@InProceedings{huggingface:dataset,
|
| 7 |
+
title = {ocr-barcodes-detection},
|
| 8 |
+
author = {TrainingDataPro},
|
| 9 |
+
year = {2023}
|
| 10 |
+
}
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
_DESCRIPTION = """\
|
| 14 |
+
The Grocery Store Receipts Dataset is a collection of photos captured from various
|
| 15 |
+
**grocery store receipts**. This dataset is specifically designed for tasks related to
|
| 16 |
+
**Optical Character Recognition (OCR)** and is useful for retail.
|
| 17 |
+
Each image in the dataset is accompanied by bounding box annotations, indicating the
|
| 18 |
+
precise locations of specific text segments on the receipts. The text segments are
|
| 19 |
+
categorized into four classes: **item, store, date_time and total**.
|
| 20 |
+
"""
|
| 21 |
+
_NAME = "ocr-barcodes-detection"
|
| 22 |
+
|
| 23 |
+
_HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}"
|
| 24 |
+
|
| 25 |
+
_LICENSE = ""
|
| 26 |
+
|
| 27 |
+
_DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/"
|
| 28 |
+
|
| 29 |
+
_LABELS = ["Barcode"]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class OcrBarcodesDetection(datasets.GeneratorBasedBuilder):
|
| 33 |
+
def _info(self):
|
| 34 |
+
return datasets.DatasetInfo(
|
| 35 |
+
description=_DESCRIPTION,
|
| 36 |
+
features=datasets.Features(
|
| 37 |
+
{
|
| 38 |
+
"id": datasets.Value("int32"),
|
| 39 |
+
"name": datasets.Value("string"),
|
| 40 |
+
"image": datasets.Image(),
|
| 41 |
+
"mask": datasets.Image(),
|
| 42 |
+
"width": datasets.Value("uint16"),
|
| 43 |
+
"height": datasets.Value("uint16"),
|
| 44 |
+
"shapes": datasets.Sequence(
|
| 45 |
+
{
|
| 46 |
+
"label": datasets.ClassLabel(
|
| 47 |
+
num_classes=len(_LABELS),
|
| 48 |
+
names=_LABELS,
|
| 49 |
+
),
|
| 50 |
+
"type": datasets.Value("string"),
|
| 51 |
+
"points": datasets.Sequence(
|
| 52 |
+
datasets.Sequence(
|
| 53 |
+
datasets.Value("float"),
|
| 54 |
+
),
|
| 55 |
+
),
|
| 56 |
+
"rotation": datasets.Value("float"),
|
| 57 |
+
"occluded": datasets.Value("uint8"),
|
| 58 |
+
"attributes": datasets.Sequence(
|
| 59 |
+
{
|
| 60 |
+
"name": datasets.Value("string"),
|
| 61 |
+
"text": datasets.Value("string"),
|
| 62 |
+
}
|
| 63 |
+
),
|
| 64 |
+
}
|
| 65 |
+
),
|
| 66 |
+
}
|
| 67 |
+
),
|
| 68 |
+
supervised_keys=None,
|
| 69 |
+
homepage=_HOMEPAGE,
|
| 70 |
+
citation=_CITATION,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
def _split_generators(self, dl_manager):
|
| 74 |
+
images = dl_manager.download(f"{_DATA}images.tar.gz")
|
| 75 |
+
masks = dl_manager.download(f"{_DATA}boxes.tar.gz")
|
| 76 |
+
annotations = dl_manager.download(f"{_DATA}annotations.xml")
|
| 77 |
+
images = dl_manager.iter_archive(images)
|
| 78 |
+
masks = dl_manager.iter_archive(masks)
|
| 79 |
+
return [
|
| 80 |
+
datasets.SplitGenerator(
|
| 81 |
+
name=datasets.Split.TRAIN,
|
| 82 |
+
gen_kwargs={
|
| 83 |
+
"images": images,
|
| 84 |
+
"masks": masks,
|
| 85 |
+
"annotations": annotations,
|
| 86 |
+
},
|
| 87 |
+
),
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
@staticmethod
|
| 91 |
+
def parse_shape(shape: ET.Element) -> dict:
|
| 92 |
+
label = shape.get("label")
|
| 93 |
+
shape_type = shape.tag
|
| 94 |
+
rotation = shape.get("rotation", 0.0)
|
| 95 |
+
occluded = shape.get("occluded", 0)
|
| 96 |
+
|
| 97 |
+
points = None
|
| 98 |
+
|
| 99 |
+
if shape_type == "points":
|
| 100 |
+
points = tuple(map(float, shape.get("points").split(",")))
|
| 101 |
+
|
| 102 |
+
elif shape_type == "box":
|
| 103 |
+
points = [
|
| 104 |
+
(float(shape.get("xtl")), float(shape.get("ytl"))),
|
| 105 |
+
(float(shape.get("xbr")), float(shape.get("ybr"))),
|
| 106 |
+
]
|
| 107 |
+
|
| 108 |
+
elif shape_type == "polygon":
|
| 109 |
+
points = [
|
| 110 |
+
tuple(map(float, point.split(",")))
|
| 111 |
+
for point in shape.get("points").split(";")
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
attributes = []
|
| 115 |
+
|
| 116 |
+
for attr in shape:
|
| 117 |
+
attr_name = attr.get("name")
|
| 118 |
+
attr_text = attr.text
|
| 119 |
+
attributes.append({"name": attr_name, "text": attr_text})
|
| 120 |
+
|
| 121 |
+
shape_data = {
|
| 122 |
+
"label": label,
|
| 123 |
+
"type": shape_type,
|
| 124 |
+
"points": points,
|
| 125 |
+
"rotation": rotation,
|
| 126 |
+
"occluded": occluded,
|
| 127 |
+
"attributes": attributes,
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
return shape_data
|
| 131 |
+
|
| 132 |
+
def _generate_examples(self, images, masks, annotations):
|
| 133 |
+
tree = ET.parse(annotations)
|
| 134 |
+
root = tree.getroot()
|
| 135 |
+
|
| 136 |
+
for idx, (
|
| 137 |
+
(image_path, image),
|
| 138 |
+
(mask_path, mask),
|
| 139 |
+
) in enumerate(zip(images, masks)):
|
| 140 |
+
image_name = image_path.split("/")[-1]
|
| 141 |
+
img = root.find(f"./image[@name='images/{image_name}']")
|
| 142 |
+
|
| 143 |
+
image_id = img.get("id")
|
| 144 |
+
name = img.get("name")
|
| 145 |
+
width = img.get("width")
|
| 146 |
+
height = img.get("height")
|
| 147 |
+
shapes = [self.parse_shape(shape) for shape in img]
|
| 148 |
+
|
| 149 |
+
yield idx, {
|
| 150 |
+
"id": image_id,
|
| 151 |
+
"name": name,
|
| 152 |
+
"image": {"path": image_path, "bytes": image.read()},
|
| 153 |
+
"mask": {"path": mask_path, "bytes": mask.read()},
|
| 154 |
+
"width": width,
|
| 155 |
+
"height": height,
|
| 156 |
+
"shapes": shapes,
|
| 157 |
+
}
|