Skip to content

Commit 5f15bcd

Browse files
author
Martha Morrissey
authored
Merge pull request #171 from developmentseed/endpoint
tms tile image format
2 parents 3311798 + 42a9b46 commit 5f15bcd

19 files changed

+58
-8
lines changed

docs/parameters.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Here is the full list of configuration parameters you can specify in a ``config.
4444
One of ``'classification'``, ``'object-detection'``, or ``'segmentation'``. This defines the output format for the final label numpy arrays (``y_train`` and ``y_test``).
4545

4646
``'classification'``
47-
Output is an array of ``len(classes) + 1``. Each array value will be either `1` or `0` based on whether it matches the class at the same index. The additional array element belongs to the background class, which will always be the first element.
47+
Output is an array of ``len(classes) + 1``. Each array value will be either `1` or `0` based on whether it matches the class at the same index. The additional array element belongs to the background class, which will always be the first element.
4848

4949
``'object-detection'``
5050
Output is an array of bounding boxes of the form ``[xmin, ymin, width, height, class_index]``. In this case, the values are pixel values measured from the upper left-hand corner (not latitude and longitude values). Each feature is tested against each class, so if a feature matches two or more classes, it will have the corresponding number of bounding boxes created.
@@ -65,3 +65,6 @@ Here is the full list of configuration parameters you can specify in a ``config.
6565

6666
**imagery_offset**: list of ints
6767
An optional list of integers representing the number of pixels to offset imagery. For example ``[15, -5]`` will move the images 15 pixels right and 5 pixels up relative to the requested tile bounds.
68+
69+
**tms_image_format**: string
70+
An option string that has the downloaded imagery's format such as `.jpg` or `.png` when it isn't provided by the endpoint

label_maker/package.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77
from PIL import Image
88

9-
from label_maker.utils import is_tif
9+
from label_maker.utils import is_tif, get_image_format
1010

1111

1212
def package_directory(dest_folder, classes, imagery, ml_type, seed=False,
@@ -72,10 +72,12 @@ def package_directory(dest_folder, classes, imagery, ml_type, seed=False,
7272
y_vals = []
7373

7474
# open the images and load those plus the labels into the final arrays
75-
o = urlparse(imagery)
76-
_, image_format = op.splitext(o.path)
7775
if is_tif(imagery): # if a TIF is provided, use jpg as tile format
7876
image_format = '.jpg'
77+
78+
else:
79+
image_format = get_image_format(imagery, kwargs)
80+
7981
for tile in tiles:
8082
image_file = op.join(dest_folder, 'tiles', '{}{}'.format(tile, image_format))
8183
try:

label_maker/utils.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ def class_match(ml_type, label, i):
2727
return np.count_nonzero(label == i)
2828
return None
2929

30+
def get_image_format(imagery, kwargs):
31+
if kwargs.get('tms_image_format'):
32+
image_format = kwargs.get('tms_image_format')
33+
else:
34+
o = urlparse(imagery)
35+
_, image_format = op.splitext(o.path)
36+
return image_format
37+
3038
def download_tile_tms(tile, imagery, folder, kwargs):
3139
"""Download a satellite image tile from a tms endpoint"""
32-
o = urlparse(imagery)
33-
_, image_format = op.splitext(o.path)
40+
41+
image_format = get_image_format(imagery, kwargs)
42+
3443
r = requests.get(url(tile.split('-'), imagery),
3544
auth=kwargs.get('http_auth'))
3645
tile_img = op.join(folder, '{}{}'.format(tile, image_format))

label_maker/validate.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
'seed': {'type': 'integer'},
3434
'imagery_offset': {'type': 'list', 'schema': {'type': 'integer'}, 'minlength': 2, 'maxlength': 2},
3535
'split_vals': {'type': 'list', 'schema': {'type': 'float'}},
36-
'split_names': {'type': 'list', 'schema': {'type': 'string'}}
36+
'split_names': {'type': 'list', 'schema': {'type': 'string'}},
37+
'tms_image_format': {'type': 'string'}
3738
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"country": "togo",
3+
"bounding_box": [1.09725, 6.05520, 1.34582, 6.30915],
4+
"zoom": 12,
5+
"classes": [
6+
{ "name": "Roads", "filter": ["has", "highway"] },
7+
{ "name": "Buildings", "filter": ["has", "building"] }
8+
],
9+
"imagery": "http://a.tiles.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.jpg?access_token=ACCESS_TOKEN",
10+
"background_ratio": 1,
11+
"ml_type": "classification",
12+
"tms_image_format": ".png"
13+
}
3.3 KB
Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

test/integration/test_classification_package.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ def setUpClass(cls):
2121
copyfile('test/fixtures/integration/labels-cl.npz', 'integration-cl-split/labels.npz')
2222
copytree('test/fixtures/integration/tiles', 'integration-cl-split/tiles')
2323

24+
makedirs('integration-cl-img-f')
25+
copyfile('test/fixtures/integration/labels-cl-img-f.npz', 'integration-cl-img-f/labels.npz')
26+
copytree('test/fixtures/integration/tiles_png', 'integration-cl-img-f/tiles')
27+
2428
@classmethod
2529
def tearDownClass(cls):
2630
rmtree('integration-cl')
2731
rmtree('integration-cl-split')
32+
rmtree('integration-cl-img-f')
2833

2934
def test_cli(self):
3035
"""Verify data.npz produced by CLI"""
@@ -73,4 +78,21 @@ def test_cli_3way_split(self):
7378
# validate label data with shapes
7479
self.assertEqual(data['y_train'].shape, (5, 7))
7580
self.assertEqual(data['y_test'].shape, (2, 7))
76-
self.assertEqual(data['y_val'].shape, (1, 7))
81+
self.assertEqual(data['y_val'].shape, (1, 7))
82+
83+
def test_tms_img_format(self):
84+
"""Verify data.npz produced by CLI"""
85+
86+
cmd = 'label-maker package --dest integration-cl-img-f --config test/fixtures/integration/config_tms_format_img.json'
87+
cmd = cmd.split(' ')
88+
subprocess.run(cmd, universal_newlines=True)
89+
90+
data = np.load('integration-cl-img-f/data.npz')
91+
92+
# validate our image data with shapes
93+
self.assertEqual(data['x_train'].shape, (9, 256, 256, 3))
94+
self.assertEqual(data['x_test'].shape, (3, 256, 256, 3))
95+
96+
# validate label data with shapes
97+
self.assertEqual(data['y_train'].shape, (9, 3))
98+
self.assertEqual(data['y_test'].shape, (3, 3))

0 commit comments

Comments
 (0)