-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (33 loc) · 1.14 KB
/
utils.py
File metadata and controls
35 lines (33 loc) · 1.14 KB
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
import re
import os
import pandas as pd
import multiprocessing
from time import time as timer
from tqdm import tqdm
import numpy as np
from pathlib import Path
from functools import partial
import requests
import urllib
def download_image(image_link, savefolder):
if(isinstance(image_link, str)):
filename = Path(image_link).name
image_save_path = os.path.join(savefolder, filename)
if(not os.path.exists(image_save_path)):
try:
urllib.request.urlretrieve(image_link, image_save_path)
except Exception as ex:
print('Warning: Not able to download - {}\n{}'.format(image_link, ex))
else:
return
return
def download_images(image_links, download_folder):
if not os.path.exists(download_folder):
os.makedirs(download_folder)
results = []
download_image_partial = partial(download_image, savefolder=download_folder)
with multiprocessing.Pool(100) as pool:
for result in tqdm(pool.imap(download_image_partial, image_links), total=len(image_links)):
results.append(result)
pool.close()
pool.join()