block by vlandham 9532826

9532826

Full Screen

metallic_classifier.ipynb

{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import glob, os, sys\n",
      "import operator\n",
      "import glob\n",
      "\n",
      "from skimage import color as col\n",
      "\n",
      "#from colormath.color_objects import RGBColor\n",
      "from PIL import Image\n",
      "from StringIO import StringIO\n",
      "import numpy as np\n",
      "import pylab as pl\n",
      "from sklearn import svm, metrics, datasets"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def histeq(im, nbr_bins=256):\n",
      "    #get image histogram\n",
      "    imhist,bins = histogram(im.flatten(),nbr_bins,density=False)\n",
      "    cdf = imhist.cumsum() #cumulative distribution function\n",
      "    cdf = 255 * cdf / cdf[-1] #normalize\n",
      "\n",
      "    #print bins.shape\n",
      "    #use linear interpolation of cdf to find new pixel values\n",
      "    im2 = interp(im.flatten(),bins[:-1],cdf)\n",
      "\n",
      "    return im2.reshape(im.shape)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 90
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def get_ms(rgb):\n",
      "    m = (1.0/3.0) * float((sum(rgb)))\n",
      "    b_r = rgb[0] + rgb[2]\n",
      "\n",
      "    if(b_r >= 2 * rgb[1]):\n",
      "        s = (3.0 / 2.0) * (rgb[0] - m)\n",
      "    else:\n",
      "        s = (3.0 / 2.0) * (m - rgb[2])\n",
      "    return [int(m),int(s)]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 72
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def transform_ms(img):\n",
      "    PIXEL_SAMPLING_INTERVAL = 1\n",
      "    w, h, r = img.shape\n",
      "    transformed = np.zeros( (w,h,2), dtype=np.uint8)\n",
      "    for x in xrange(0, w, PIXEL_SAMPLING_INTERVAL):\n",
      "        for y in xrange(0, h, PIXEL_SAMPLING_INTERVAL):\n",
      "            transformed[x,y] = get_ms(img[x,y])\n",
      "\n",
      "    transformed[:,:,1] = histeq(transformed[:,:,1])\n",
      "    return transformed"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 92
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def get_img_data(img):\n",
      "    subset_len = 5\n",
      "    #img_subset = transform_ms(img[:subset_len,:subset_len])\n",
      "    img_subset = col.rgb2lab(img[:subset_len,:subset_len])\n",
      "    #img_subset = img[:subset_len,:subset_len]\n",
      "    #print img_subset.shape\n",
      "    img_subset = img_subset.reshape(-1)\n",
      "    #print img_subset.shape\n",
      "    return img_subset"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 99
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "metals_dir = \"metal_shoes_swatch\"\n",
      "non_metals_dir = \"non_metal_shoes_swatch\"\n",
      "\n",
      "metal_imgs = [get_img_data(pl.imread(img)) for img in glob.glob(metals_dir + \"/*.jpg\")]\n",
      "metal_targets = [\"metal\" for i in range(0,len(metal_imgs))]\n",
      "#print(metal_targets[0])\n",
      "#print len(metal_imgs)\n",
      "non_metal_imgs = [get_img_data(pl.imread(img)) for img in glob.glob(non_metals_dir + \"/*.jpg\")]\n",
      "non_metal_targets = [\"non\" for i in range(0,len(non_metal_imgs))]\n",
      "#print len(non_metal_imgs)\n",
      "#print metal_imgs[0].shape\n",
      "\n",
      "#metal_data = [get_data(img) for img in metal_imgs]\n",
      "\n",
      "train_num = 50\n",
      "\n",
      "train_data = metal_imgs[:train_num]\n",
      "test_data = metal_imgs[train_num:]\n",
      "train_data = train_data + non_metal_imgs[:train_num]\n",
      "test_data = test_data + non_metal_imgs[train_num:]\n",
      "\n",
      "train_targets = metal_targets[:train_num]\n",
      "test_targets = metal_targets[train_num:]\n",
      "\n",
      "train_targets = train_targets + non_metal_targets[:train_num]\n",
      "test_targets = test_targets + non_metal_targets[train_num:]\n",
      "\n",
      "print train_targets\n",
      "\n",
      "\n",
      "digits = datasets.load_digits()\n",
      "\n",
      "# To apply an classifier on this data, we need to flatten the image, to\n",
      "# turn the data in a (samples, feature) matrix:\n",
      "#n_samples = len(digits.images)\n",
      "#print digits.images.shape\n",
      "#data = digits.images.reshape((n_samples, -1))\n",
      "#print(n_samples)\n",
      "#print data[0].shape"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "['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']\n"
       ]
      }
     ],
     "prompt_number": 100
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "classifier = svm.SVC(gamma=0.001)\n",
      "# We learn the digits on the first half of the digits\n",
      "#classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2])\n",
      "classifier.fit(train_data, train_targets)\n",
      "\n",
      "# Now predict the value of the digit on the second half:\n",
      "expected = test_targets\n",
      "predicted = classifier.predict(test_data)\n",
      "print predicted\n",
      "\n",
      "print(\"Classification report for classifier %s:\\n%s\\n\"\n",
      "      % (classifier, metrics.classification_report(expected, predicted)))\n",
      "print(\"Confusion matrix:\\n%s\" % metrics.confusion_matrix(expected, predicted))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "['metal' 'metal' 'non' 'non' 'non' 'non' 'non' 'non' 'non' 'metal' 'metal'\n",
        " 'non' 'non' 'non' 'non' 'metal' 'metal' 'metal' 'non' 'non' 'non' 'non'\n",
        " 'metal' 'non' 'non' 'non' 'non' 'metal' 'non' 'non' 'non' 'non' 'non'\n",
        " 'non' 'non' 'non' 'non' 'non' 'non' 'non']\n",
        "Classification report for classifier SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,\n",
        "  gamma=0.001, kernel=rbf, max_iter=-1, probability=False,\n",
        "  random_state=None, shrinking=True, tol=0.001, verbose=False):\n",
        "             precision    recall  f1-score   support\n",
        "\n",
        "      metal       0.78      0.35      0.48        20\n",
        "        non       0.58      0.90      0.71        20\n",
        "\n",
        "avg / total       0.68      0.62      0.59        40\n",
        "\n",
        "\n",
        "Confusion matrix:\n",
        "[[ 7 13]\n",
        " [ 2 18]]\n"
       ]
      }
     ],
     "prompt_number": 101
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}