Skip to content

Commit

Permalink
almost full flow
Browse files Browse the repository at this point in the history
  • Loading branch information
zugaldia committed Jun 23, 2015
1 parent af8a053 commit fe807a9
Show file tree
Hide file tree
Showing 132 changed files with 229 additions and 52 deletions.
44 changes: 44 additions & 0 deletions 05_build_samplesfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from utils.geo import get_rectangle
from utils.geo import WAYS_DATA_FILENAME
import json
import os

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

samples_data = {}

image_files = [f for f in os.listdir('satellite/gray') if f.endswith('.png')]
for image_file in image_files:
print 'Processing %s...' % image_file

# Find the category
sport = image_file[image_file.find('_')+1:image_file.rfind('_')]
if sport not in samples_data:
samples_data[sport] = []

# 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)
if w <= 25 or h <= 25:
print 'Pic not added'
continue
if x <= 0 or y <= 0 or w <= 0 or h <= 0:
print 'Pic not added'
continue
entry = 'satellite/gray/%s\t1\t%d\t%d\t%d\t%d\n' % (image_file, x, y, w, h)
samples_data[sport].append(entry)

for sport in samples_data.keys():
datafile = 'info_%s.dat' % sport
print 'Saving data file: %s' % datafile
with open(datafile, 'w') as f:
print len(samples_data[sport])
for entry in samples_data[sport]:
f.write(entry)
68 changes: 16 additions & 52 deletions 05_draw_bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,38 @@
will use this box to better train our ML algo.
'''

from utils.geo import get_rectangle
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)
# 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')]
print len(image_files)

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']
# 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)
# 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)

# To show the image
# cv2.imshow('img',img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# 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()
62 changes: 62 additions & 0 deletions 07_fit_min_neighbors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from __future__ import division
import csv
import cv2
import numpy as np
import os

tennis_cascade_files = [
'output/cascade-default.xml',
'output/cascade-4000-2000.xml',
'output/cascade-6000-3000.xml',
'output/cascade-8000-4000.xml']

positive_files = [os.path.join('satellite/fit', f) \
for f in os.listdir('satellite/fit') if f.endswith('.png')]

negative_files = [os.path.join('satellite/fit/negative', f) \
for f in os.listdir('satellite/fit/negative') if f.endswith('.png')]

def get_total_pitches(tennis_cascade, filename, min_neighbors):
img = cv2.imread(filename, 0)
pitches = tennis_cascade.detectMultiScale(
img, minNeighbors=min_neighbors)
return len(pitches)

for tennis_cascade_file in tennis_cascade_files:
print tennis_cascade_file
tennis_cascade = cv2.CascadeClassifier(tennis_cascade_file)

# Open
positive_f = open(tennis_cascade[:-4] + '_positive.csv', 'w')
negative_f = open(tennis_cascade[:-4] + '_negative.csv', 'w')
positive_writer = csv.writer(positive_f)
negative_writer = csv.writer(negative_f)

for min_neighbors in range(0, 501, 10):
print min_neighbors

# Pos
total_set = 0
for positive_file in positive_files:
total_pitches = get_total_pitches(
tennis_cascade=tennis_cascade,
filename=positive_file,
min_neighbors=min_neighbors)
total_set += total_pitches
total_average = total_set / len(positive_files)
positive_writer.writerow([total_average, min_neighbors])

# Neg
total_set = 0
for negative_file in negative_files:
total_pitches = get_total_pitches(
tennis_cascade=tennis_cascade,
filename=negative_file,
min_neighbors=min_neighbors)
total_set += total_pitches
total_average = total_set / len(negative_files)
negative_writer.writerow([total_average, min_neighbors])

# Close
positive_f.close()
negative_f.close()
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ step4:
python 04_download_satellite.py --sport volleyball --count 10
python 04_download_satellite.py --sport multi --count 10
python 04_download_satellite.py --sport softball --count 10
# mv satellite/*png satellite/training/
# mv satellite/*png satellite/testing/

step5:
# Draw the bbox on the test files
Expand All @@ -33,3 +35,13 @@ step5:
step6:
# Get some random images
python 06_get_negatives.py --count 25
find satellite/negative -type f > negative.txt

createsamples:
opencv_createsamples -info info_baseball.dat -num 99 -vec info_baseball.vec
opencv_createsamples -info info_basketball.dat -num 100 -vec info_basketball.vec
opencv_createsamples -info info_tennis.dat -num 9998 -vec info_tennis.vec
opencv_traincascade -data output -vec info_baseball.vec -bg negative.txt -numPos 99 -numNeg 100
opencv_traincascade -data output -vec info_basketball.vec -bg negative.txt -numPos 100 -numNeg 100
opencv_traincascade -data output -vec info_tennis.vec -bg negative.txt -numPos 9998 -numNeg 5000

58 changes: 58 additions & 0 deletions detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np
import cv2

baseball_cascade = cv2.CascadeClassifier('baseball.xml')
basketball_cascade = cv2.CascadeClassifier('basketball.xml')
tennis_cascade = cv2.CascadeClassifier('tennis.xml')

# testfile = 'satellite/training/pitch_baseball_220727025.png'
# testfile = 'satellite/training/pitch_baseball_222703638.png'
# testfile = 'satellite/training/pitch_baseball_223914194.png'
# testfile = 'satellite/training/pitch_baseball_226905824.png'
# testfile = 'satellite/training/pitch_baseball_227372226.png'
# testfile = 'satellite/training/pitch_baseball_227683244.png'

# testfile = 'satellite/detection/pitch_baseball_133230978.png'
# testfile = 'satellite/detection/pitch_baseball_133593974.png'
# testfile = 'satellite/detection/pitch_baseball_134874855.png'
# testfile = 'satellite/detection/pitch_baseball_199130202.png'
# testfile = 'satellite/detection/pitch_baseball_284527697.png'
# testfile = 'satellite/detection/pitch_baseball_317137177.png'
# testfile = 'satellite/detection/pitch_baseball_48331085.png'
# testfile = 'satellite/detection/pitch_baseball_68467399.png'
# testfile = 'satellite/detection/pitch_baseball_81189377.png'
# testfile = 'satellite/detection/pitch_baseball_97575184.png'

# testfile = 'satellite/detection/pitch_tennis_105660674.png'
# testfile = 'satellite/detection/pitch_tennis_120231577.png'
# testfile = 'satellite/detection/pitch_tennis_172547292.png'
# testfile = 'satellite/detection/pitch_tennis_177425633.png'
# testfile = 'satellite/detection/pitch_tennis_224740547.png'
# testfile = 'satellite/detection/pitch_tennis_250911604.png'
# testfile = 'satellite/detection/pitch_tennis_285058169.png'
# testfile = 'satellite/detection/pitch_tennis_290182837.png'
# testfile = 'satellite/detection/pitch_tennis_302813940.png'
# testfile = 'satellite/detection/pitch_tennis_343232913.png'

# testfile = 'satellite/detection/pitch_basketball_139165791.png'
# testfile = 'satellite/detection/pitch_basketball_156416713.png'
testfile = 'satellite/detection/pitch_basketball_242894925.png'
# testfile = 'satellite/detection/pitch_basketball_256427642.png'
# testfile = 'satellite/detection/pitch_basketball_271825251.png'
# testfile = 'satellite/detection/pitch_basketball_276916820.png'
# testfile = 'satellite/detection/pitch_basketball_302422677.png'
# testfile = 'satellite/detection/pitch_basketball_331162643.png'
# testfile = 'satellite/detection/pitch_basketball_332627356.png'
# testfile = 'satellite/detection/pitch_basketball_48665083.png'

img = cv2.imread(testfile)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

pitches = basketball_cascade.detectMultiScale(gray, minNeighbors=200)
print 'Pitches found: %d' % len(pitches)
for (x,y,w,h) in pitches:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
1 change: 1 addition & 0 deletions satellite/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
gray
rectangle
fit
Binary file removed satellite/negative/negative_1239337358.png
Binary file not shown.
Binary file removed satellite/negative/negative_1465628912.png
Binary file not shown.
Binary file removed satellite/negative/negative_1469806886.png
Binary file not shown.
Binary file removed satellite/negative/negative_1477462366.png
Binary file not shown.
Binary file removed satellite/negative/negative_1479946096.png
Binary file not shown.
Binary file removed satellite/negative/negative_1509079165.png
Binary file not shown.
Binary file removed satellite/negative/negative_1531763822.png
Binary file not shown.
Binary file removed satellite/negative/negative_1551531602.png
Binary file not shown.
Binary file removed satellite/negative/negative_1659769605.png
Binary file not shown.
Binary file removed satellite/negative/negative_1687660480.png
Binary file not shown.
Binary file removed satellite/negative/negative_1770303109.png
Binary file not shown.
Binary file removed satellite/negative/negative_2094211295.png
Binary file not shown.
Binary file removed satellite/negative/negative_2190175391.png
Binary file not shown.
Binary file removed satellite/negative/negative_2545053098.png
Binary file not shown.
Binary file removed satellite/negative/negative_2603479878.png
Binary file not shown.
Binary file removed satellite/negative/negative_2624336037.png
Binary file not shown.
Binary file removed satellite/negative/negative_2701555441.png
Binary file not shown.
Binary file removed satellite/negative/negative_2894565032.png
Binary file not shown.
Binary file removed satellite/negative/negative_3058393913.png
Binary file not shown.
Binary file removed satellite/negative/negative_3172336959.png
Binary file not shown.
Binary file removed satellite/negative/negative_3235053434.png
Binary file not shown.
Binary file removed satellite/negative/negative_3254025089.png
Binary file not shown.
Binary file removed satellite/negative/negative_3407598977.png
Binary file not shown.
Binary file removed satellite/negative/negative_386283285.png
Binary file not shown.
Binary file removed satellite/negative/negative_779049065.png
Binary file not shown.
Binary file removed satellite/pitch_american_football_138015612.png
Diff not rendered.
Binary file removed satellite/pitch_american_football_208324572.png
Diff not rendered.
Binary file removed satellite/pitch_american_football_222718773.png
Diff not rendered.
Binary file removed satellite/pitch_american_football_243596300.png
Diff not rendered.
Binary file removed satellite/pitch_american_football_256677303.png
Diff not rendered.
Binary file removed satellite/pitch_american_football_289134124.png
Diff not rendered.
Binary file removed satellite/pitch_american_football_312162610.png
Diff not rendered.
Binary file removed satellite/pitch_american_football_322936139.png
Diff not rendered.
Binary file removed satellite/pitch_american_football_324615361.png
Diff not rendered.
Binary file removed satellite/pitch_american_football_47851872.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_133230978.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_133593974.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_134874855.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_199130202.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_284527697.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_317137177.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_48331085.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_68467399.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_81189377.png
Diff not rendered.
Binary file removed satellite/pitch_baseball_97575184.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_139165791.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_156416713.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_242894925.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_256427642.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_271825251.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_276916820.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_302422677.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_331162643.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_332627356.png
Diff not rendered.
Binary file removed satellite/pitch_basketball_48665083.png
Diff not rendered.
Binary file removed satellite/pitch_golf_175176750.png
Diff not rendered.
Binary file removed satellite/pitch_golf_227387788.png
Diff not rendered.
Binary file removed satellite/pitch_golf_245472836.png
Diff not rendered.
Binary file removed satellite/pitch_golf_251673614.png
Diff not rendered.
Binary file removed satellite/pitch_golf_268203925.png
Diff not rendered.
Binary file removed satellite/pitch_golf_278760920.png
Diff not rendered.
Binary file removed satellite/pitch_golf_302318288.png
Diff not rendered.
Binary file removed satellite/pitch_golf_330838130.png
Diff not rendered.
Binary file removed satellite/pitch_golf_338074783.png
Diff not rendered.
Binary file removed satellite/pitch_golf_44348494.png
Diff not rendered.
Binary file removed satellite/pitch_multi_120856186.png
Diff not rendered.
Binary file removed satellite/pitch_multi_126928909.png
Diff not rendered.
Binary file removed satellite/pitch_multi_133192148.png
Diff not rendered.
Binary file removed satellite/pitch_multi_191660333.png
Diff not rendered.
Binary file removed satellite/pitch_multi_198722358.png
Diff not rendered.
Binary file removed satellite/pitch_multi_208578647.png
Diff not rendered.
Binary file removed satellite/pitch_multi_227726069.png
Diff not rendered.
Binary file removed satellite/pitch_multi_297125974.png
Diff not rendered.
Binary file removed satellite/pitch_multi_306784237.png
Diff not rendered.
Binary file removed satellite/pitch_multi_83861607.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_141729871.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_162680940.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_169921118.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_170622155.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_191871212.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_207004847.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_231250753.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_315148027.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_40666614.png
Diff not rendered.
Binary file removed satellite/pitch_soccer_55975004.png
Diff not rendered.
Binary file removed satellite/pitch_softball_170604460.png
Diff not rendered.
Binary file removed satellite/pitch_softball_205824376.png
Diff not rendered.
Binary file removed satellite/pitch_softball_234840502.png
Diff not rendered.
Binary file removed satellite/pitch_softball_285275356.png
Diff not rendered.
Binary file removed satellite/pitch_softball_296091024.png
Diff not rendered.
Binary file removed satellite/pitch_softball_31000730.png
Diff not rendered.
Binary file removed satellite/pitch_softball_310462293.png
Diff not rendered.
Binary file removed satellite/pitch_softball_311432471.png
Diff not rendered.
Binary file removed satellite/pitch_softball_69701783.png
Diff not rendered.
Binary file removed satellite/pitch_softball_96046405.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_105660674.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_120231577.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_172547292.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_177425633.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_224740547.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_250911604.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_285058169.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_290182837.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_302813940.png
Diff not rendered.
Binary file removed satellite/pitch_tennis_343232913.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_123680240.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_151948029.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_152578925.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_152688738.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_229338257.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_264786946.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_286780390.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_288411386.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_339454428.png
Diff not rendered.
Binary file removed satellite/pitch_unknown_91981160.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_188558393.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_202503673.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_206157716.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_216231362.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_233697784.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_242666079.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_268478401.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_269077687.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_295193244.png
Diff not rendered.
Binary file removed satellite/pitch_volleyball_330316573.png
Diff not rendered.
36 changes: 36 additions & 0 deletions utils/geo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from shapely.geometry import Polygon
import math

image_width = 1280
image_height = 1280

ELEMENTS_FILENAME = 'data/elements.json'
WAYS_DATA_FILENAME = 'data/ways.json'
Expand Down Expand Up @@ -27,3 +31,35 @@ def get_ways_data(elements, coords):

# Done
return ways_data

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
latitude_factor = 111132.0 / 0.21
longitude_factor = 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

0 comments on commit fe807a9

Please sign in to comment.