davanstrien HF staff commited on
Commit
50a59a9
1 Parent(s): c39e419

tidy script

Browse files
Files changed (1) hide show
  1. early_printed_books_font_detection.py +19 -13
early_printed_books_font_detection.py CHANGED
@@ -13,13 +13,17 @@
13
  # limitations under the License.
14
  """Dataset of illustrated and non illustrated 19th Century newspaper ads."""
15
 
16
- import os
17
  from pathlib import Path
18
 
19
  import datasets
20
  import requests
21
  from PIL import Image
22
 
 
 
 
 
23
  _CITATION = """\
24
  @dataset{seuret_mathias_2019_3366686,
25
  author = {Seuret, Mathias and
@@ -95,18 +99,17 @@ class EarlyBookFonts(datasets.GeneratorBasedBuilder):
95
  if file["type"] == "zip"
96
  )
97
  *image_urls, label_url = urls
98
- config = datasets.DownloadConfig()
99
- labels = dl_manager.download_and_extract(label_url)
100
  images = dl_manager.download_and_extract(image_urls)
101
 
102
  return [
103
  datasets.SplitGenerator(
104
  name=datasets.Split.TRAIN,
105
- gen_kwargs={"images": images, "labels": os.path.join(labels), "split": "training"},
106
  ),
107
  datasets.SplitGenerator(
108
  name=datasets.Split.TEST,
109
- gen_kwargs={"images": images, "labels": os.path.join(labels), "split": "test"},
110
  ),
111
  ]
112
 
@@ -115,11 +118,14 @@ class EarlyBookFonts(datasets.GeneratorBasedBuilder):
115
  for directory in images:
116
  for file in Path(directory).rglob("*"):
117
  mapping["/".join(file.parts[-2:])] = file
118
- with open(f"labels-{split}.csv", 'r') as label_csv:
119
- for id_, row in enumerate(label_csv.readlines()):
120
- filename, *labels = row.split(",")
121
- labels = [label.strip("\n") for label in labels]
122
- labels = [label for label in labels if label != '-']
123
- filename = mapping[filename]
124
- image = Image.open(filename)
125
- yield id_, {"image": image, "labels": labels}
 
 
 
13
  # limitations under the License.
14
  """Dataset of illustrated and non illustrated 19th Century newspaper ads."""
15
 
16
+ import zipfile
17
  from pathlib import Path
18
 
19
  import datasets
20
  import requests
21
  from PIL import Image
22
 
23
+ # Some of the images are large so we set this to avoid Decompression bomb warnings
24
+
25
+ Image.MAX_IMAGE_PIXELS = None
26
+
27
  _CITATION = """\
28
  @dataset{seuret_mathias_2019_3366686,
29
  author = {Seuret, Mathias and
99
  if file["type"] == "zip"
100
  )
101
  *image_urls, label_url = urls
102
+ labels = dl_manager.download(label_url)
 
103
  images = dl_manager.download_and_extract(image_urls)
104
 
105
  return [
106
  datasets.SplitGenerator(
107
  name=datasets.Split.TRAIN,
108
+ gen_kwargs={"images": images, "labels": labels, "split": "training"},
109
  ),
110
  datasets.SplitGenerator(
111
  name=datasets.Split.TEST,
112
+ gen_kwargs={"images": images, "labels": labels, "split": "test"},
113
  ),
114
  ]
115
 
118
  for directory in images:
119
  for file in Path(directory).rglob("*"):
120
  mapping["/".join(file.parts[-2:])] = file
121
+ with zipfile.ZipFile(labels) as labelzip:
122
+ with labelzip.open(f"labels-{split}.csv") as label_csv:
123
+ for id_, row in enumerate(label_csv.readlines()):
124
+ row = row.decode("utf-8")
125
+ filename, *labels = row.split(",")
126
+ labels = [label.strip("\n") for label in labels]
127
+ labels = [label for label in labels if label != "-"]
128
+ filename = mapping[filename]
129
+
130
+ image = Image.open(filename)
131
+ yield id_, {"image": image, "labels": labels}