Commit
·
d3e509e
1
Parent(s):
55128e0
Upload cord.py
Browse files
cord.py
CHANGED
@@ -5,8 +5,7 @@ import os
|
|
5 |
|
6 |
import datasets
|
7 |
import gdown
|
8 |
-
|
9 |
-
import numpy as np
|
10 |
logger = datasets.logging.get_logger(__name__)
|
11 |
|
12 |
|
@@ -24,21 +23,6 @@ https://github.com/clovaai/cord
|
|
24 |
"""
|
25 |
_URL = "https://drive.google.com/uc?id=1MqhTbcj-AHXOqYoeoh12aRUwIprzTJYI"
|
26 |
|
27 |
-
def load_image(image_path):
|
28 |
-
image = Image.open(image_path)
|
29 |
-
image = image.convert("RGB")
|
30 |
-
w, h = image.size
|
31 |
-
return image, (w, h)
|
32 |
-
|
33 |
-
|
34 |
-
def normalize_bbox(bbox, size):
|
35 |
-
return [
|
36 |
-
int(1000 * bbox[0] / size[0]),
|
37 |
-
int(1000 * bbox[1] / size[1]),
|
38 |
-
int(1000 * bbox[2] / size[0]),
|
39 |
-
int(1000 * bbox[3] / size[1]),
|
40 |
-
]
|
41 |
-
|
42 |
|
43 |
def gdrive_downloader(url, path):
|
44 |
gdown.download(url, path, quiet=False)
|
@@ -71,6 +55,7 @@ class Cord(datasets.GeneratorBasedBuilder):
|
|
71 |
"id": datasets.Value("string"),
|
72 |
"tokens": datasets.Sequence(datasets.Value("string")),
|
73 |
"bboxes": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
|
|
|
74 |
"ner_tags": datasets.Sequence(
|
75 |
datasets.features.ClassLabel(
|
76 |
names=['menu.cnt',
|
@@ -163,8 +148,16 @@ class Cord(datasets.GeneratorBasedBuilder):
|
|
163 |
image_path = image_path.replace(
|
164 |
filepath, filepaths[other_dir_idx])
|
165 |
|
166 |
-
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
for item in data["valid_line"]:
|
170 |
for word in item['words']:
|
@@ -172,30 +165,20 @@ class Cord(datasets.GeneratorBasedBuilder):
|
|
172 |
txt = word['text']
|
173 |
|
174 |
# get bounding box
|
175 |
-
# important: each bounding box should be in (upper left, lower right) format
|
176 |
-
# it took me some time to understand the upper left is (x1, y3)
|
177 |
-
# and the lower right is (x3, y1)
|
178 |
x1 = word['quad']['x1']
|
179 |
y1 = word['quad']['y1']
|
180 |
x3 = word['quad']['x3']
|
181 |
y3 = word['quad']['y3']
|
182 |
|
183 |
box = [x1, y1, x3, y3]
|
184 |
-
box = normalize_bbox(box, size)
|
185 |
|
186 |
# ADDED
|
187 |
# skip empty word
|
188 |
if len(txt) < 1:
|
189 |
continue
|
190 |
-
if min(box) < 0 or max(box) > 1000: # another bug in which a box had -4
|
191 |
-
continue
|
192 |
-
# another bug in which a box difference was -12
|
193 |
-
if ((box[3] - box[1]) < 0) or ((box[2] - box[0]) < 0):
|
194 |
-
continue
|
195 |
-
# ADDED
|
196 |
|
197 |
tokens.append(txt)
|
198 |
bboxes.append(box)
|
199 |
ner_tags.append(item['category'])
|
200 |
|
201 |
-
yield guid, {"id": str(guid), "tokens": tokens, "bboxes": bboxes, "ner_tags": ner_tags, "image_path": image_path}
|
|
|
5 |
|
6 |
import datasets
|
7 |
import gdown
|
8 |
+
|
|
|
9 |
logger = datasets.logging.get_logger(__name__)
|
10 |
|
11 |
|
|
|
23 |
"""
|
24 |
_URL = "https://drive.google.com/uc?id=1MqhTbcj-AHXOqYoeoh12aRUwIprzTJYI"
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
def gdrive_downloader(url, path):
|
28 |
gdown.download(url, path, quiet=False)
|
|
|
55 |
"id": datasets.Value("string"),
|
56 |
"tokens": datasets.Sequence(datasets.Value("string")),
|
57 |
"bboxes": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
|
58 |
+
"roi": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
|
59 |
"ner_tags": datasets.Sequence(
|
60 |
datasets.features.ClassLabel(
|
61 |
names=['menu.cnt',
|
|
|
148 |
image_path = image_path.replace(
|
149 |
filepath, filepaths[other_dir_idx])
|
150 |
|
151 |
+
roi = data["roi"]
|
152 |
+
if roi:
|
153 |
+
top_left = [roi["x1"], roi["y1"]]
|
154 |
+
bottom_right = [roi["x3"], roi["y3"]]
|
155 |
+
bottom_left = [roi["x4"], roi["y4"]]
|
156 |
+
top_right = [roi["x2"], roi["y2"]]
|
157 |
+
roi = [top_left, top_right, bottom_right, bottom_left]
|
158 |
+
else:
|
159 |
+
roi = []
|
160 |
+
|
161 |
|
162 |
for item in data["valid_line"]:
|
163 |
for word in item['words']:
|
|
|
165 |
txt = word['text']
|
166 |
|
167 |
# get bounding box
|
|
|
|
|
|
|
168 |
x1 = word['quad']['x1']
|
169 |
y1 = word['quad']['y1']
|
170 |
x3 = word['quad']['x3']
|
171 |
y3 = word['quad']['y3']
|
172 |
|
173 |
box = [x1, y1, x3, y3]
|
|
|
174 |
|
175 |
# ADDED
|
176 |
# skip empty word
|
177 |
if len(txt) < 1:
|
178 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
tokens.append(txt)
|
181 |
bboxes.append(box)
|
182 |
ner_tags.append(item['category'])
|
183 |
|
184 |
+
yield guid, {"id": str(guid), "tokens": tokens, "bboxes": bboxes, "ner_tags": ner_tags, "image_path": image_path, "roi":roi}
|