{"nbformat": 4, "cells": [{"cell_type": "code", "execution_count": null, "outputs": [], "source": ["import pandas as pd\n", "import numpy as np\n", "import bson\n", "import matplotlib.pyplot as plt\n", "import cv2 #opencv library \n", "from skimage import data, io, filters, feature #essential when extracting features\n", "\n", "\n", "from skimage.transform import (hough_line, hough_line_peaks,\n", "                               probabilistic_hough_line)\n", "from skimage.feature import canny,hog,corner_harris"], "metadata": {"_uuid": "69d2f60ea815912daec71effa42a470c6074a258", "collapsed": true, "_cell_guid": "e4247924-275c-40c5-b1e2-d99c3628ff5c"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["path = '../input/'\n", "!ls \"$path\""], "metadata": {"_uuid": "f4819fe071e35294188487b0ca4581773b3943ab", "_cell_guid": "bc577e0e-1ebf-4895-a22c-9fb85d2c7460"}}, {"cell_type": "markdown", "source": ["#### Read Files"], "metadata": {"_uuid": "b098aa074fdefad0304399f1b48c454ecc0e6ef8", "_cell_guid": "fd9ab510-a956-484e-95af-a900c777ea45"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["categories = pd.read_csv('{}{}'.format(path,'category_names.csv'),\n", "                         index_col=0)"], "metadata": {"_uuid": "903efc4617c0f6a533ca2301e790222702457e78", "collapsed": true, "_cell_guid": "ffd8a247-43e5-4b50-b331-bbc9fffe121f"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["categories.head()"], "metadata": {"_uuid": "8b27132a34ceb37780f23c0f15179f0b557562e2", "_cell_guid": "111721b7-5ef2-4771-a847-3caa7c6d25a6"}}, {"cell_type": "markdown", "source": ["#### Read bson file and convert to pandas DataFrame"], "metadata": {"_uuid": "56ea1ba90258d79ca7254fff2dde4a385321f6d4", "_cell_guid": "17fc803e-5350-4b20-bcbc-be348738d6c5"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["with open('{}{}'.format(path,'train_example.bson'),'rb') as b:\n", "    df = pd.DataFrame(bson.decode_all(b.read()))\n", "#read and convert to opencv image\n", "df['imgs'] = df['imgs'].apply(lambda rec: cv2.imdecode(\n", "    np.fromstring(rec[0]['picture'], np.uint8), cv2.IMREAD_COLOR))\n", "#change index to catergory id\n", "df.set_index('category_id',inplace=True)\n", "# Combine images and categries\n", "df[categories.columns.tolist()] = categories.loc[df.index]"], "metadata": {"_uuid": "4fa168dcd1047f38951ab529e2fb3d5bc633043b", "collapsed": true, "_cell_guid": "3dd561e5-409f-46d2-8159-fab560c5c36c"}}, {"cell_type": "markdown", "source": ["###### Run through filter"], "metadata": {"_uuid": "59ac341bdbf264c3bf5c378fdc19ab793e78046a", "_cell_guid": "02e5d02f-2680-4746-85ab-639d342824b7"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["def get_edges(img):\n", "    #convert to grayscale\n", "    gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", "    edges = filters.prewitt_h(gray)\n", "    return edges\n", "df['prewitt_img'] = df['imgs'].apply(get_edges)"], "metadata": {"_uuid": "ad762506a3574b29d15517f07ccc5670e6252fe8", "collapsed": true, "_cell_guid": "f02e3778-e73b-41bd-a014-1d3b66b44bf3"}}, {"cell_type": "markdown", "source": ["### Conrner detection"], "metadata": {"_uuid": "f809a02bf0ea062d0c9841acda6ee2b35b07fafb", "collapsed": true, "_cell_guid": "0cf92dcb-c6e5-4a5d-8a6c-d142dfd78f1f"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["def get_corners(img):\n", "    #convert to grayscale\n", "    gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", "    corners = feature.corner_fast(gray)\n", "    return corners\n", "df['corners'] = df['imgs'].apply(get_corners)"], "metadata": {"_uuid": "f02ce75fc675d5737af4357f4a6b68a4264f6c92", "collapsed": true, "_cell_guid": "067e9514-1d7e-4cb5-afc6-2262c55874c2"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["df['corners'].head()"], "metadata": {"_uuid": "05f0d092c839fe67024045c975720a6f2fbb6a70", "_cell_guid": "c2f37dc6-2e27-4491-94c5-9a91846d8f22"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["fig,axs  = plt.subplots(4,4,figsize=(8,8))\n", "title = df['category_level1'].str.split('-').str[0].str.strip()\n", "# title += ','\n", "# title += df['category_level2'].str.split('-').str[0].str.strip()\n", "#specify which block of images to display\n", "n=50\n", "title = title.tolist()\n", "\n", "axs = axs.flatten()\n", "\n", "for i,ax in enumerate(axs):\n", "    ax.imshow(df.iloc[i+n,1][:,:,1],cmap='gray')\n", "    #convert the edges matrix to image array\n", "    ax.contour((np.interp(df.iloc[i+n,-2],\n", "                      [df.iloc[i+n,-2].min(),df.iloc[i+n,-1].max()], #old range\n", "                      [0,255] #new range\n", "                        )>10).astype(np.uint8),colors='b',lw=.5)\n", "    ax.contour((df.iloc[i+n,-1]).astype(np.uint8),\n", "               colors='r',lw=3.)\n", "    ax.set_title(title[i+n])\n", "    #remove frame and ticks\n", "    ax.axis('off')\n", "\n", "plt.tight_layout()\n", "plt.suptitle('Filter+corner')\n", " #make room for main title\n", "fig.subplots_adjust(top=0.90)\n", "#create and show legend\n", "proxy = [plt.Rectangle((0,0),1,1,fc = pc) for pc in ['gray','blue','red']]\n", "axs[0].legend(proxy, ['Grayscale image','Prewitt_h Filter','Corner detection'],\n", "           bbox_to_anchor=(0.1,.9), loc=\"upper right\",frameon=False);\n"], "metadata": {"_uuid": "e34a3dec53ef1fc8ba90b1acc5fefbaad2abef65", "_cell_guid": "89d62d30-4fbb-4083-8e15-457276577a8f"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["\n", "image = df[-10][...,1]\n", "#pass the image through a canny edge detecter\n", "edges = canny(image, 2, 1, 25)\n", "#apply hough transform\n", "lines = probabilistic_hough_line(edges, threshold=20, line_length=10,\n", "                                 line_gap=3)\n", "\n", "# Generating figure 2\n", "fig, axes = plt.subplots(1, 3)\n", "axs = axes.flatten()\n", "\n", "axs[0].imshow(image, cmap=cm.gray)\n", "axs[0].set_title('Input image')\n", "\n", "axs[1].imshow(edges, cmap=cm.gray)\n", "axs[1].set_title('Canny edges')\n", "\n", "axs[2].imshow(edges * 0)\n", "for line in lines:\n", "    p0, p1 = line\n", "    ax[2].plot((p0[0], p1[0]), (p0[1], p1[1]))\n", "ax[2].set_xlim((0, image.shape[1]))\n", "ax[2].set_ylim((image.shape[0], 0))\n", "ax[2].set_title('Probabilistic Hough')\n", "\n", "for a in ax:\n", "    a.set_axis_off()\n", "    a.set_adjustable('box-forced')\n", "\n", "plt.tight_layout()\n", "plt.show()"], "metadata": {"_uuid": "07388ceae088d789b2aa05bf7c2f8243b786b4ef", "collapsed": true, "_cell_guid": "d7e16ea3-2ec7-4767-a64f-07bef838b999"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": [], "metadata": {"_uuid": "4b415705649bd4acccfa03f96adc8f20a992a37f", "collapsed": true, "_cell_guid": "6b6b5f19-cc95-46b2-acb6-5e8ef755f9c4"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": [], "metadata": {"_uuid": "5dd3568272fadd7f966678a53c82d4ecabcfae4b", "collapsed": true, "_cell_guid": "fd340538-cee7-40cb-9aa2-e81a849d2fab"}}], "metadata": {"kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}, "language_info": {"name": "python", "version": "3.6.1", "mimetype": "text/x-python", "pygments_lexer": "ipython3", "codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "file_extension": ".py"}}, "nbformat_minor": 1}