{"nbformat": 4, "cells": [{"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", "\n", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "\n", "import os\n", "import gzip\n", "import json\n", "\n", "import PIL\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import skimage\n", "pd.set_option('max_columns', 50)\n", "pd.set_option('max_rows', 1000)\n", "\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "matplotlib.style.use('ggplot')\n", "%matplotlib inline\n", "matplotlib.rcParams['figure.figsize'] = (8, 6)\n", "\n", "from pandas.io.parsers import read_csv\n", "from sklearn.utils import shuffle\n", "\n", "from IPython.core.display import display, HTML, Image\n", "\n"], "metadata": {"_cell_guid": "52e00fd8-8540-4903-874b-995c98beefb2", "_uuid": "d42dd80a45edef1c06b9d18a478c08c05176f5c4"}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": ["def img_to_array(img):\n", "    return np.array(img.getdata()).reshape(img.width, img.width, 3) / 255\n", "\n", "\n", "def trim(im):\n", "    \"\"\"trim black margin, http://stackoverflow.com/questions/10615901/trim-whitespace-using-pil\"\"\"\n", "    bg = PIL.Image.new(im.mode, im.size, im.getpixel((0,0)))\n", "    diff = PIL.ImageChops.difference(im, bg)\n", "    diff = PIL.ImageChops.add(diff, diff, 2.0, -20)\n", "    bbox = diff.getbbox()\n", "    if bbox:\n", "        return im.crop(bbox)\n", "\n", "\n", "def calc_thumbnail_size(img):\n", "    \"\"\"calculate thumbnail size with constant aspect ratio\"\"\"\n", "    width, length = img.size\n", "    ratio = width / length\n", "\n", "    # for some reason, if it's exactly 224, then thumnailed image is 223\n", "    dim = 224 + 1          # output dim\n", "    if ratio > 1:\n", "        size = (dim * ratio, dim)\n", "    else:\n", "        size = (dim, dim / ratio)\n", "#     print(size)\n", "    return size\n", "\n", "\n", "def calc_crop_coords(img):\n", "    \"\"\"crop to square of desired dimension size\"\"\"\n", "    dim = 224\n", "    width, length = img.size\n", "    left = 0\n", "    right = width\n", "    bottom = length\n", "    top = 0\n", "    if width > dim:\n", "        delta = (width - dim) / 2\n", "        left = delta\n", "        right = width - delta\n", "    if length > dim:\n", "        delta = (length - dim) / 2\n", "        top = delta\n", "        bottom = length - delta\n", "    return (left, top, right, bottom)\n", "\n", "\n", "def preprocess(img):\n", "    img = trim(img)\n", "    tsize = calc_thumbnail_size(img)\n", "    img.thumbnail(tsize)\n", "    crop_coords = calc_crop_coords(img)\n", "    img = img.crop(crop_coords)\n", "    return img\n"], "metadata": {"collapsed": true}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": ["df = pd.read_csv('../input/trainLabels.csv')"], "metadata": {"collapsed": true}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": ["df.level.value_counts().to_frame(name='count')"], "metadata": {}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": ["df = df.query('image in {0}'.format([_.replace('.jpeg', '') for _ in os.listdir('../input/')]))\n"], "metadata": {"collapsed": true}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": ["data_dir = '../input/'"], "metadata": {"collapsed": true}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": ["PIL.__version__"], "metadata": {}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": ["imgs_with_label = []\n", "n_samples = 5\n", "for i in range(5):\n", "    _vals = df.query('level == {0}'.format(i)).sample(n_samples).image.apply(\n", "        lambda v: (os.path.join(data_dir, v) + '.jpeg', i)).values.tolist()\n", "    imgs_with_label.extend(_vals)\n", "\n", "fig, axes = plt.subplots(5, 5, figsize=(16, 16))\n", "axes = axes.ravel()\n", "for k, (img, label) in enumerate(imgs_with_label):\n", "    im = PIL.Image.open(img)\n", "    im = preprocess(im)\n", "    ax = axes[k]\n", "    ax.imshow(img_to_array(im))\n", "    ax.set_xticklabels([])\n", "    ax.set_yticklabels([])\n", "    ax.grid(False)\n", "    if k % 5 == 0:\n", "        ax.set_ylabel('level = {0}'.format(label))\n", "    ax.set_title(os.path.basename(img))\n"], "metadata": {}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": ["imgs_with_label = []\n", "n_samples = 5\n", "for i in range(5):\n", "    _vals = df.query('level == {0}'.format(i)).sample(n_samples).image.apply(\n", "        lambda v: (os.path.join(data_dir, v) + '.jpeg', i)).values.tolist()\n", "    imgs_with_label.extend(_vals)\n", "\n", "fig, axes = plt.subplots(5, 5, figsize=(16, 16))\n", "axes = axes.ravel()\n", "for k, (img, label) in enumerate(imgs_with_label):\n", "    im = PIL.Image.open(img)\n", "    im = preprocess(im)\n", "    ax = axes[k]\n", "    ax.imshow(img_to_array(im))\n", "    ax.set_xticklabels([])\n", "    ax.set_yticklabels([])\n", "    ax.grid(False)\n", "    if k % 5 == 0:\n", "        ax.set_ylabel('level = {0}'.format(label))\n", "    ax.set_title(os.path.basename(img))"], "metadata": {}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": ["imgs_with_label = []\n", "n_samples = 5\n", "for i in range(5):\n", "    _vals = df.query('level == {0}'.format(i)).sample(n_samples).image.apply(\n", "        lambda v: (os.path.join(data_dir, v) + '.jpeg', i)).values.tolist()\n", "    imgs_with_label.extend(_vals)\n", "\n", "fig, axes = plt.subplots(5, 5, figsize=(16, 16))\n", "axes = axes.ravel()\n", "for k, (img, label) in enumerate(imgs_with_label):\n", "    im = PIL.Image.open(img)\n", "    im = preprocess(im)\n", "    ax = axes[k]\n", "    ax.imshow(img_to_array(im))\n", "    ax.set_xticklabels([])\n", "    ax.set_yticklabels([])\n", "    ax.grid(False)\n", "    if k % 5 == 0:\n", "        ax.set_ylabel('level = {0}'.format(label))\n", "    ax.set_title(os.path.basename(img))"], "metadata": {}, "execution_count": null, "cell_type": "code", "outputs": []}, {"source": [], "metadata": {"collapsed": true}, "execution_count": null, "cell_type": "code", "outputs": []}], "nbformat_minor": 1, "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "3.6.3", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython3", "codemirror_mode": {"version": 3, "name": "ipython"}}}}