{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Neural network takes data in csv form, as pair rgb, depth per line.\n",
    "\n",
    "So we prepare data in required format and csv for them."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### converting files from tiff to jpg (RGB) and png (depth) and moving them to new folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import glob\n",
    "import os\n",
    "from configparser import ConfigParser\n",
    "import progressbar\n",
    "from joblib import Parallel, delayed\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "import csv\n",
    "from visualization import load_depth\n",
    "from gta_math import ndc_to_real, construct_proj_matrix, depth_to_integer_range, depth_crop_and_positive\n",
    "from math import tan, atan, radians, degrees\n",
    "import random\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_base_name(name):\n",
    "    return os.path.basename(os.path.splitext(name)[0])\n",
    "\n",
    "\n",
    "def convert_tiff(in_directory, out_directory, out_name, name, out_format, is_depth=False, transform=None):\n",
    "    if 'pbar' in globals() and 'counter' in globals():\n",
    "        global counter\n",
    "        counter += 1\n",
    "        pbar.update(counter)\n",
    "\n",
    "    outfile = os.path.join(out_directory, \"{}.{}\".format(out_name, out_format))\n",
    "    if os.path.exists(outfile):\n",
    "        return\n",
    "\n",
    "    try:\n",
    "        infile = os.path.join(in_directory, name)\n",
    "        # print('infile: ', infile)\n",
    "        # print('is_depth: ', is_depth)\n",
    "        # print('outfile: ', outfile)\n",
    "        if is_depth:\n",
    "            depth = load_depth(name)\n",
    "            if transform is None:\n",
    "                depth = depth * 255.0\n",
    "                im = Image.fromarray(depth.astype(np.uint8), mode=\"L\")\n",
    "            else:\n",
    "                H = transform['H']\n",
    "                W = transform['W']\n",
    "                fov = transform['fov']\n",
    "                near_clip = transform['near_clip']\n",
    "                depth = ndc_to_real(depth, construct_proj_matrix(H, W, fov, near_clip))\n",
    "                depth = depth_crop_and_positive(depth)\n",
    "                depth = depth_to_integer_range(depth)\n",
    "                im = Image.fromarray(depth, mode=\"I\")  # I is mode for int32 single valued image, but using only uint16 which fits into png\n",
    "        else:\n",
    "            im = Image.open(infile)\n",
    "            im = im.convert(mode=\"RGB\")\n",
    "        # print(\"Generating new format for {} to new file {}\".format(name, out_name))\n",
    "        im.save(outfile)\n",
    "    except OSError:\n",
    "        # print(\"Skipping invalid file {}\".format(name))\n",
    "        return\n",
    "\n",
    "\n",
    "def tiff_to_dataset(in_directory, out_directory, name, rgb_format, depth_format):\n",
    "    convert_tiff(in_directory, out_directory, get_base_name(name), get_base_name(name)+'.tiff', rgb_format, is_depth=False)\n",
    "    convert_tiff(in_directory, out_directory, get_base_name(name), get_base_name(name), depth_format, is_depth=True)\n",
    "\n",
    "\n",
    "def tiff_to_dataset_with_transform(in_directory, out_directory, name, rgb_format, depth_format, transform):\n",
    "    convert_tiff(in_directory, out_directory, get_base_name(name), get_base_name(name)+'.tiff', rgb_format, is_depth=False)\n",
    "    convert_tiff(in_directory, out_directory, get_base_name(name), get_base_name(name), depth_format, is_depth=True, transform=transform)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ini_file = \"gta-postprocessing.local.ini\"\n",
    "\n",
    "CONFIG = ConfigParser()\n",
    "CONFIG.read(ini_file)\n",
    "in_directory = CONFIG[\"Images\"][\"Tiff\"]\n",
    "out_directory = CONFIG[\"Images\"][\"MlDataset\"]\n",
    "out_root_directory = os.path.join(out_directory, '..')\n",
    "\n",
    "rgb_format = 'jpg'\n",
    "depth_format = 'png'\n",
    "\n",
    "import visualization\n",
    "visualization.multi_page = False\n",
    "visualization.ini_file = ini_file\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for raw files, in NDC, without db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "workers = 10\n",
    "pattern = '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]--[0-9][0-9]-[0-9][0-9]-[0-9][0-9]--[0-9][0-9][0-9].tiff'\n",
    "\n",
    "widgets = [progressbar.Percentage(), ' ', progressbar.Counter(), ' ', progressbar.Bar(), ' ',\n",
    "           progressbar.FileTransferSpeed()]\n",
    "\n",
    "files = glob.glob(os.path.join(in_directory, pattern))\n",
    "\n",
    "pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(files) * 2).start()\n",
    "counter = 0\n",
    "\n",
    "Parallel(n_jobs=workers, backend='threading')(delayed(tiff_to_dataset)(in_directory, out_directory, name, rgb_format, depth_format)\n",
    "                         for name in files)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for transformation from NDC to world, with db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 98% 65770 |##################################################### |  11.6 KiB/s"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " None,\n",
       " ...]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "workers = 10\n",
    "\n",
    "run_id = 6\n",
    "\n",
    "widgets = [progressbar.Percentage(), ' ', progressbar.Counter(), ' ', progressbar.Bar(), ' ',\n",
    "           progressbar.FileTransferSpeed()]\n",
    "\n",
    "conn = visualization.get_connection()\n",
    "cur = conn.cursor()\n",
    "cur.execute(\"\"\"SELECT snapshot_id, imagepath, camera_fov, width, height, cam_near_clip \\\n",
    "    FROM snapshots \\\n",
    "    WHERE run_id = {} \\\n",
    "    ORDER BY snapshot_id DESC \\\n",
    "    \"\"\".format(run_id))\n",
    "\n",
    "results = [dict(res) for res in cur]\n",
    "#files = [os.path.join(in_directory, i['imagepath']) for i in results]\n",
    "records = [(os.path.join(in_directory, i['imagepath']), \n",
    "            {'H': i['height'], 'W': i['width'], 'fov': i['camera_fov'], 'near_clip': i['cam_near_clip']}) for i in results]\n",
    "\n",
    "\n",
    "pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(records) * 2).start()\n",
    "counter = 0\n",
    "\n",
    "Parallel(n_jobs=workers, backend='threading')(delayed(tiff_to_dataset_with_transform)\n",
    "                                              (in_directory, out_directory, name, rgb_format, depth_format, transform)\n",
    "                         for name, transform in records)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### prepare files with training and testing filenames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "filenames = []\n",
    "for i, (name, _) in enumerate(records):\n",
    "    rgb_name = \"{}.{}\".format(get_base_name(name), rgb_format)\n",
    "    rgb_name = os.path.relpath(os.path.join(out_directory, rgb_name), start=out_root_directory)\n",
    "    rgb_name = rgb_name.replace('\\\\', '/')\n",
    "    depth_name = \"{}.{}\".format(get_base_name(name), depth_format)\n",
    "    depth_name = os.path.relpath(os.path.join(out_directory, depth_name), start=out_root_directory)\n",
    "    depth_name = depth_name.replace('\\\\', '/')\n",
    "    filenames.append([rgb_name, depth_name])\n",
    "\n",
    "    # r je abych nemusel psát zpětná lomítka\n",
    "with open(os.path.join(out_root_directory, 'whole-depth-gta.csv'), mode='w+', newline='') as f:\n",
    "    csv.writer(f).writerows(filenames)\n",
    "\n",
    "random.shuffle(filenames)\n",
    "\n",
    "train_ratio = 0.8\n",
    "train_threshold = int(train_ratio * len(filenames))\n",
    "train_filenames = filenames[:train_threshold]\n",
    "test_filenames = filenames[train_threshold:]\n",
    "\n",
    "with open(os.path.join(out_root_directory, 'train-depth-gta.csv'), mode='w+', newline='') as f:\n",
    "    csv.writer(f).writerows(train_filenames)\n",
    "with open(os.path.join(out_root_directory, 'test-depth-gta.csv'), mode='w+', newline='') as f:\n",
    "    csv.writer(f).writerows(test_filenames)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### trying for one image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "run_id = 6\n",
    "\n",
    "conn = visualization.get_connection()\n",
    "cur = conn.cursor()\n",
    "cur.execute(\"\"\"SELECT snapshot_id, imagepath, camera_fov, width, height, cam_near_clip \\\n",
    "    FROM snapshots \\\n",
    "    WHERE run_id = {} \\\n",
    "    ORDER BY snapshot_id DESC \\\n",
    "    \"\"\".format(run_id))\n",
    "\n",
    "results = [dict(res) for res in cur]\n",
    "#files = [os.path.join(in_directory, i['imagepath']) for i in results]\n",
    "records = [(os.path.join(in_directory, i['imagepath']), \n",
    "            {'H': i['height'], 'W': i['width'], 'fov': i['camera_fov'], 'near_clip': i['cam_near_clip']}) for i in results]\n",
    "\n",
    "name, transform = records[0]\n",
    "tiff_to_dataset_with_transform(in_directory, out_directory, name, rgb_format, depth_format, transform)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "name, transform = records[0]\n",
    "name = get_base_name(name)\n",
    "out_name = get_base_name(name)\n",
    "\n",
    "infile = os.path.join(in_directory, name)\n",
    "# print('infile: ', infile)\n",
    "# print('is_depth: ', is_depth)\n",
    "# print('outfile: ', outfile)\n",
    "depth = load_depth(name)\n",
    "H = transform['H']\n",
    "W = transform['W']\n",
    "fov = transform['fov']\n",
    "near_clip = transform['near_clip']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "start = time.time()\n",
    "depth = ndc_to_real(depth, construct_proj_matrix(H, W, fov, near_clip))\n",
    "end = time.time()\n",
    "print('time to convert coords: ', end - start)\n",
    "depth = depth_crop_and_positive(depth)\n",
    "print('depth calculated')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "out_format = 'png'\n",
    "outfile = os.path.join(out_directory, \"{}.{}\".format(out_name, out_format))\n",
    "\n",
    "# whole conversion\n",
    "import gta_math\n",
    "gta_math.MAXIMUM = np.iinfo(np.uint16).max\n",
    "depth_int = depth_to_integer_range(depth)\n",
    "\n",
    "print(np.min(depth_int))\n",
    "print(np.max(depth_int))\n",
    "print(np.iinfo(np.int16).min)\n",
    "print(np.iinfo(np.int16).max)\n",
    "\n",
    "# import png\n",
    "# f = open(outfile, 'wb')      # binary mode is important\n",
    "# w = png.Writer(depth_int.shape[1], depth_int.shape[0], greyscale=True, bitdepth=16)\n",
    "# w.write(f, depth_int)\n",
    "# f.close()\n",
    "\n",
    "im = Image.fromarray(depth_int, mode=\"I\")  # I is mode for int32 single valued image\n",
    "print('image created')\n",
    "# print(\"Generating new format for {} to new file {}\".format(name, out_name))\n",
    "im.save(outfile)\n",
    "print('file saved')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from gta_math import depth_from_integer_range\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "im = Image.open(outfile)\n",
    "im_arr = np.array(im)\n",
    "print(im_arr.dtype)\n",
    "print(np.min(im_arr))\n",
    "print(np.max(im_arr))\n",
    "\n",
    "im_arr = depth_from_integer_range(im_arr)\n",
    "print(np.min(im_arr))\n",
    "print(np.max(im_arr))\n",
    "\n",
    "im_arr = im_arr.reshape(depth.shape[0], depth.shape[1])\n",
    "%matplotlib inline\n",
    "plt.figure()\n",
    "plt.title('loaded')\n",
    "ii = plt.imshow(im_arr)\n",
    "plt.colorbar(ii)\n",
    "\n",
    "plt.figure()\n",
    "plt.title('orig')\n",
    "ii = plt.imshow(depth)\n",
    "plt.colorbar(ii)\n",
    "\n",
    "plt.figure()\n",
    "plt.title('diff')\n",
    "ii = plt.imshow(depth - im_arr)\n",
    "plt.colorbar(ii)\n",
    "\n",
    "plt.show()\n",
    "print('mean difference: ', np.mean(np.abs(depth - im_arr)))\n",
    "print('max difference: ', np.max(np.abs(depth - im_arr)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### inspecting conversion one step at a time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "THRESHOLD = 1000\n",
    "MAXIMUM = np.iinfo(np.uint16).max\n",
    "# then we treshold the far clip so when we scale to integer range\n",
    "depth_threshold = np.copy(depth)\n",
    "depth_threshold[depth_threshold > THRESHOLD] = THRESHOLD\n",
    "ratio = MAXIMUM / THRESHOLD\n",
    "depth_integer = depth_threshold * ratio\n",
    "depth_integer_rounded = depth_integer.astype(np.int32)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "\n",
    "%matplotlib inline\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.title('loaded image')\n",
    "ii = plt.imshow(im_arr)\n",
    "plt.colorbar(ii)\n",
    "\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.title('depth')\n",
    "ii = plt.imshow(depth)\n",
    "plt.colorbar(ii)\n",
    "\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.title('depth_threshold')\n",
    "ii = plt.imshow(depth_threshold)\n",
    "plt.colorbar(ii)\n",
    "\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.title('depth_integer')\n",
    "ii = plt.imshow(depth_integer)\n",
    "plt.colorbar(ii)\n",
    "\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.title('depth_integer_rounded')\n",
    "ii = plt.imshow(depth_integer_rounded)\n",
    "plt.colorbar(ii)\n",
    "\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.title('depth int')\n",
    "ii = plt.imshow(depth_int)\n",
    "plt.colorbar(ii)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(np.min(im_arr))\n",
    "print(np.max(im_arr))\n",
    "\n",
    "%matplotlib inline\n",
    "plt.figure(figsize=(13, 13))\n",
    "plt.title('depth from file')\n",
    "plt.yscale('log', nonposy='clip')\n",
    "plt.hist(im_arr.reshape(-1, 1), 100)\n",
    "\n",
    "plt.figure(figsize=(13, 13))\n",
    "plt.title('depth')\n",
    "plt.yscale('log', nonposy='clip')\n",
    "plt.hist(depth.reshape(-1, 1), 100)\n",
    "\n",
    "plt.figure(figsize=(13, 13))\n",
    "plt.title('depth_threshold')\n",
    "plt.yscale('log', nonposy='clip')\n",
    "plt.hist(depth_threshold.reshape(-1, 1), 100)\n",
    "\n",
    "plt.figure(figsize=(13, 13))\n",
    "plt.title('depth_integer')\n",
    "plt.yscale('log', nonposy='clip')\n",
    "plt.hist(depth_integer.reshape(-1, 1), 100)\n",
    "\n",
    "plt.figure(figsize=(13, 13))\n",
    "plt.title('depth_integer_rounded')\n",
    "plt.yscale('log', nonposy='clip')\n",
    "plt.hist(depth_integer_rounded.reshape(-1, 1), 100)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
