{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Resize session images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 1 sessions\n",
      "Session 'GFox_03' at folder: /home/kleinsteuber/vscode/GeneratedSessions_NoBackup/VIELAAS_Spring_Session03-VIELAAS_GFox_03\n",
      "Loaded scans.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from tqdm import tqdm\n",
    "import cv2 as cv\n",
    "\n",
    "from py.Session import Session\n",
    "from py.Dataset import Dataset\n",
    "from py.FileUtils import list_jpegs_recursive\n",
    "\n",
    "DIR = \"/home/kleinsteuber/vscode/GeneratedSessions_NoBackup/\" # '/home/AMMOD_data/camera_traps/BayerWald/Vielkadaver-Projekt/' # dataset directory\n",
    "\n",
    "ds = Dataset(DIR)\n",
    "session = ds.create_session(\"gfox_03\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def copy_session(session: Session, target_folder_name: str, scale=None, size=None, truncate_y=(0, 0)):\n",
    "    new_folder = os.path.join(target_folder_name, os.path.basename(session.folder))\n",
    "    for file in tqdm(list_jpegs_recursive(session.folder)):\n",
    "        new_file = os.path.join(new_folder, os.path.relpath(file, session.folder))\n",
    "        os.makedirs(os.path.dirname(new_file), exist_ok=True)\n",
    "        # print(f\"Copying {file} to {new_file}\")\n",
    "        img = cv.imread(file)\n",
    "        img = img[truncate_y[0]:(-truncate_y[1])]\n",
    "        # scale\n",
    "        if scale is not None and scale < 1:\n",
    "            img = cv.resize(img, None, fx=scale, fy=scale, interpolation=cv.INTER_LINEAR)\n",
    "        elif size is not None:\n",
    "            assert type(size) == tuple\n",
    "            img = cv.resize(img, size, interpolation=cv.INTER_LINEAR)\n",
    "        cv.imwrite(new_file, img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/kleinsteuber/vscode/GeneratedSessions_NoBackup/VIELAAS_Spring_Session03-VIELAAS_GFox_03/**/*.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5701/5701 [08:15<00:00, 11.51it/s]\n"
     ]
    }
   ],
   "source": [
    "copy_session(session, \"ResizedSessions_NoBackup\", scale=0.3, truncate_y=(40, 40))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/kleinsteuber/vscode/GeneratedSessions_NoBackup/VIELAAS_Spring_Session03-VIELAAS_GFox_03/**/*.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5701/5701 [07:17<00:00, 13.04it/s]\n"
     ]
    }
   ],
   "source": [
    "copy_session(session, \"ResizedSessions256_NoBackup\", size=(256, 256), truncate_y=(40, 40))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.9 64-bit",
   "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.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
