In [13]:
import glob, os, sys
import operator
import glob

from skimage import color as col

#from colormath.color_objects import RGBColor
from PIL import Image
from StringIO import StringIO
import numpy as np
import pylab as pl
from sklearn import svm, metrics, datasets
In [90]:
def histeq(im, nbr_bins=256):
    #get image histogram
    imhist,bins = histogram(im.flatten(),nbr_bins,density=False)
    cdf = imhist.cumsum() #cumulative distribution function
    cdf = 255 * cdf / cdf[-1] #normalize

    #print bins.shape
    #use linear interpolation of cdf to find new pixel values
    im2 = interp(im.flatten(),bins[:-1],cdf)

    return im2.reshape(im.shape)
In [72]:
def get_ms(rgb):
    m = (1.0/3.0) * float((sum(rgb)))
    b_r = rgb[0] + rgb[2]

    if(b_r >= 2 * rgb[1]):
        s = (3.0 / 2.0) * (rgb[0] - m)
    else:
        s = (3.0 / 2.0) * (m - rgb[2])
    return [int(m),int(s)]
In [92]:
def transform_ms(img):
    PIXEL_SAMPLING_INTERVAL = 1
    w, h, r = img.shape
    transformed = np.zeros( (w,h,2), dtype=np.uint8)
    for x in xrange(0, w, PIXEL_SAMPLING_INTERVAL):
        for y in xrange(0, h, PIXEL_SAMPLING_INTERVAL):
            transformed[x,y] = get_ms(img[x,y])

    transformed[:,:,1] = histeq(transformed[:,:,1])
    return transformed
In [99]:
def get_img_data(img):
    subset_len = 5
    #img_subset = transform_ms(img[:subset_len,:subset_len])
    img_subset = col.rgb2lab(img[:subset_len,:subset_len])
    #img_subset = img[:subset_len,:subset_len]
    #print img_subset.shape
    img_subset = img_subset.reshape(-1)
    #print img_subset.shape
    return img_subset
In [100]:
metals_dir = "metal_shoes_swatch"
non_metals_dir = "non_metal_shoes_swatch"

metal_imgs = [get_img_data(pl.imread(img)) for img in glob.glob(metals_dir + "/*.jpg")]
metal_targets = ["metal" for i in range(0,len(metal_imgs))]
#print(metal_targets[0])
#print len(metal_imgs)
non_metal_imgs = [get_img_data(pl.imread(img)) for img in glob.glob(non_metals_dir + "/*.jpg")]
non_metal_targets = ["non" for i in range(0,len(non_metal_imgs))]
#print len(non_metal_imgs)
#print metal_imgs[0].shape

#metal_data = [get_data(img) for img in metal_imgs]

train_num = 50

train_data = metal_imgs[:train_num]
test_data = metal_imgs[train_num:]
train_data = train_data + non_metal_imgs[:train_num]
test_data = test_data + non_metal_imgs[train_num:]

train_targets = metal_targets[:train_num]
test_targets = metal_targets[train_num:]

train_targets = train_targets + non_metal_targets[:train_num]
test_targets = test_targets + non_metal_targets[train_num:]

print train_targets


digits = datasets.load_digits()

# To apply an classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
#n_samples = len(digits.images)
#print digits.images.shape
#data = digits.images.reshape((n_samples, -1))
#print(n_samples)
#print data[0].shape
['metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'metal', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non', 'non']

In [101]:
classifier = svm.SVC(gamma=0.001)
# We learn the digits on the first half of the digits
#classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2])
classifier.fit(train_data, train_targets)

# Now predict the value of the digit on the second half:
expected = test_targets
predicted = classifier.predict(test_data)
print predicted

print("Classification report for classifier %s:\n%s\n"
      % (classifier, metrics.classification_report(expected, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))
['metal' 'metal' 'non' 'non' 'non' 'non' 'non' 'non' 'non' 'metal' 'metal'
 'non' 'non' 'non' 'non' 'metal' 'metal' 'metal' 'non' 'non' 'non' 'non'
 'metal' 'non' 'non' 'non' 'non' 'metal' 'non' 'non' 'non' 'non' 'non'
 'non' 'non' 'non' 'non' 'non' 'non' 'non']
Classification report for classifier SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
  gamma=0.001, kernel=rbf, max_iter=-1, probability=False,
  random_state=None, shrinking=True, tol=0.001, verbose=False):
             precision    recall  f1-score   support

      metal       0.78      0.35      0.48        20
        non       0.58      0.90      0.71        20

avg / total       0.68      0.62      0.59        40


Confusion matrix:
[[ 7 13]
 [ 2 18]]

In []: