Repository navigation
Expand file tree
/
Copy pathdata.py
More file actions
64 lines (50 loc) · 1.78 KB
/
Copy pathdata.py
File metadata and controls
64 lines (50 loc) · 1.78 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import shutil
import zipfile
from glob import glob
from PIL import Image
from urllib import request
import matplotlib.pyplot as plt
IMAGE_URL = "https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_Dataset.zip"
TEXT_URL = "https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_text.zip"
files = ['Flickr8k_Dataset.zip', 'Flickr8k_text.zip']
def download_and_extract(url, fname, root):
"""Downloads the zip file from given url and extracts
it in given folder
Parameters
----------
url : str
url of zip file
fname: str
folder name where the zip file will be extracted
Returns
-------
None
"""
path = os.path.join(root, fname)
if os.path.exists(path): # if file already exsits than dont extract it again
print(f'File already exists at path {path}.')
return
print(f'Downloading file from {url} ...')
request.urlretrieve(url, path)
with zipfile.ZipFile(path, 'r') as f:
print(f'extracting file at path {root} ...')
f.extractall(root)
def download_data(root):
download_and_extract(IMAGE_URL, files[0], root)
download_and_extract(TEXT_URL, files[1], root)
path = os.path.join(root, 'Flicker8k_text')
os.makedirs(path, exist_ok=True)
for file in glob(os.path.join(root, '*.txt')):
fname = file.rsplit('/')[-1]
shutil.move(file, os.path.join(path, fname))
image_files = glob('./Flicker8k_Dataset/*.jpg')
# TODO: move this funtion to differt module
def show(shape=(3, 4), figsize=(10, 10)):
fig, axs = plt.subplots(*shape, figsize=figsize)
for i, ax in enumerate(axs.flatten()):
ax.imshow(Image.open(image_files[i]))
ax.set_xticks([])
ax.set_yticks([])
plt.tight_layout()
plt.show()