{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "08a4edc4-b9c8-1a75-5540-375ab16d0ac7"
      },
      "source": [
        "Data Augmentation\n",
        "\n",
        "Motivation: Increase the number of training samples for non-mammal features.\n",
        "\n",
        "Input: large image\n",
        "Output: Smaller images for training CNN model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bc3729b6-042a-5a59-c1e5-6ac26352adab"
      },
      "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",
        "\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",
        "# 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",
        "\n",
        "from subprocess import check_output\n",
        "print(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": "99a3f0b7-3d37-a389-4599-a6b766b5dd9d"
      },
      "outputs": [],
      "source": [
        "import math\n",
        "import os\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "from skimage.transform import resize\n",
        "from skimage.transform import rotate\n",
        "from skimage import color\n",
        "from glob import glob"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "84aeef25-53ab-ad05-83fc-a36516d29dcd"
      },
      "outputs": [],
      "source": [
        "#Custom directories\n",
        "custom_files = glob('data/RawFeatures/*.*') #Input Dir for my cropped images\n",
        "samples_dir = 'data/classes/' #Output directory for cropped images\n",
        "samples = glob('data/classes/*.*') # input \n",
        "augmented = 'data/augmented/' # folder to store augmented samples\n",
        "#\n",
        "training_dotted = glob('../input/TrainDotted/*.jpg')\n",
        "trainset = glob('../input/Train/*.jpg')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "236c98d6-5cb1-ce46-4abe-40bebd917888"
      },
      "outputs": [],
      "source": [
        "#Misc data structures \n",
        "classes = ['adult_males','subadult_males','adult_females','juveniles','pups','error']\n",
        "tel = {'red': 1, 'magenta': 1,'brown':1,'blue':1,'green':1,'black':0}\n",
        "keys = ['red', 'magenta','brown','blue','green','black']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "aa9d54fd-da9d-35c0-2449-750c5a5d8741"
      },
      "outputs": [],
      "source": [
        "#Input Image\n",
        "#output cropped images based on the specified dimensions\n",
        "\n",
        "def squared_slice(image_path, out_name, outdir, slice_size,threshold):\n",
        "    from skimage.io import imsave\n",
        "    img = plt.imread(image_path)\n",
        "    #---------------------------------------------------------\n",
        "    img=img[0:500,1000:1500] #remove this line before using\n",
        "    #---------------------------------------------------------\n",
        "    width, height, d= img.shape\n",
        "\n",
        "    slices_h = int(math.ceil(height/slice_size))\n",
        "    slices_v = int(math.ceil(width/slice_size))\n",
        "    name = image_path # (image_path.split(\"\\\\\")[1]).split(\".\")[0]\n",
        "\n",
        "    if width*height > threshold:\n",
        "        n=0\n",
        "        for s_h in range(slices_h-1):\n",
        "            for s_v in range(slices_v-1):\n",
        "                x_r1 = ((s_h)*slice_size)\n",
        "                x_r2 = (slice_size*s_h+slice_size)\n",
        "                \n",
        "                x_y1 = ((s_v)*slice_size)\n",
        "                x_y2 = (slice_size*s_v+slice_size)\n",
        "                \n",
        "\n",
        "                slic = img[x_r1:x_r2,x_y1:x_y2] #Cropped\n",
        "                plt.imshow(slic)\n",
        "                plt.show()\n",
        "                # I save the files in a different folder for further processing\n",
        "                try:\n",
        "                    filename = samples_dir + out_name +\"-\" + name+\"-\" +\"-\" +str(n) +\".jpg\"\n",
        "               #     imsave(filename, slic)\n",
        "                except:\n",
        "                     print( image_path +\":\" + str(x_r1)+str(x_r2) + str(x_y1) + str(x_y2) )\n",
        "                n+=1\n",
        "    else:\n",
        "        filename = samples_dir + out_name +\"-\" + name+\"-\" +\"-\" +str(n) +\".jpg\"\n",
        "        imsave(filename, slic)\n",
        "        return"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "12b3707f-7107-2f11-a21c-49fdc20bc9f8"
      },
      "outputs": [],
      "source": [
        "#Input: Image\n",
        "#Output: One image per rotation angle\n",
        "# dark areas are pesent around the edges of rotated images.\n",
        "\n",
        "def rotate_img(img,h,w,name):\n",
        "    from skimage.io import imsave\n",
        "    temp=np.zeros((0,h*w))\n",
        "    img = resize(img, (h,w),mode='reflect')\n",
        "    img = color.rgb2grey(img)\n",
        "\n",
        "    #temp = np.append(temp,org,axis=0)\n",
        "    rads = [ 20,280 ] #increase as needed.\n",
        "\n",
        "    n=0\n",
        "    for i in rads:\n",
        "        img_r = rotate(img,i)\n",
        "\n",
        "        try:\n",
        "            plt.imshow(img_r)\n",
        "            plt.show()\n",
        "            #imsave( filename , img_r)\n",
        "        except:\n",
        "            print(\"low contrast\")\n",
        "        n+=1\n",
        "    "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7b90905b-7252-d26c-b867-4142bbfaddfd"
      },
      "outputs": [],
      "source": [
        "#3for i in training_dotted[1]:\n",
        "#i = i[0:200,0:200]\n",
        "\n",
        "img = plt.imread(training_dotted[0])\n",
        "img2=img[0:1000,1000:2000]\n",
        "print(\"Original Image\")\n",
        "plt.imshow(img)\n",
        "plt.show()\n",
        "    #Y , X\n",
        "print(\"cropped images\") #\n",
        "plt.imshow(img[0:1000,1000:2000])\n",
        "plt.show()\n",
        "# Arguments\n",
        "#file path\n",
        "# output name\n",
        "# Output dir\n",
        "# slice size\n",
        "# Threshold = Width * height\n",
        "squared_slice(training_dotted[0],'sliced',samples_dir,500,400)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "e59d630a-2dd5-97fd-c940-604b724ab052"
      },
      "source": [
        "Image Rotation"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "61df58d3-c7b2-aa70-f6fd-90a95052bb27"
      },
      "outputs": [],
      "source": [
        "h=500\n",
        "w=500\n",
        "img = plt.imread(training_dotted[1])\n",
        "rotate_img(img,h,w,'test' )"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c775a1c5-5586-b035-e504-df2ec7e67cb7"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "faf5cf27-5a22-f5d6-d986-9f180bf8f711"
      },
      "source": [
        "#Extract sealions from Train files\n",
        "#Use these snippet + rotate function above to create your training set\n",
        "train_data = 'data/Train/*.jpg'\n",
        "#Coordinate files came from threeplusone Thanks\n",
        "#https://www.kaggle.com/threeplusone/sea-lion-coordinates\n",
        "coord = pd.read_csv('coords.csv') # some coordinate file\n",
        "\n",
        "window = 48 # 1/2 of actual window, adjust as needed.\n",
        "\n",
        "error_file = [] \n",
        "for file in train_data:\n",
        "    pic = file.split('.')[0] # pic number/name\n",
        "    pic = pic.split('/')[2]\n",
        "    pic =int(pic)\n",
        "    sub_coord = coord.loc[coord['tid']==pic,: ]#Grap all coords for that pic\n",
        "    # print(file)\n",
        "    pic_count = 0\n",
        "    for c in sub_coord.values:\n",
        "        tid,cls,row,col=c[0],c[1],c[2],c[3]\n",
        "        img = plt.imread(file)\n",
        "\n",
        "        cx1 = row-window\n",
        "        cx2 = row+window\n",
        "        cy1 = col-window\n",
        "        cy2 = col+window\n",
        "        sl1=img[cx1:cx2,cy1:cy2]\n",
        "        filename = output_dir + str(tid) +\"-\" + str(cls) +\"-\" +str(pic_count) +\".jpg\"\n",
        "        try:\n",
        "            plt.imsave(filename,sl1)\n",
        "        except:\n",
        "            print(\"error: \" + file )\n",
        "            error_file = np.append(error_file,file)\n",
        "        pic_count +=1\n",
        "    plt.close('all')"
      ]
    }
  ],
  "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
}