Skip to content

Commit

Permalink
sample negative images
Browse files Browse the repository at this point in the history
  • Loading branch information
zugaldia committed Jun 17, 2015
1 parent 9fafd46 commit af8a053
Show file tree
Hide file tree
Showing 29 changed files with 140 additions and 0 deletions.
76 changes: 76 additions & 0 deletions 05_draw_bbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'''
We're gonna use the bbox info from OSM to draw a box around the pitch, we
will use this box to better train our ML algo.
'''

from utils.geo import WAYS_DATA_FILENAME
import cv2
import json
import math
import os

image_width = 1280
image_height = 1280

def get_rectangle(bounds):
# This converts a latitude delta into an image delta. For USA, at zoom
# level 19, we know that we have 0.21 meters/pixel. So, an image is showing
# about 1280 pixels * 0.21 meters/pixel = 268.8 meters.
# On the other hand we know that at the same angle, a degress in latlon is:
# https://en.wikipedia.org/wiki/Latitude
# latitude = 111,132 m
# longitude = 78,847 m
# Finally, we add 5% to make sure the feature is contained within the
# rectangle (from what we see, the bbox are tight around the feature)
latitude_factor = 1.05 * 111132.0 / 0.21
longitude_factor = 1.05 * 78847.0 / 0.21

# Feature size
feature_width = longitude_factor * math.fabs(bounds[1] - bounds[3])
feature_height = latitude_factor * math.fabs(bounds[0] - bounds[2])
if feature_width > image_width or feature_height > image_height:
print '** Warning ** The feature is bigger than the image.'

# CV params (int required)
x = int((image_width / 2) - (feature_width / 2))
y = int((image_height / 2) - (feature_height / 2))
w = int(feature_width)
h = int(feature_height)
if w <= 25 or h <= 25:
print '** Warning ** This image has very narrow bounds.'
print bounds
print x, y, w, h
if x == 0 or y == 0 or w == 0 or h == 0:
print '** Warning ** There is something wrong with this image bounds.'
print bounds
print x, y, w, h
return x, y, w, h

# We need the elements
print 'Loading %s...' % WAYS_DATA_FILENAME
with open(WAYS_DATA_FILENAME, 'r') as f:
ways_data = json.load(f)

image_files = [f for f in os.listdir('satellite') if f.endswith('.png')]

for image_file in image_files:
print 'Processing %s...' % image_file
# The ID is between the last underscore and the extension dot
# For example: pitch_volleyball_268478401.png -> 268478401
way_id = image_file[image_file.rfind('_') + 1:image_file.find('.')]
bounds = ways_data[way_id]['bounds']

# Add a rectangle with the feature
x, y, w, h = get_rectangle(bounds=bounds)
img = cv2.imread(os.path.join('satellite', image_file))
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imwrite(os.path.join('satellite/rectangle', image_file), img)

# Generate a grayscale version
img_gray = cv2.imread(os.path.join('satellite', image_file), 0)
cv2.imwrite(os.path.join('satellite/gray', image_file), img_gray)

# To show the image
# cv2.imshow('img',img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
58 changes: 58 additions & 0 deletions 06_get_negatives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ step4:
step5:
# Draw the bbox on the test files
python 05_draw_bbox.py

step6:
# Get some random images
python 06_get_negatives.py --count 25
2 changes: 2 additions & 0 deletions satellite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gray
rectangle
Binary file added satellite/negative/negative_1239337358.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1465628912.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1469806886.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1477462366.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1479946096.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1509079165.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1531763822.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1551531602.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1659769605.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1687660480.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_1770303109.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_2094211295.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_2190175391.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_2545053098.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_2603479878.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_2624336037.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_2701555441.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_2894565032.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_3058393913.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_3172336959.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_3235053434.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_3254025089.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_3407598977.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_386283285.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added satellite/negative/negative_779049065.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit af8a053

Please sign in to comment.