unit test p1 based on imagefolder
Browse files
README.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1 |
---
|
2 |
license: cc-by-nc-4.0
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: cc-by-nc-4.0
|
3 |
---
|
4 |
+
|
5 |
+
## Required installation
|
6 |
+
|
7 |
+
```bash
|
8 |
+
pip3 install pypdf2 pdf2image
|
9 |
+
sudo apt-get install poppler-utils
|
10 |
+
```
|
data/train/categoryA/90022-confcommittee-budget-051211.pdf
ADDED
The diff for this file is too large to render.
See raw diff
|
|
data/train/categoryB/581261-brown-invoice-10-29-11-04-12-2-13530037073412-pdf.pdf
ADDED
Binary file (367 kB). View file
|
|
loader.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import List
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
import pdf2image
|
6 |
+
|
7 |
+
logger = datasets.logging.get_logger(__name__)
|
8 |
+
|
9 |
+
_DESCRIPTION = "A generic pdf folder"
|
10 |
+
|
11 |
+
|
12 |
+
class PdfFolder(datasets.GeneratorBasedBuilder):
|
13 |
+
def _info(self):
|
14 |
+
|
15 |
+
folder=None
|
16 |
+
if isinstance(self.config.data_files, str):
|
17 |
+
folder = self.config.data_files
|
18 |
+
elif isinstance(self.config.data_files, dict):
|
19 |
+
folder = self.config.data_files.get('train', None)
|
20 |
+
|
21 |
+
if folder is None:
|
22 |
+
raise RuntimeError()
|
23 |
+
|
24 |
+
classes = sorted([x.name.lower() for x in Path(folder).glob('*/**')]).unique()
|
25 |
+
|
26 |
+
return datasets.DatasetInfo(
|
27 |
+
description=_DESCRIPTION,
|
28 |
+
features=datasets.Features(
|
29 |
+
{
|
30 |
+
"file": datasets.Sequence(datasets.Image()),
|
31 |
+
"labels": datasets.features.ClassLabel(names=classes)
|
32 |
+
}
|
33 |
+
),
|
34 |
+
task_templates=None,
|
35 |
+
)
|
36 |
+
|
37 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
38 |
+
|
39 |
+
data_files = self.config.data_files
|
40 |
+
|
41 |
+
if isinstance(data_files, str):
|
42 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'archive_path': data_files})]
|
43 |
+
|
44 |
+
splits = []
|
45 |
+
for split_name, folder in data_files.items():
|
46 |
+
splits.append(datasets.SplitGenerator(name=split_name, gen_kwargs={'archive_path': folder}))
|
47 |
+
|
48 |
+
return splits
|
49 |
+
|
50 |
+
def _generate_examples(self, archive_path):
|
51 |
+
labels = self.info.features['labels']
|
52 |
+
logger.info("generating examples from = %s", archive_path)
|
53 |
+
extensions = {'.pdf'}
|
54 |
+
for i, path in enumerate(Path(archive_path).glob('**/*')):
|
55 |
+
if path.suffix in extensions:
|
56 |
+
|
57 |
+
images = pdf2image.convert_from_bytes(path.posix())
|
58 |
+
|
59 |
+
#convert PDF to list of images
|
60 |
+
yield i, {'file': images, 'labels': labels.encode_example(path.parent.name.lower())}
|