{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "692b7057-5285-45cc-342e-288c5aaae7ae"
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4d82bc68-360a-788c-80b7-65284cd53a6e"
      },
      "outputs": [],
      "source": [
        "\n",
        "data_path = \"../input/%s\"\n",
        "train_type_1_path = data_path % \"train/Type_1\"\n",
        "train_type_2_path = data_path % \"train/Type_2\"\n",
        "train_type_3_path = data_path % \"train/Type_3\"\n",
        "test_path = data_path % \"test/\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9f76f4b6-5c38-c328-d978-fb6182e928c8"
      },
      "outputs": [],
      "source": [
        "import cv2\n",
        "import os\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "from scipy import ndimage\n",
        "from functools import lru_cache\n",
        "from itertools import starmap\n",
        "import numpy as np\n",
        "import pandas as pd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "434725c5-a120-c480-e909-c8590baa8d35"
      },
      "outputs": [],
      "source": [
        "from functools import partial\n",
        "from itertools import islice"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b97a6b46-5b29-1812-0cc0-c3c7378a41ee"
      },
      "outputs": [],
      "source": [
        "type_1_img_files = filter(lambda x: x!=\".DS_Store\", os.listdir(train_type_1_path))\n",
        "type_2_img_files = filter(lambda x: x!=\".DS_Store\", os.listdir(train_type_2_path))\n",
        "type_3_img_files = filter(lambda x: x!=\".DS_Store\", os.listdir(train_type_3_path))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "825c4f0a-9084-1529-c9fb-65ee752d5a15"
      },
      "outputs": [],
      "source": [
        "def get_img_path(name, kind):\n",
        "    return {\n",
        "        \"type1\": \"%s/%s\" % (train_type_1_path, name),\n",
        "        \"type2\": \"%s/%s\" % (train_type_2_path, name),\n",
        "        \"type3\": \"%s/%s\" % (train_type_3_path, name),\n",
        "        \"test\": \"%s/%s\" % (test_path, name)\n",
        "    }[kind]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a65226e8-6f2f-e1f6-564a-74bcbf8aa9cb"
      },
      "outputs": [],
      "source": [
        "def load_img(name, kind):\n",
        "    try:\n",
        "        img = cv2.imread(get_img_path(name, kind))\n",
        "        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
        "        return img\n",
        "    except:\n",
        "        print(name, kind)\n",
        "\n",
        "def as_gray(img):\n",
        "    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "368820bf-b0a0-9bc8-4779-bba49d371b53"
      },
      "outputs": [],
      "source": [
        "def show_img(imgs, max):\n",
        "    fig = plt.figure(figsize=(12,8))\n",
        "    for i, t in zip(range(1, max + 1), imgs):\n",
        "        ax = fig.add_subplot(int(max / 5), 5, i)\n",
        "        plt.imshow(t)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "12b9434a-bb5e-ecbe-c963-66e67408365d"
      },
      "outputs": [],
      "source": [
        "def red_filted(img):\n",
        "    ori = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)\n",
        "    lower_red = np.array([100,100,200])\n",
        "    upper_red = np.array([255,255,255])\n",
        "    mask = cv2.inRange(ori, lower_red, upper_red)\n",
        "    return cv2.bitwise_and(img, img, mask=mask)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1c7265d4-4ecf-6f24-5fcd-d61d3f23b104"
      },
      "outputs": [],
      "source": [
        "type_1_imgs = list(islice(map(partial(load_img, kind=\"type1\"), type_1_img_files), 0, 10))\n",
        "type_2_imgs = list(islice(map(partial(load_img, kind=\"type2\"), type_2_img_files), 0, 10))\n",
        "type_3_imgs = list(islice(map(partial(load_img, kind=\"type3\"), type_3_img_files), 0, 10))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1aa79542-db53-4e9d-22c3-e019b6762f35"
      },
      "outputs": [],
      "source": [
        "type_1_gray_imgs = list(map(as_gray, type_1_imgs))\n",
        "type_2_gray_imgs = list(map(as_gray, type_2_imgs))\n",
        "type_3_gray_imgs = list(map(as_gray, type_3_imgs))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bd8e10a0-ef44-0373-b203-f5a30c6ab970"
      },
      "outputs": [],
      "source": [
        "type_1_mask_imgs = list(map(red_filted, type_1_imgs))\n",
        "type_2_mask_imgs = list(map(red_filted, type_2_imgs))\n",
        "type_3_mask_imgs = list(map(red_filted, type_3_imgs))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a57d5ccb-b48b-5bb1-ded9-7fd28973c2c7"
      },
      "outputs": [],
      "source": [
        "show_img(type_1_imgs, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d9ba9838-f97f-5ceb-62a3-ec6f0d504675"
      },
      "outputs": [],
      "source": [
        "show_img(type_1_gray_imgs, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8a5f76bc-d450-eebc-0d02-721c8e021169"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c480f416-d97b-bf26-1cee-0d1ee6259bf6"
      },
      "outputs": [],
      "source": [
        "show_img(type_2_imgs, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c4ab88fe-98dc-0bda-ab29-960b15832c20"
      },
      "outputs": [],
      "source": [
        "show_img(type_2_gray_imgs, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "983e0835-0664-9147-3d76-f4ce6a94a213"
      },
      "outputs": [],
      "source": [
        "show_img(type_2_mask_imgs, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2cee9f12-5414-6ad9-9a45-9ceaf6e15b8e"
      },
      "outputs": [],
      "source": [
        "show_img(type_3_imgs, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "511a9a66-012b-f557-6215-7acf78df99de"
      },
      "outputs": [],
      "source": [
        "show_img(type_3_gray_imgs, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f1b7712f-c002-2a94-aa2c-0a410fce0084"
      },
      "outputs": [],
      "source": [
        "show_img(type_3_mask_imgs, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "13ede3d4-0754-03dc-a618-9f47cc0a287f"
      },
      "outputs": [],
      "source": [
        "img = type_1_imgs[1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "88b632ad-5813-2468-32c2-22981dbe57c6"
      },
      "outputs": [],
      "source": [
        "def xxxx(img):\n",
        "    ori = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)\n",
        "    lower_red = np.array([100,100,100])\n",
        "    upper_red = np.array([255,255,255])\n",
        "    mask = cv2.inRange(ori, lower_red, upper_red)\n",
        "    return cv2.bitwise_and(img, img, mask=mask)\n",
        "\n",
        "\n",
        "\n",
        "plt.imshow(xxxx(img))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a00a5d53-5177-0e14-3f9f-5300f9110b23"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8c4ae01f-048f-2f79-d872-bd89de87903c",
        "collapsed": true
      },
      "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
}