nehulagrawal commited on
Commit
0464d11
1 Parent(s): 826265b

Upload table-detection-yolo.py

Browse files
Files changed (1) hide show
  1. table-detection-yolo.py +129 -0
table-detection-yolo.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import collections
2
+ import json
3
+ import os
4
+
5
+ import datasets
6
+
7
+ _HOMEPAGE = "https://www.foduu.ai/datasets/table-detection-yolo"
8
+ _CITATION = """
9
+ """
10
+ _ANNOTATION_FILENAME = "_annotations.coco.json"
11
+
12
+
13
+ class TABLEEXTRACTIONConfig(datasets.BuilderConfig):
14
+ """Builder Config for table-extraction"""
15
+
16
+ def __init__(self, data_urls, **kwargs):
17
+ """
18
+ BuilderConfig for table-extraction.
19
+
20
+ Args:
21
+ data_urls: `dict`, name to url to download the zip file from.
22
+ **kwargs: keyword arguments forwarded to super.
23
+ """
24
+ super(TABLEEXTRACTIONConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
25
+ self.data_urls = data_urls
26
+
27
+
28
+ class TABLEEXTRACTION(datasets.GeneratorBasedBuilder):
29
+ """table-extraction object detection dataset"""
30
+
31
+ VERSION = datasets.Version("1.0.0")
32
+ BUILDER_CONFIGS = [
33
+ TABLEEXTRACTIONConfig(
34
+ name="full",
35
+ description="Full version of table-detection-yolo dataset.",
36
+ data_urls={
37
+ "train": "https://huggingface.co/datasets/foduucom/table-detection-yolo/resolve/main/data/train.zip",
38
+ "validation": "https://huggingface.co/datasets/foduucom/table-detection-yolo/resolve/main/data/valid.zip",
39
+ "test": "https://huggingface.co/datasets/foduucom/table-detection-yolo/resolve/main/data/test.zip",
40
+ },
41
+ )
42
+ ]
43
+
44
+ def _info(self):
45
+ features = datasets.Features(
46
+ {
47
+ "image_id": datasets.Value("int64"),
48
+ "image": datasets.Image(),
49
+ "width": datasets.Value("int32"),
50
+ "height": datasets.Value("int32"),
51
+ "objects": datasets.Sequence(
52
+ {
53
+ "id": datasets.Value("int64"),
54
+ "area": datasets.Value("int64"),
55
+ "bbox": datasets.Sequence(datasets.Value("float32"), length=4),
56
+ "category": datasets.ClassLabel(),
57
+ }
58
+ ),
59
+ }
60
+ )
61
+ return datasets.DatasetInfo(
62
+ features=features,
63
+ homepage=_HOMEPAGE,
64
+ citation=_CITATION,
65
+ )
66
+
67
+ def _split_generators(self, dl_manager):
68
+ data_files = dl_manager.download_and_extract(self.config.data_urls)
69
+ return [
70
+ datasets.SplitGenerator(
71
+ name=datasets.Split.TRAIN,
72
+ gen_kwargs={
73
+ "folder_dir": data_files["train"],
74
+ },
75
+ ),
76
+ datasets.SplitGenerator(
77
+ name=datasets.Split.VALIDATION,
78
+ gen_kwargs={
79
+ "folder_dir": data_files["validation"],
80
+ },
81
+ ),
82
+ datasets.SplitGenerator(
83
+ name=datasets.Split.TEST,
84
+ gen_kwargs={
85
+ "folder_dir": data_files["test"],
86
+ },
87
+ ),
88
+ ]
89
+
90
+ def _generate_examples(self, folder_dir):
91
+ def process_annot(annot, category_id_to_category):
92
+ return {
93
+ "id": annot["id"],
94
+ "area": annot["area"],
95
+ "bbox": annot["bbox"],
96
+ "category": category_id_to_category[annot["category_id"]],
97
+ }
98
+
99
+ image_id_to_image = {}
100
+ idx = 0
101
+
102
+ annotation_filepath = os.path.join(folder_dir, _ANNOTATION_FILENAME)
103
+ with open(annotation_filepath, "r") as f:
104
+ annotations = json.load(f)
105
+ category_id_to_category = {category["id"]: category["name"] for category in annotations["categories"]}
106
+ image_id_to_annotations = collections.defaultdict(list)
107
+ for annot in annotations["annotations"]:
108
+ image_id_to_annotations[annot["image_id"]].append(annot)
109
+ filename_to_image = {image["file_name"]: image for image in annotations["images"]}
110
+
111
+ for filename in os.listdir(folder_dir):
112
+ filepath = os.path.join(folder_dir, filename)
113
+ if filename in filename_to_image:
114
+ image = filename_to_image[filename]
115
+ objects = [
116
+ process_annot(annot, category_id_to_category) for annot in image_id_to_annotations[image["id"]]
117
+ ]
118
+ with open(filepath, "rb") as f:
119
+ image_bytes = f.read()
120
+ yield idx, {
121
+ "image_id": image["id"],
122
+ "image": {"path": filepath, "bytes": image_bytes},
123
+ "width": image["width"],
124
+ "height": image["height"],
125
+ "objects": objects,
126
+ }
127
+ idx += 1
128
+
129
+