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