In [1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from modules.ml_utils import *
from modules.image_utils import *
from modules.utils import *
from modules.image_sample import ImageSample
from find_green import simple_find_green
from modules.preprocess import Preprocess
%matplotlib inline
In [3]:
cv2.destroyAllWindows()
In [ ]:
file_path = '../data/images/v.jpg'
img = cv2.imread(file_path)
result = simple_find_green(file_path)
res = np.hstack((img,binarized_to_rgb(result)))
cv2_imshow('res', res)
In [5]:
# CLAHE demo
img = cv2.imread('../data/images/v.jpg')
normalized = apply_clahe_rgb(img)
res = np.hstack((img,normalized))
res = cv2_resize(res, y=400)
#cv2_imshow('res',res)
cv2.imwrite('clahe.jpg', res)
Out[5]:
In [7]:
prep = Preprocess()
clahe = prep.sharp_image(img)
images = [img, clahe]
modes = ['median', 'gaussian']
clahe_mode = [True, False]
for mode in modes:
for clahe_application in clahe_mode:
blur = prep.blur_image(img, mode=mode, apply_clahe=clahe_application)
images.append(blur)
res = np.hstack(images)
res = cv2_resize(res, y=400)
#cv2_imshow('res',res)
cv2.imwrite('prep.jpg', res)
Out[7]:
In [10]:
# initialize directories
root_dir = './data/textures'
positive_textures = os.path.join(root_dir, 'positive')
negative_textures = os.path.join(root_dir, 'negative')
negative_files = list_files(negative_textures)
positive_files = list_files(positive_textures)
In [11]:
# load textures
samples = []
def load_labeles_samples(files, label):
return [(ImageSample(path=img_file), label) for img_file in files]
samples = []
samples.extend(load_labeles_samples(negative_files, 0))
samples.extend(load_labeles_samples(positive_files, 1))
In [12]:
# load samples
X = []
y = []
samples_per_texture = 1024
for img_sample, label in samples:
X.extend(img_sample.to_samples(samples_per_texture))
y.extend([label] * samples_per_texture)
In [14]:
# sanity check
print '# Sample images', len(samples)
print '# samples', len(X)
print '# labels', len(y)
print '# features', len(X[0])
In [15]:
models = [DummyClassifier(), GaussianNB(), LinearSVC(), SVC(), DecisionTreeClassifier(), RandomForestClassifier()]
parameters = [{}, {}, {},{'kernel':('linear', 'rbf','poly','sigmoid')},{}, {}]
models = [GaussianNB()]
parameters = [{}]
In [16]:
evaluate_models(X, y, models, parameters, verbose=True)
In [15]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
model = RandomForestClassifier()
model.fit(X_train, y_train)
printScore(y_train, model.predict(X_train))
printScore(y_test, model.predict(X_test))
In [16]:
# Free data from textures
X = []
y = []
X_train = X_test = y_train = y_test = []
In [17]:
# Store model
filename = './stored_models/tree_clahe.joblib.pkl'
store_model(filename, model)
In [3]:
# Load saved model
filename = './stored_models/tree2.joblib.pkl'
model = load_model(filename)
In [4]:
@time_usage
def analyze_single_image(file_path, model):
print 'Processing file', file_path
img = cv2.imread(file_path)
#img = cv2_resize(img, y=600)
img_name = os.path.basename(file_path).split('.')[0]
#file_path = './' + img_name + '_rsz.jpg'
#cv2.imwrite(file_path, img)
out_path = './' + img_name + '_out.jpg'
img = ImageSample(path=file_path)
#raw_input('continue')
result = img.find_vegetation(model)
simple = simple_find_green(file_path)
simple[simple==1] = 255
result[result==1] = 255
result = binarized_to_rgb(result)
simple = binarized_to_rgb(simple)
grid = np.hstack([img.img_rgb, result, simple])
cv2.imwrite(out_path, grid)
def analyze_whole_dir(dir_path, model):
for file_path in list_files(dir_path):
analyze_single_image(file_path, model)
In [8]:
# single image
file_path = '/home/marko/PycharmProjects/SSIP16/data/ssip_data/test.jpg'
analyze_single_image(file_path, model)
In [5]:
# whole directory analysis
dir_path = '/home/marko/PycharmProjects/SSIP16/data/ssip_data/'
analyze_whole_dir(dir_path, model)
In [28]:
cv2.destroyAllWindows()
In [10]:
list_files = lambda path: [os.path.join(path, img_path) for img_path in os.listdir(path)]
root = './data/images'
pos_dir = './new_textures/pos/'
neg_dir = './new_textures/neg/'
map(init_dir, [root, pos_dir, neg_dir])
rect_size = 150
step = 15
current_id = 0
for file_path in list_files(root):
img = cv2.imread(file_path)
height, width, _ = img.shape
print file_path, height, width
i, j = (0, 0)
def get_file_name(dir_path, i):
return os.path.join(dir_path, '%d.jpg' % i)
def slide_rect():
global current_id, i, j
def out_of_range(i, j):
return i < 0 or i >= (height - rect_size) \
or j < 0 or j >= (width - rect_size)
while True:
tmp = img.copy()
texture = img[i:i+rect_size, j:j+rect_size,]
cv2.rectangle(tmp,(j, i),(j+rect_size,i+rect_size),(0,255,0),3)
cv2.imshow('original', tmp)
cv2.imshow('texture', texture)
key = cv2.waitKey(0)
di = 0
dj = 0
if key == ord('n'):
print 'continue'; return
elif key == ord('q'):
return True
#store texture
elif key == ord('1'):
current_id += 1;
cv2.imwrite(get_file_name(pos_dir, current_id), texture)
elif key == ord('2'):
current_id += 1;
cv2.imwrite(get_file_name(neg_dir, current_id), texture)
# movement
elif key == ord('w'): di = -step
elif key == ord('s'): di = step
elif key == ord('a'): dj = -step
elif key == ord('d'): dj = step
ni, nj = (i+di, j+dj)
if not out_of_range(ni, nj):
i, j = (ni, nj)
ret = slide_rect()
if ret: break
print 'Completed'
In [11]:
cv2.destroyAllWindows()
In [ ]: