nastyboget commited on
Commit
15ead30
·
1 Parent(s): 6726991

loading file

Browse files
Files changed (1) hide show
  1. synthetic_hkr.py +50 -0
synthetic_hkr.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 SyntheticHKR(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 = {"img": "img.zip", "labels": "gt.txt"}
25
+ data_paths = dl_manager.download_and_extract(_URLS)
26
+
27
+ return [datasets.SplitGenerator(
28
+ name=datasets.Split.TRAIN,
29
+ gen_kwargs={
30
+ "image_paths": dl_manager.iter_files(data_paths["img"]),
31
+ "labels_path": data_paths["labels"]
32
+ })]
33
+
34
+ def _generate_examples(self, image_paths, labels_path):
35
+ df = pd.read_csv(labels_path, sep=",", names=["path", "text"])
36
+ df["path"] = df.path.str[4:]
37
+ df.set_index("path", inplace=True)
38
+
39
+ for image_path in image_paths:
40
+ image_name = os.path.basename(image_path)
41
+
42
+ if image_name in df.index:
43
+
44
+ example = {
45
+ "image": image_path,
46
+ "path": image_path,
47
+ "name": image_name,
48
+ "text": df["text"][image_name]
49
+ }
50
+ yield image_name, example