{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a08dcc23",
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import os\n",
    "import cv2\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "eaab4f12",
   "metadata": {},
   "outputs": [],
   "source": [
    "vid_path = './train/images/'\n",
    "youtube_path = './train/labels/'\n",
    "youtube_vid_list = [file for file in os.listdir(youtube_path)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c341cdd3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.ipynb_checkpoints',\n",
       " 'kbs_news_daegue - Trim',\n",
       " 'rest',\n",
       " 'water_main_break - Trim',\n",
       " 'water_main_break - Trim2 - Trim',\n",
       " 'ytn_news - Trim - Trim']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "youtube_vid_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "408f5429",
   "metadata": {},
   "outputs": [],
   "source": [
    "del youtube_vid_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e4d214ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['kbs_news_daegue - Trim',\n",
       " 'rest',\n",
       " 'water_main_break - Trim',\n",
       " 'water_main_break - Trim2 - Trim',\n",
       " 'ytn_news - Trim - Trim']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "youtube_vid_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0acb7e70",
   "metadata": {},
   "outputs": [],
   "source": [
    "for folder in youtube_vid_list:\n",
    "    label_files = [file for file in os.listdir(os.path.join(youtube_path,folder)) if file.endswith(\".txt\")]\n",
    "    \n",
    "    os.listdir(os.path.join(vid_path,folder))[1]\n",
    "    \n",
    "    image_file = cv2.imread(os.path.join(vid_path,folder,'0000001.png'))\n",
    "    img_h,img_w,c = image_file.shape\n",
    "    \n",
    "    # convert xywh float to xyxy int\n",
    "    label_array = []\n",
    "    for idx,txt_file in enumerate(label_files):\n",
    "        with open(os.path.join(youtube_path,folder,txt_file)) as f:\n",
    "            lines = f.readlines()\n",
    "        \n",
    "        for line in lines:\n",
    "            temp = line.split(' ')\n",
    "            class_id = temp[0]\n",
    "            c_x = int(float(temp[1])*img_w)\n",
    "            c_y = int(float(temp[2])*img_h)\n",
    "            w = int(float(temp[3])*img_w)\n",
    "            h = int(float(temp[4])*img_h)\n",
    "        \n",
    "            label_array.append([idx,c_x-int(w/2),c_y-int(h/2),c_x+int(w/2),c_y+int(h/2),class_id])\n",
    "    \n",
    "    df = pd.DataFrame(label_array)\n",
    "        \n",
    "    df.to_csv(folder+'.csv', index=False)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02e71fa0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yt38",
   "language": "python",
   "name": "yt38"
  },
  "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
}
