import numpy as np
import glob
import os
class ImageHelpers:
def __init__(self):
self.sift_object = cv2.SIFT()
def read_image(path):
img = cv2.imread(path)
if img is None:
raise IOError("Unable to open '%s'. Are you sure it's a valid image path?")
return im
Returns:
--------
training_idxs, test_idxs, val_idxs
Both lists of integers
"""
if percent_test + percent_val >= 1.0:
raise ValueError('percent_test and percent_val must sum to less than 1.0')
row_range = range(total_rows)
no_test_rows = int(total_rows*(percent_test))
test_idxs = np.random.choice(row_range, size=no_test_rows, replace=False)
# remove test indexes
row_range = [idx for idx in row_range if idx not in test_idxs]
no_val_rows = int(total_rows*(percent_val))
val_idxs = np.random.choice(row_range, size=no_val_rows, replace=False)
# remove validation indexes
training_idxs = [idx for idx in row_range if idx not in val_idxs]
def gen_sift_features(labeled_img_paths):
"""
Generate SIFT features for images
Parameters:
-----------
labeled_img_paths : list of lists
Of the form [[image_path, label], ...]
Returns:
--------
img_descs : list of SIFT descriptors with same indicies as labeled_img_paths
y : list of corresponding labels
"""
# img_keypoints = {}
img_descs = []
name_dict = {}
train_labels = np.array([])
im_helper = ImageHelpers()
Parameters:
-----------
X : list of lists of SIFT descriptors (img_descs)
Returns:
--------
X, cluster_model :
X has K feature columns, each column corresponding to a visual word
cluster_model has been fit to the training set
"""
n_clusters = cluster_model.n_clusters
if all_train_descriptors.shape[1] != 128:
raise ValueError('Expected SIFT descriptors to have 128 features, got',
all_train_descriptors.shape[1])
# finally make a histogram of clustered word counts for each image. These are
the final features.
img_bow_hist = np.array(
[np.bincount(clustered_words, minlength=n_clusters) for clustered_words in
img_clustered_words])
X = img_bow_hist
print 'done generating BoW histograms.'
return X, cluster_model
Returns:
--------
X_train, X_test, X_val, y_train, y_test, y_val
"""
X_train = X[training_idxs]
X_test = X[test_idxs]
X_val = X[val_idxs]
y_train = y[training_idxs]
y_test = y[test_idxs]
y_val = y[val_idxs]
img = read_image(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
kp, desc = sift.detectAndCompute(gray, None)
clustered_desc = cluster_model.predict(desc)
img_bow_hist = np.bincount(clustered_desc, minlength=cluster_model.n_clusters)