Commit
·
15a4742
1
Parent(s):
f325eb5
add loading file
Browse files- gan_hkr.py +61 -0
gan_hkr.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import zipfile
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
from datasets import load_dataset
|
7 |
+
|
8 |
+
|
9 |
+
class GanHKR(datasets.GeneratorBasedBuilder):
|
10 |
+
|
11 |
+
def _info(self):
|
12 |
+
return datasets.DatasetInfo(
|
13 |
+
features=datasets.Features(
|
14 |
+
{
|
15 |
+
"image": datasets.Image(),
|
16 |
+
"path": datasets.Value("string"),
|
17 |
+
"name": datasets.Value("string"),
|
18 |
+
"text": datasets.Value("string")
|
19 |
+
}
|
20 |
+
)
|
21 |
+
)
|
22 |
+
|
23 |
+
def _split_generators(self, dl_manager):
|
24 |
+
_URLS = {f"img{i:02d}": f"img/img-{i:02d}.zip" for i in range(10)}
|
25 |
+
_URLS["labels"] = "gt.txt"
|
26 |
+
|
27 |
+
data_paths = dl_manager.download(_URLS)
|
28 |
+
archive_paths = [data_paths[key] for key in data_paths if key.startswith("img")]
|
29 |
+
img_path = os.path.join(os.path.split(data_paths["labels"])[0], "img")
|
30 |
+
os.makedirs(img_path, exist_ok=True)
|
31 |
+
|
32 |
+
for archive_path in archive_paths:
|
33 |
+
with zipfile.ZipFile(archive_path) as archive:
|
34 |
+
archive.extractall(img_path)
|
35 |
+
os.remove(archive_path)
|
36 |
+
|
37 |
+
image_paths = [os.path.join(img_path, img_name) for img_name in os.listdir(img_path)]
|
38 |
+
return [datasets.SplitGenerator(
|
39 |
+
name=datasets.Split.TRAIN,
|
40 |
+
gen_kwargs={
|
41 |
+
"image_paths": image_paths,
|
42 |
+
"labels_path": data_paths["labels"]
|
43 |
+
})]
|
44 |
+
|
45 |
+
def _generate_examples(self, image_paths, labels_path):
|
46 |
+
df = pd.read_csv(labels_path, sep=",", names=["path", "text"])
|
47 |
+
df["path"] = df.path.str[4:]
|
48 |
+
df.set_index("path", inplace=True)
|
49 |
+
|
50 |
+
for image_path in image_paths:
|
51 |
+
image_name = os.path.basename(image_path)
|
52 |
+
|
53 |
+
if image_name in df.index:
|
54 |
+
|
55 |
+
example = {
|
56 |
+
"image": image_path,
|
57 |
+
"path": image_path,
|
58 |
+
"name": image_name,
|
59 |
+
"text": df["text"][image_name]
|
60 |
+
}
|
61 |
+
yield image_name, example
|