forked from Azure/ImageSimilarityUsingCntk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0_downloadImages.py
36 lines (31 loc) · 1.14 KB
/
0_downloadImages.py
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
# -*- coding: utf-8 -*-
from helpers import *
locals().update(importlib.import_module("PARAMETERS").__dict__)
####################################
# Main
####################################
makeDirectory(rootDir + "/data/")
makeDirectory(imgDir)
counter = 0
imgUrls = readTable(imgUrlsPath)
imgUrls = randomizeList(imgUrls)
for index, (label, url) in enumerate(imgUrls):
# Skip image if was already downloaded
outImgPath = os.path.join(imgDir, label, str(index) + ".jpg")
if os.path.exists(outImgPath):
continue
# Download image
print("Downloading image {} of {}: label={}, url={}".format(index, len(imgUrls), label, url))
data = downloadFromUrl(url, False)
if len(data) > 0:
makeDirectory(os.path.join(imgDir, label))
writeBinaryFile(outImgPath, data)
# Sanity check: delete image if it is corrupted
try:
imread(outImgPath)
counter += 1
except:
print("Removing corrupted image {}, url={}".format(outImgPath, url))
os.remove(outImgPath)
print("Successfully downloaded {} of the {} image urls.".format(counter, len(imgUrls)))
print("DONE.")