{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9971e1fe-2055-0051-3b28-2e72930cff0b"},"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\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."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2280e48f-add1-7870-4ec1-c7c638026b8a"},"outputs":[],"source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom subprocess import check_output\nimport matplotlib.pyplot as plt\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\nfrom glob import glob\nimport seaborn as sns\nfrom scipy import stats\n\ndf = pd.read_csv('../input/Train/train.csv')\nprint(\"{} training samples total\".format(df.shape[0]))\ndf.head()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"3070973c-68db-6af4-7bf5-f1ee423f6e21"},"outputs":[],"source":"df[['adult_males', 'subadult_males', 'adult_females', 'juveniles', 'pups']].sum(axis=0).plot.barh()\n\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"60ae847e-d56e-15cd-6dee-a0c38dc69ade"},"outputs":[],"source":"def corrfunc(x, y, **kws):\n    r, _ = stats.pearsonr(x, y)\n    ax = plt.gca()\n    ax.annotate(\"r = {:.3f}\".format(r),\n                xy=(.1, .9), xycoords=ax.transAxes)\n\ng = sns.PairGrid(df[['adult_males', 'subadult_males', 'adult_females', 'juveniles', 'pups']], palette=[\"red\"])\n#g.map_upper(plt.scatter, s=10)\ng.map_lower(plt.scatter, s=10)\ng.map_diag(sns.distplot, kde=False)\n#g.map_lower(sns.kdeplot, cmap=\"Blues_d\")\ng.map_lower(corrfunc)\n#sns.pairplot(df)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"78db745a-6a36-2e3c-4e56-2627c6448ae3"},"outputs":[],"source":"all_types = ['adult_males', 'subadult_males', 'adult_females', 'juveniles', 'pups']\nall_normalized_types = ['normalized_'+t for t in all_types]\nrow_counts = df[['adult_males', 'subadult_males', 'adult_females', 'juveniles', 'pups']].sum(axis=1)\n\nfor t in all_types:\n    df['normalized_'+t] = df[t].divide(row_counts)\n\ndf.head()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"7ae96add-0fd2-35f6-90f3-7e442136fa07"},"outputs":[],"source":"sns.clustermap(\n    df[all_normalized_types].fillna(0.0),\n    col_cluster=False,\n    cmap=plt.get_cmap('viridis'),\n    figsize=(12,10)\n)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"23a725c8-f71c-dd1a-d01f-ad9262a06410"},"outputs":[],"source":"training_images = glob('../input/Train/*.jpg')\ntraining_dotted = glob('../input/TrainDotted/*.jpg')\nlen(training_images), len(training_dotted)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"fdd0fce0-d772-5842-4f1f-c48e54157c3c"},"outputs":[],"source":"fig = plt.figure(figsize=(16,10))\nfor i in range(4):\n    ax = fig.add_subplot(2,2,i+1)\n    plt.imshow(plt.imread(training_images[i]))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c954034d-925b-903d-0a3e-a3cc9059cb01"},"outputs":[],"source":"from skimage.io import imread, imshow\nfrom skimage.util import crop\nimport cv2\n\ncropped_dotted = cv2.cvtColor(cv2.imread('../input/TrainDotted/8.jpg'), cv2.COLOR_BGR2RGB)[500:1500,2000:2800,:]\ncropped_raw = cv2.cvtColor(cv2.imread('../input/Train/8.jpg'), cv2.COLOR_BGR2RGB)[500:1500,2000:2800,:]\n\nfig = plt.figure(figsize=(12,8))\nax = fig.add_subplot(1,2,1)\nplt.imshow(cropped_dotted)\nax = fig.add_subplot(1,2,2)\nplt.imshow(cropped_raw)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2f54e3f0-a09a-a002-68de-ab4d49ab35e3"},"outputs":[],"source":"diff = cv2.subtract(cropped_dotted, cropped_raw)\ndiff = diff/diff.max()\nplt.figure(figsize=(12,8))\nplt.imshow((diff > 0.20).astype(float))\nplt.grid(False)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1bc6df98-5138-201b-d088-ad38fd7b2efd"},"outputs":[],"source":"diff = cv2.absdiff(cropped_dotted, cropped_raw)\ngray = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY)\nret,th1 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)\n\ncnts = cv2.findContours(th1.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]\nprint(\"Sea Lions Found: {}\".format(len(cnts)))\n\nx, y = [], []\n\nlion_patches = []\n\nfor loc in cnts:\n    ((xx, yy), _) = cv2.minEnclosingCircle(loc)\n\n    # store patches of some sea lions\n    if xx > 10 and xx < gray.shape[1] - 10:\n        lion_patches.append(cropped_raw[yy-10:yy+10, xx-10:xx+10])\n\n    x.append(xx)\n    y.append(yy)\n\nx = np.array(x)\ny = np.array(y)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"63c35a3d-d318-5d06-62ac-d43656c1e983"},"outputs":[],"source":"from scipy.stats.kde import gaussian_kde\n\nk = gaussian_kde(np.vstack([x, y]), bw_method=0.5)\nxi, yi = np.mgrid[x.min():x.max():x.size**0.5*1j,y.min():y.max():y.size**0.5*1j]\nzi = k(np.vstack([xi.flatten(), yi.flatten()]))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d5f2de97-b94f-7437-8f2e-1c93a67eb933"},"outputs":[],"source":"fig = plt.figure(figsize=(12,12))\nax1 = fig.add_subplot(211)\nax2 = fig.add_subplot(212)\n\n# alpha=0.5 will make the plots semitransparent\nax1.pcolormesh(xi, yi, zi.reshape(xi.shape), alpha=0.5)\nax2.contourf(xi, yi, zi.reshape(xi.shape), alpha=0.5)\n\nax1.set_xlim(x.min(), x.max())\nax1.set_ylim(y.min(), y.max())\nax2.set_xlim(x.min(), x.max())\nax2.set_ylim(y.min(), y.max())\n\nax1.imshow(cropped_raw, extent=[x.min(), x.max(), y.min(), y.max()], aspect='auto')\nax2.imshow(cropped_raw, extent=[x.min(), x.max(), y.min(), y.max()], aspect='auto')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"dea318fd-a8e2-00ac-b124-a0ca29810e94"},"outputs":[],"source":""}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}