{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipfml.processing.segmentation import divide_in_blocks\n",
    "from ipfml.processing import transform\n",
    "from ipfml.filters import convolution as conv\n",
    "from ipfml.filters import kernels\n",
    "from ipfml import utils\n",
    "\n",
    "from PIL import Image\n",
    "from scipy import signal\n",
    "from skimage import color\n",
    "import scipy.stats as stats\n",
    "import seaborn as sns\n",
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from numpy.linalg import svd\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_folder = \"../dataset\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data augmentation analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Utils functions definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compute_images_path(dict_data):\n",
    "    \n",
    "    scene = dict_data['name']\n",
    "    prefix = dict_data['prefix']\n",
    "    indices = dict_data['indices']\n",
    "    \n",
    "    images_path = []\n",
    "    for index in indices:\n",
    "        path = os.path.join(data_folder, os.path.join(scene, prefix + index + \".png\"))\n",
    "        print(path)\n",
    "        images_path.append(path)\n",
    "    return images_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_images_zones(dict_data, images_path):\n",
    "    \n",
    "    id_zone = dict_data['zone']\n",
    "    \n",
    "    zones_img = []\n",
    "    for path in images_path:\n",
    "        img = Image.open(path)\n",
    "        zones = divide_in_blocks(img, (200, 200))\n",
    "        zones_img.append(zones[id_zone])\n",
    "        \n",
    "    return zones_img"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scenes information data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# start 00020 - ref 00900 - step 10\n",
    "dict_appart = {'name': 'Appart1opt02', \n",
    "               'prefix': 'appartAopt_', \n",
    "               'indices': [\"00050\", \"00100\", \"00200\", \"00300\", \"00400\", \"00500\", \"00700\", \"00900\"],\n",
    "               'zone': 9}\n",
    "\n",
    "# start 00050 - ref 01200 - step 10\n",
    "dict_cuisine = {'name': 'Cuisine01', \n",
    "               'prefix': 'cuisine01_', \n",
    "               'indices': [\"00050\", \"00100\", \"00200\", \"00300\", \"00400\", \"00600\", \"00700\",\"00900\", \"01200\"],\n",
    "               'zone': 6}\n",
    "\n",
    "# start 00020 - ref 00950 - step 10\n",
    "dict_sdb_c = {'name': 'SdbCentre', \n",
    "               'prefix': 'SdB2_', \n",
    "               'indices': [\"00020\", \"00400\", \"00950\"],\n",
    "               'zone': 3}\n",
    "\n",
    "# start 00020 - ref 00950 - step 10\n",
    "dict_sdb_d = {'name': 'SdbDroite', \n",
    "               'prefix': 'SdB2_D_', \n",
    "               'indices': [\"00020\", \"00400\", \"00950\"],\n",
    "               'zone': 3}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Definition of parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../dataset/Appart1opt02/appartAopt_00050.png\n",
      "../dataset/Appart1opt02/appartAopt_00100.png\n",
      "../dataset/Appart1opt02/appartAopt_00200.png\n",
      "../dataset/Appart1opt02/appartAopt_00300.png\n",
      "../dataset/Appart1opt02/appartAopt_00400.png\n",
      "../dataset/Appart1opt02/appartAopt_00500.png\n",
      "../dataset/Appart1opt02/appartAopt_00700.png\n",
      "../dataset/Appart1opt02/appartAopt_00900.png\n",
      "../dataset/Cuisine01/cuisine01_00050.png\n",
      "../dataset/Cuisine01/cuisine01_00100.png\n",
      "../dataset/Cuisine01/cuisine01_00200.png\n",
      "../dataset/Cuisine01/cuisine01_00300.png\n",
      "../dataset/Cuisine01/cuisine01_00400.png\n",
      "../dataset/Cuisine01/cuisine01_00600.png\n",
      "../dataset/Cuisine01/cuisine01_00700.png\n",
      "../dataset/Cuisine01/cuisine01_00900.png\n",
      "../dataset/Cuisine01/cuisine01_01200.png\n"
     ]
    }
   ],
   "source": [
    "images_path_appart = compute_images_path(dict_appart)\n",
    "images_path_cuisine = compute_images_path(dict_cuisine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "zones_appart = get_images_zones(dict_appart, images_path_appart)\n",
    "zones_cuisine = get_images_zones(dict_cuisine, images_path_cuisine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_augmented_images(zone):\n",
    "    \n",
    "    output_block_img = zone\n",
    "    \n",
    "    # Data augmentation!\n",
    "    augmented_images = []\n",
    "    legends = []\n",
    "    rotations = [0, 90, 180, 270]\n",
    "    img_flip_labels = ['original', 'horizontal']\n",
    "\n",
    "    horizontal_img = output_block_img.transpose(Image.FLIP_LEFT_RIGHT)\n",
    "\n",
    "    flip_images = [output_block_img, horizontal_img]\n",
    "\n",
    "    # rotate and flip image to increase dataset size\n",
    "    for id, flip in enumerate(flip_images):\n",
    "        for id_rot, rotation in enumerate(rotations):\n",
    "            rotated_output_img = flip.rotate(rotation)\n",
    "            \n",
    "            legend = img_flip_labels[id] + \" \" + str(rotations[id_rot])\n",
    "            legends.append(legend)\n",
    "            augmented_images.append(rotated_output_img)\n",
    "            \n",
    "\n",
    "    fig=plt.figure(figsize=(20, 10))\n",
    "    rows = 4\n",
    "    columns = 4\n",
    "    \n",
    "    for i, img in enumerate(augmented_images):\n",
    "        \n",
    "        ax1 = fig.add_subplot(rows, columns, (i+1))\n",
    "        ax1.title.set_text(legends[i])\n",
    "\n",
    "        # Turn off tick labels\n",
    "        ax1.set_yticklabels([])\n",
    "        ax1.set_xticklabels([])\n",
    "                   \n",
    "        plt.imshow(img)\n",
    "    \n",
    "    plt.show()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 357940 characters]\n",
      "text/plain": [
       "<Figure size 1440x720 with 8 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_augmented_images(zones_appart[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "thesis-venv",
   "language": "python",
   "name": "thesis-venv"
  },
  "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": 2
}
