{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "0dd7cff5-4889-b7c7-e0fe-7f7c13ed460b"
      },
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c47f8770-b1b9-e83d-7134-32be20eff111"
      },
      "outputs": [],
      "source": [
        "SYSTEM = \"Kaggle\" # \"Paperspace\" # "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "0e9a9d8b-8918-d512-7e62-58228961b48c"
      },
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9e560d45-4456-7d96-8985-1c0e06d1d59b"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import os\n",
        "import cv2\n",
        "import matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "381ca545-bd4e-e730-4ae5-e7b74a04a6b1"
      },
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d66b9345-2a61-d9fd-4d83-791309a04bdc"
      },
      "outputs": [],
      "source": [
        "if SYSTEM == \"Kaggle\":\n",
        "    Image_Path = \"../input/Train/\"\n",
        "    Crop_Path  = \"./TestLargeCrop/\"\n",
        "else:\n",
        "    Image_Path = \"/home/paperspace/Project/Sealion/TestLargeRaw/\"\n",
        "    Crop_Path  = \"/home/paperspace/Project/Sealion/TestLargeCrop/\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "028b54d6-012e-1d45-f7f8-aecd50356bec"
      },
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d03f1299-6fe7-bec8-9ef5-dc9c2357a369"
      },
      "outputs": [],
      "source": [
        "Crop_Size = (416,416)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "099e8df5-7ce2-6f57-d11d-e9479cfc8015"
      },
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "81bea6fd-4006-7077-cd49-d8681be64a7f"
      },
      "outputs": [],
      "source": [
        "file_names = os.listdir(Image_Path)\n",
        "file_names = sorted(file_names, key=lambda \n",
        "                    item: (int(item.partition('.')[0]) if item[0].isdigit() else float('inf'), item))\n",
        "file_names = file_names[:1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d5fe98b5-055a-8d14-c26d-1a3f2091a178"
      },
      "outputs": [],
      "source": [
        "print(file_names)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "16a4d066-6243-ad31-c29b-ba5fc54c6f5d"
      },
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a99634ea-dcb6-5978-3723-4c302e31ad55"
      },
      "outputs": [],
      "source": [
        "def ceil_devide(Big, Small):\n",
        "    result = int(Big/Small)\n",
        "    if(Big%Small != 0):\n",
        "        result += 1\n",
        "    return result"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "50636a15-db2e-fcff-0fc1-7d7e7bdbc872"
      },
      "outputs": [],
      "source": [
        "def create_crop_template(filename):\n",
        "    ### remove existing template\n",
        "    if os.path.exists('crop_template.jpg'):\n",
        "        os.remove('crop_template.jpg')\n",
        "        \n",
        "    image = cv2.imread(Image_Path + filename)\n",
        "    image = image[:Crop_Size[1],:Crop_Size[0],:]\n",
        "    image = cv2.absdiff(image,image)\n",
        "\n",
        "    cv2.imwrite('crop_template.jpg',image)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "302a1d11-274c-3833-a442-714d1b57d41f"
      },
      "outputs": [],
      "source": [
        "def delete_crop_template():\n",
        "    os.remove('crop_template.jpg')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "25bc4bd1-989e-b513-9cc7-117a833237d6"
      },
      "outputs": [],
      "source": [
        "def create_crop_file():\n",
        "    if(SYSTEM == \"Kaggle\"):\n",
        "        if not os.path.exists(\"./TestLargeCrop/\"):\n",
        "            os.makedirs(\"./TestLargeCrop/\")\n",
        "        if not os.path.exists(\"./TestLargeCrop/JPEGImages/\"):\n",
        "            os.makedirs(\"./TestLargeCrop/JPEGImages/\")        \n",
        "    else:\n",
        "        if not os.path.exists(\"/home/paperspace/Project/Sealion/TestLargeCrop/\"):\n",
        "            os.makedirs(\"/home/paperspace/Project/Sealion/TestLargeCrop/\")\n",
        "        if not os.path.exists(\"/home/paperspace/Project/Sealion/TestLargeCrop/JPEGImages/\"):\n",
        "            os.makedirs(\"/home/paperspace/Project/Sealion/TestLargeCrop/JPEGImages/\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f5285bc1-ef36-9cb8-4527-8e5baae93936"
      },
      "outputs": [],
      "source": [
        "def delete_crop_image_names():\n",
        "    if os.path.exists(Crop_Path + \"TestLargeCrop.txt\"):\n",
        "        os.remove(Crop_Path + \"TestLargeCrop.txt\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "694fdd99-166a-76f3-d2de-89ee34a589f3"
      },
      "outputs": [],
      "source": [
        "create_crop_template(file_names[0])\n",
        "create_crop_file()\n",
        "\n",
        "delete_crop_image_names()\n",
        "crop_image_names = open(Crop_Path + \"TestLargeCrop.txt\", 'w')\n",
        "\n",
        "for filename in file_names:\n",
        "    ### skip if file is not image\n",
        "    if(filename[-3:] != 'jpg'):\n",
        "        continue\n",
        "        \n",
        "    ### read origin image\n",
        "    print(\"Cropping {0}\".format(filename))\n",
        "    ori_image = cv2.imread(Image_Path + filename)\n",
        "    Shape = ori_image.shape\n",
        "    X_Len = Shape[1]\n",
        "    Y_Len = Shape[0]\n",
        "    \n",
        "    X_Amt = ceil_devide(X_Len, Crop_Size[0])\n",
        "    Y_Amt = ceil_devide(Y_Len, Crop_Size[1])\n",
        "    \n",
        "    cnt = 0\n",
        "    for j in range(Y_Amt):\n",
        "        for i in range(X_Amt):\n",
        "            # counting\n",
        "            cnt += 1\n",
        "            \n",
        "            # create crop image\n",
        "            crop_image = cv2.imread('crop_template.jpg')\n",
        "            tmp_image  = ori_image[j*Crop_Size[1]:(j+1)*Crop_Size[1], i*Crop_Size[0]:(i+1)*Crop_Size[0], :]\n",
        "            crop_image[:tmp_image.shape[0], :tmp_image.shape[1], :] = tmp_image\n",
        "            \n",
        "            # save crop image\n",
        "            Name = Crop_Path + \"JPEGImages/\" + filename.split('.')[0] + '_' + str(cnt) + '.jpg'\n",
        "            cv2.imwrite(Name, crop_image) \n",
        "            \n",
        "            # write crop images in txt\n",
        "            crop_image_names.write(Name)\n",
        "            crop_image_names.write(\"\\n\")\n",
        "\n",
        "crop_image_names.close()\n",
        "delete_crop_template()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "30bd4053-9188-0078-0684-e579c1f34a13"
      },
      "outputs": [],
      "source": [
        "create_crop_file()\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ac9b3d1a-e4f4-2bf0-5aa7-ea6135c7041f"
      },
      "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
}