forked from worldbank/ml4dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_get_negatives.py
58 lines (48 loc) · 1.68 KB
/
06_get_negatives.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'''
Let's download a few negative images to train the algo.
Usage:
python 06_get_negatives.py [-h] [--count COUNT]
optional arguments:
-h, --help show this help message and exit
--count COUNT The total number of negative images to download.
'''
from random import shuffle
from utils.geo import ELEMENTS_FILENAME
from utils.mapbox_static import MapboxStatic
import argparse
import json
import random
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('--count',
type=int, default=5,
help='The total number of negative images to download.')
args = vars(parser.parse_args())
count = args.get('count')
print 'We are gonna download %d negative images' % count
# We need the elements
print 'Loading %s...' % ELEMENTS_FILENAME
with open(ELEMENTS_FILENAME, 'r') as f:
elements = json.load(f)
# Randomize elements list to make sure we don't download all pics from the
# same sample. Then cut it.
shuffle(elements)
# Now we're gonna download the satellite images for these locations
mapbox_static = MapboxStatic(
namespace='negative',
root_folder='satellite/negative')
total_downloaded = 0
for element in elements:
if total_downloaded >= count:
break
if element.get('type') != 'node':
continue
# Move the latlon a random amount, random() is in the range [0.0, 1.0)
target_lat = element.get('lat') + (random.random() - 0.5)
target_lon = element.get('lon') + (random.random() - 0.5)
url = mapbox_static.get_url(latitude=target_lat, longitude=target_lon)
print url
success = mapbox_static.download_tile(
element_id=element.get('id'),
url=url)
if success:
total_downloaded += 1