Update cvdataset-layoutlmv3.py

#11
by nvm472001 - opened
Files changed (1) hide show
  1. cvdataset-layoutlmv3.py +17 -14
cvdataset-layoutlmv3.py CHANGED
@@ -1,16 +1,16 @@
1
  import json
2
- import os
3
- from pathlib import Path
4
- from PIL import Image
5
  import pandas as pd
6
  import datasets
 
 
 
7
 
8
  logger = datasets.logging.get_logger(__name__)
9
 
10
  _CITATION = """\
11
  @article{LayoutLmv3 for CV extractions,
12
  title={LayoutLmv3for Key Information Extraction},
13
- author={MisaR&D Team},
14
  year={2022},
15
  }
16
  """
@@ -19,10 +19,12 @@ CV is a collection of receipts. It contains, for each photo about cv personal, a
19
  https://arxiv.org/abs/2103.14470
20
  """
21
 
 
22
  def load_image(image_path):
23
- image = Image.open(image_path)
24
  w, h = image.size
25
- return image, (w,h)
 
26
 
27
  def normalize_bbox(bbox, size):
28
  return [
@@ -32,6 +34,7 @@ def normalize_bbox(bbox, size):
32
  int(1000 * bbox[3] / size[1]),
33
  ]
34
 
 
35
  def _get_drive_url(url):
36
  base_url = 'https://drive.google.com/uc?id='
37
  split_url = url.split('/')
@@ -39,23 +42,24 @@ def _get_drive_url(url):
39
  return base_url + split_url[5]
40
 
41
  _URLS = [
 
42
  _get_drive_url("https://drive.google.com/file/d/1bqESdP3UhQ5H9ZEnn5NsH44FZmiQa0G_/"),
43
  _get_drive_url("https://drive.google.com/file/d/11SRDeRKUr8XacB7tauiGjkw1PXDGFKUx/"),
44
  _get_drive_url("https://drive.google.com/file/d/1KdDBmGP96lFc7jv2Bf4eqrO121ST-TCh/"),
45
  ]
46
 
47
- class DatasetConfig(datasets.BuilderConfig):
48
  """BuilderConfig for WildReceipt Dataset"""
49
  def __init__(self, **kwargs):
50
  """BuilderConfig for WildReceipt Dataset.
51
  Args:
52
  **kwargs: keyword arguments forwarded to super.
53
  """
54
- super(DatasetConfig, self).__init__(**kwargs)
55
 
56
  class CVDataset(datasets.GeneratorBasedBuilder):
57
  BUILDER_CONFIGS = [
58
- DatasetConfig(name="CV Extractions", version=datasets.Version("1.0.0"), description="CV dataset"),
59
  ]
60
 
61
  def _info(self):
@@ -88,23 +92,22 @@ class CVDataset(datasets.GeneratorBasedBuilder):
88
  """Returns SplitGenerators."""
89
  """Uses local files located with data_dir"""
90
  downloaded_file = dl_manager.download_and_extract(_URLS)
91
- dest = Path(downloaded_file[0])/'data1'
92
 
93
  return [
94
  datasets.SplitGenerator(
95
- name=datasets.Split.TRAIN, gen_kwargs={"filepath": dest/"update_train.txt", "dest": dest}
96
  ),
97
  datasets.SplitGenerator(
98
- name=datasets.Split.TEST, gen_kwargs={"filepath": dest/"update_test.txt", "dest": dest}
99
  ),
100
  ]
101
 
102
  def _generate_examples(self, filepath, dest):
103
 
104
- df = pd.read_csv(dest/'update_class_list.txt', delimiter='\s', header=None)
105
  id2labels = dict(zip(df[0].tolist(), df[1].tolist()))
106
 
107
-
108
  logger.info("⏳ Generating examples from = %s", filepath)
109
 
110
  item_list = []
 
1
  import json
 
 
 
2
  import pandas as pd
3
  import datasets
4
+ from pathlib import Path
5
+ from PIL import Image
6
+
7
 
8
  logger = datasets.logging.get_logger(__name__)
9
 
10
  _CITATION = """\
11
  @article{LayoutLmv3 for CV extractions,
12
  title={LayoutLmv3for Key Information Extraction},
13
+ author={Misa R&D Team},
14
  year={2022},
15
  }
16
  """
 
19
  https://arxiv.org/abs/2103.14470
20
  """
21
 
22
+
23
  def load_image(image_path):
24
+ image = Image.open(image_path).convert("RGB")
25
  w, h = image.size
26
+
27
+ return image, (w, h)
28
 
29
  def normalize_bbox(bbox, size):
30
  return [
 
34
  int(1000 * bbox[3] / size[1]),
35
  ]
36
 
37
+
38
  def _get_drive_url(url):
39
  base_url = 'https://drive.google.com/uc?id='
40
  split_url = url.split('/')
 
42
  return base_url + split_url[5]
43
 
44
  _URLS = [
45
+ _get_drive_url("https://drive.google.com/file/d/1fHDcNKdYFu33Z0kOQzVemLwjuuc3tdg-/"),
46
  _get_drive_url("https://drive.google.com/file/d/1bqESdP3UhQ5H9ZEnn5NsH44FZmiQa0G_/"),
47
  _get_drive_url("https://drive.google.com/file/d/11SRDeRKUr8XacB7tauiGjkw1PXDGFKUx/"),
48
  _get_drive_url("https://drive.google.com/file/d/1KdDBmGP96lFc7jv2Bf4eqrO121ST-TCh/"),
49
  ]
50
 
51
+ class CVENConfig(datasets.BuilderConfig):
52
  """BuilderConfig for WildReceipt Dataset"""
53
  def __init__(self, **kwargs):
54
  """BuilderConfig for WildReceipt Dataset.
55
  Args:
56
  **kwargs: keyword arguments forwarded to super.
57
  """
58
+ super(CVENConfig, self).__init__(**kwargs)
59
 
60
  class CVDataset(datasets.GeneratorBasedBuilder):
61
  BUILDER_CONFIGS = [
62
+ CVENConfig(name="CV Extractions", version=datasets.Version("1.0.0"), description="CV dataset"),
63
  ]
64
 
65
  def _info(self):
 
92
  """Returns SplitGenerators."""
93
  """Uses local files located with data_dir"""
94
  downloaded_file = dl_manager.download_and_extract(_URLS)
95
+ dest = Path(downloaded_file[0])/'data2'
96
 
97
  return [
98
  datasets.SplitGenerator(
99
+ name=datasets.Split.TRAIN, gen_kwargs={"filepath": dest/"train_en.txt", "dest": dest}
100
  ),
101
  datasets.SplitGenerator(
102
+ name=datasets.Split.TEST, gen_kwargs={"filepath": dest/"test_en.txt", "dest": dest}
103
  ),
104
  ]
105
 
106
  def _generate_examples(self, filepath, dest):
107
 
108
+ df = pd.read_csv(dest/'class_list.txt', delimiter='\s', header=None)
109
  id2labels = dict(zip(df[0].tolist(), df[1].tolist()))
110
 
 
111
  logger.info("⏳ Generating examples from = %s", filepath)
112
 
113
  item_list = []