File size: 1,667 Bytes
eeaf2b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
from pathlib import Path

import datasets

_DESCRIPTION = """\
Captcha images dataset.
"""
_LICENSE = "wtfpl"
_DATA_DIR = "data"

class Captcha(datasets.GeneratorBasedBuilder):
    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                        "image": datasets.Image(),
                        "solution": datasets.Value("string"),
                }
            ),
            license=_LICENSE,
        )

    def _split_generators(self, dl_manager):
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "images": dl_manager.iter_archive(dl_manager.download(os.path.join(_DATA_DIR, "train.tar.gz"))),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "images": dl_manager.iter_archive(dl_manager.download(os.path.join(_DATA_DIR, "validation.tar.gz"))),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "images": dl_manager.iter_archive(dl_manager.download(os.path.join(_DATA_DIR, "test.tar.gz"))),
                },
            ),
        ]

    def _generate_examples(self, images):
        for file_path, file_obj in images:
            yield file_path, {
                    "image": {"path": file_path, "bytes": file_obj.read()},
                    "solution": Path(file_path).name.split('.')[0],
                }