{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\n\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.\nimport os\n\nimport cv2\nimport glob\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport matplotlib.pyplot as plt\n\nfrom skimage.feature import hog\nfrom skimage import color, exposure\nimport time"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "drivers = pd.read_csv('../input/driver_imgs_list.csv')\ntrain_files = [f for f in glob.glob(\"../input/train/*/*.jpg\")]\ntest_files = [\"../input/test/\" + f for f in os.listdir(\"../input/test/\")]\nprint(train_files[:10])\nprint(test_files[:10])\nprint('Shape of the image',cv2.imread(train_files[0]).shape)\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "import random\nfi = random.choice(train_files)\nprint(fi)\nim = cv2.imread(fi)\nplt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)); plt.axis('off')\n\n\n\n\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "lbl = {'c0' : 'safe driving', \n'c1' : 'texting - right', \n'c2' : 'talking on the phone - right', \n'c3' : 'texting - left', \n'c4' : 'talking on the phone - left', \n'c5' : 'operating the radio', \n'c6' : 'drinking', \n'c7' : 'reaching behind', \n'c8' : 'hair and makeup', \n'c9' : 'talking to passenger'}\n\nplt.rcParams['figure.figsize'] = (8.0, 20.0)\nplt.subplots_adjust(wspace=0, hspace=0)\ni_ = 0\nfor l in lbl:\n    tf = [\"../input/train/\" + l + \"/\" + f for f in os.listdir(\"../input/train/\" + l + \"/\")]\n    fi = random.choice(tf)\n    print(fi)\n    im = cv2.imread(fi)\n    plt.subplot(5, 2, i_+1).set_title(lbl[l])\n    plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)); plt.axis('off')\n    i_ += 1\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "from sklearn.feature_extraction.image import extract_patches_2d"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "i = random.choice(train_files)\nprint(fi)\nim = cv2.imread(fi)\nim.shape\nim= cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)\np=extract_patches_2d(im,(200, 200),max_patches=20,\n                                  random_state=0)\nfor i in range(0,20):\n    plt.subplot(10, 2, i+1).set_title('patches_'+str(i))\n    plt.imshow(p[i,:,:],cmap='Greys_r'); plt.axis('off')\n    "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "def feature_extraction(filename) :\n    im = cv2.imread(filename)\n    im= cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)\n    p=extract_patches_2d(im,(200, 200),max_patches=20,\n                                  random_state=0)\n    feature_vec=np.zeros((0))\n    for i in range(0,20):\n        f=np.histogram(hog(p[i,:,:]), bins=50, density=True)[0]\n        feature_vec=np.hstack((feature_vec,  f))\n        \n   \n    return feature_vec\n\n\n    \n    \n    "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "start=time.time()\nfeatures=np.zeros((len(train_files), 1000))\nLabels=[]\nfor i in range(len(train_files)):\n    features[i,:] =feature_extraction(train_files[i])\n    Labels.append(train_files[0][15:17])\n    if ((i+1)%100)==0 :\n        end=time.time()\n        break\nprint(end-start)\n    "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "len(train_files)* 45/(100*60*60)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}