forked from worldbank/ml4dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
132 changed files
with
229 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
gray | ||
rectangle | ||
fit |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters