{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "08f61d63-754f-4c5e-947a-22ae9e54b919",
   "metadata": {},
   "source": [
    "# Submission"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d7ef9694-b611-4498-b945-2c7fc749e248",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "import re\n",
    "import natsort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "26dbaeb3-35c5-402c-a91e-d94c291f96f7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "DIR = './SUBMISSION_final_9'\n",
    "site_names = [f for f in os.listdir(DIR) if os.path.isdir(os.path.join(DIR, f))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a869ca6d-9cb2-4db9-9320-57d2c7f59254",
   "metadata": {},
   "outputs": [],
   "source": [
    "site_names = natsort.natsorted(site_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "81edc820-bc4e-4e89-a2d3-4712f1dec1e1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|                                                     | 0/7 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S001\n",
      "['100' '101' '102' '103' '104' '105' '106' '107' '108' '109' '110' '111'\n",
      " '112' '113' '114' '115' '116']\n",
      "Total detected objects: 17\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 14%|██████▍                                      | 1/7 [00:01<00:08,  1.42s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S003\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 29%|████████████▊                                | 2/7 [00:01<00:04,  1.15it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['100' '101' '102' '103' '104' '105' '106' '107' '108' '109']\n",
      "Total detected objects: 10\n",
      "S009\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 43%|███████████████████▎                         | 3/7 [00:02<00:02,  1.42it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['100' '101' '102' '103' '104' '105' '106' '107' '108' '109']\n",
      "Total detected objects: 10\n",
      "S014\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 57%|█████████████████████████▋                   | 4/7 [00:02<00:01,  1.78it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['100' '101' '102' '103' '104' '105' '106' '107' '108' '109' '110']\n",
      "Total detected objects: 11\n",
      "S018\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 71%|████████████████████████████████▏            | 5/7 [00:03<00:01,  1.77it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['100' '101' '102' '103' '104' '105' '106' '107' '108' '109']\n",
      "Total detected objects: 10\n",
      "S021\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 86%|██████████████████████████████████████▌      | 6/7 [00:03<00:00,  2.04it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['100' '101' '102' '103' '104' '105' '106']\n",
      "Total detected objects: 7\n",
      "S022\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████| 7/7 [00:04<00:00,  1.71it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['100' '101' '102' '103' '104' '105' '106' '107']\n",
      "Total detected objects: 8\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "total_objects_per_site = []\n",
    "all_camera_ids = []\n",
    "for site_name in tqdm(site_names):\n",
    "    print(site_name)\n",
    "    site_path = os.path.join(DIR, site_name)\n",
    "    camera_ids =  [f for f in os.listdir(site_path) if os.path.isdir(os.path.join(site_path, f))]\n",
    "    all_camera_ids.extend(camera_ids)    \n",
    "    object_ids = []\n",
    "    for camera_id in camera_ids:\n",
    "        # print(camera_id)\n",
    "        camera_path = os.path.join(site_path, camera_id)\n",
    "        txt_path = os.path.join(camera_path, 'sorted_associated.txt')\n",
    "        # read txt - check total_number of objects\n",
    "        with open(txt_path, 'r') as f:\n",
    "            for line in f:\n",
    "                line_c = line.strip()\n",
    "                object_id = line_c.split(',')[1]\n",
    "                object_ids.append(object_id)\n",
    "    print(np.unique(object_ids))\n",
    "    print(\"Total detected objects: {}\".format(len(np.unique(object_ids))))\n",
    "    total_objects_per_site.append(len(np.unique(object_ids)))\n",
    "    # break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e7cb52e8-7c05-41d5-910d-d003404db0c8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[17, 10, 10, 11, 10, 7, 8]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_objects_per_site"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f5109e2e-b796-438b-9fe3-8b1b99fbb65f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17\n",
      "27\n",
      "37\n",
      "48\n",
      "58\n",
      "65\n",
      "73\n"
     ]
    }
   ],
   "source": [
    "sum_objects_per_site = []\n",
    "init = 0\n",
    "sum_init = 0\n",
    "for num_object in total_objects_per_site:\n",
    "    sum_init = num_object + sum_init\n",
    "    print(sum_init)\n",
    "    sum_objects_per_site.append(sum_init)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ac22ffc9-1b16-49a2-8279-f7885a6e0a35",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[17, 27, 37, 48, 58, 65, 73]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_objects_per_site"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2482c786-c4a0-4ade-b21e-cc2339305f19",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def convert_id(object_id, site_num, total_objects_per_site):\n",
    "    normalize_id = object_id - 99\n",
    "    # print(normalize_id)\n",
    "    if site_num != 0:\n",
    "        real_id = normalize_id + total_objects_per_site[site_num-1]\n",
    "        return real_id\n",
    "    else:\n",
    "        return normalize_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "24e60a0d-95f9-4faf-a5fc-12149fb68e13",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "0it [00:00, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Site num: 0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "1it [00:04,  4.93s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Site num: 1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2it [00:06,  3.23s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Site num: 2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "3it [00:09,  2.80s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Site num: 3\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "4it [00:10,  2.28s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Site num: 4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5it [00:13,  2.33s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Site num: 5\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "6it [00:14,  2.07s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Site num: 6\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "7it [00:16,  2.37s/it]\n"
     ]
    }
   ],
   "source": [
    "all_camera_ids = []\n",
    "submision_txts = []\n",
    "for site_num, site_name in tqdm(enumerate(site_names)):\n",
    "    print(\"Site num: {}\".format(site_num))\n",
    "    site_path = os.path.join(DIR, site_name)\n",
    "    camera_ids =  [f for f in os.listdir(site_path) if os.path.isdir(os.path.join(site_path, f))]\n",
    "    \n",
    "    camera_ids = natsort.natsorted(camera_ids)\n",
    "    \n",
    "    all_camera_ids.extend(camera_ids)    \n",
    "    object_ids = []\n",
    "    for camera_id in camera_ids:\n",
    "        # print(camera_id)\n",
    "        camera_path = os.path.join(site_path, camera_id)\n",
    "        txt_path = os.path.join(camera_path, 'sorted_associated.txt')\n",
    "        # read txt - check total_number of objects\n",
    "        with open(txt_path, 'r') as f:\n",
    "            for line in f:\n",
    "                line_c = line.strip()\n",
    "                object_id = line_c.split(',')[1]\n",
    "                object_ids.append(object_id)\n",
    "                \n",
    "                if line_c.split(\",\")[7].isdigit():\n",
    "                    glob_x = line_c.split(\",\")[7]\n",
    "                else:\n",
    "                    # print(line_c.split(\",\")[7])\n",
    "                    glob_x = -1\n",
    "                if line_c.split(\",\")[8].isdigit():\n",
    "                    glob_y = line_c.split(\",\")[8]\n",
    "                else: \n",
    "                    glob_y = -1\n",
    "                    \n",
    "                cam_numbers = int(re.findall(r'\\d+', camera_id)[0])\n",
    "                \n",
    "                txt = \"{},{},{},{},{},{},{},{},{}\".format(cam_numbers,\n",
    "                            convert_id(int(object_id), site_num, sum_objects_per_site),\n",
    "                            line_c.split(\",\")[2],line_c.split(\",\")[3],line_c.split(\",\")[4],\n",
    "                            line_c.split(\",\")[5],line_c.split(\",\")[6],\n",
    "                                                          glob_x, glob_y)\n",
    "                # print(txt)\n",
    "                submision_txts.append(txt)\n",
    "                # break\n",
    "        # break\n",
    "    # break\n",
    "    # print(np.unique(object_ids))\n",
    "    # print(\"Total detected objects: {}\".format(len(np.unique(object_ids))))\n",
    "    # break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "120827ed-2566-4f6e-bbda-2bb2fd0981f1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "with open('submission.txt', 'w') as f:\n",
    "\n",
    "    # Loop over each element of the list\n",
    "    for element in submision_txts:\n",
    "        # Write the element to a new line in the file\n",
    "        f.write(element + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9cf2ce6c-0070-43e3-9a1d-09e2a33b3e21",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "convert_id(108, 2, total_objects_per_site)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf51c2c7-4f3f-4935-b6cc-39acd2de7ec5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75828d45-6e52-4723-a9e9-347e1251f43b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da3d98fd-7b57-489e-8853-6d9628c553ec",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch190-test",
   "language": "python",
   "name": "torch190-test"
  },
  "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.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
