umarigan commited on
Commit
a06ce17
·
verified ·
1 Parent(s): 76704fd

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +36 -1
README.md CHANGED
@@ -60,4 +60,39 @@ The metadata is made available through a series of parquet files with the follow
60
  - `mime_type`: The MIME type of the image file.
61
  - `hash`: The MD5 hash of the image file.
62
  - `license`: The URL of the image license.
63
- - `source` : The source organization of the image.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  - `mime_type`: The MIME type of the image file.
61
  - `hash`: The MD5 hash of the image file.
62
  - `license`: The URL of the image license.
63
+ - `source` : The source organization of the image.
64
+
65
+ ## Download Images:
66
+ ```python
67
+ from concurrent.futures import ThreadPoolExecutor
68
+ from functools import partial
69
+ import io
70
+ import urllib
71
+ import PIL.Image
72
+ from datasets import load_dataset
73
+ from datasets.utils.file_utils import get_datasets_user_agent
74
+ USER_AGENT = get_datasets_user_agent()
75
+ def fetch_single_image(image_url, timeout=None, retries=0):
76
+ for _ in range(retries + 1):
77
+ try:
78
+ request = urllib.request.Request(
79
+ image_url,
80
+ data=None,
81
+ headers={"user-agent": USER_AGENT},
82
+ )
83
+ with urllib.request.urlopen(request, timeout=timeout) as req:
84
+ image = PIL.Image.open(io.BytesIO(req.read()))
85
+ break
86
+ except Exception:
87
+ image = None
88
+ return image
89
+ def fetch_images(batch, num_threads, timeout=None, retries=0):
90
+ fetch_single_image_with_args = partial(fetch_single_image, timeout=timeout, retries=retries)
91
+ with ThreadPoolExecutor(max_workers=num_threads) as executor:
92
+ batch["image"] = list(executor.map(fetch_single_image_with_args, batch["url"]))
93
+ return batch
94
+
95
+
96
+ num_threads = 20
97
+ dataset = dataset.map(fetch_images, batched=True, batch_size=100, fn_kwargs={"num_threads": num_threads})
98
+ ```