{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7fb6abe9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import os\n",
    "import cv2\n",
    "import pandas as pd\n",
    "import random\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fded2b08",
   "metadata": {},
   "source": [
    "#### for cctv test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "83d68fb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "root_path = './yolo_dataset_cctv_test/train'\n",
    "dest_path = './yolo_dataset_combine_you/train'\n",
    "\n",
    "cctv_vid_list = [file for file in os.listdir(os.path.join(root_path,\"labels\"))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a6cb19c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['kyunbbu_augmented',\n",
       " 'yungtong_night_augmented',\n",
       " 'mannam_waterfall_augmented_20230207_06_05_38',\n",
       " 'mannam_waterfall_augmented',\n",
       " 'yungtong_augmented',\n",
       " 'yungtong_night_augmented_20230207_05_37_26',\n",
       " '.ipynb_checkpoints']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cctv_vid_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "44db1d10",
   "metadata": {},
   "outputs": [],
   "source": [
    "del cctv_vid_list[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eead421f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['yungtong_night_augmented', 'yungtong_night_augmented_20230207_05_37_26']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cctv_vid_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ac9e231c",
   "metadata": {},
   "outputs": [],
   "source": [
    "for folder in cctv_vid_list:\n",
    "    label_files = [file for file in os.listdir(os.path.join(root_path,\"labels\",folder)) if file.endswith(\".txt\")]\n",
    "    \n",
    "    extracted_n = int(len(label_files)/100)\n",
    "\n",
    "    extracted_files = random.sample(list(label_files), extracted_n) #<class 'list'>\n",
    "    \n",
    "    for idx,txt_file in enumerate(extracted_files):\n",
    "        \n",
    "        if not os.path.exists(os.path.join(dest_path,\"labels\",folder)):\n",
    "            os.makedirs(os.path.join(dest_path,\"labels\",folder))\n",
    "            \n",
    "        if not os.path.exists(os.path.join(dest_path,\"images\",folder)):\n",
    "            os.makedirs(os.path.join(dest_path,\"images\",folder))\n",
    "        \n",
    "        # label\n",
    "        shutil.copyfile(os.path.join(root_path,\"labels\",folder,txt_file), \n",
    "                       os.path.join(dest_path,\"labels\",folder,txt_file))\n",
    "        \n",
    "        # label\n",
    "        shutil.copyfile(os.path.join(root_path,\"images\",folder,txt_file.split('.')[0]+'.png'), \n",
    "                       os.path.join(dest_path,\"images\",folder,txt_file.split('.')[0]+'.png'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bf3428b",
   "metadata": {},
   "source": [
    "#### for youtube test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "32696a2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "root_path = './yolo_dataset_youtube_test/train'\n",
    "dest_path = './yolo_dataset_combine_split/train'\n",
    "\n",
    "youtube_vid_list = [file for file in os.listdir(os.path.join(root_path,\"labels\"))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "375e8f98",
   "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": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "youtube_vid_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a3992d4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "del youtube_vid_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8d49903d",
   "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": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "youtube_vid_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "203d5c7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "for folder in youtube_vid_list:\n",
    "    label_files = [file for file in os.listdir(os.path.join(root_path,\"labels\",folder)) if file.endswith(\".txt\")]\n",
    "    \n",
    "    extracted_n = int(len(label_files)/4)\n",
    "\n",
    "    extracted_files = random.sample(list(label_files), extracted_n) #<class 'list'>\n",
    "    \n",
    "    for idx,txt_file in enumerate(extracted_files):\n",
    "        \n",
    "        if not os.path.exists(os.path.join(dest_path,\"labels\",folder)):\n",
    "            os.makedirs(os.path.join(dest_path,\"labels\",folder))\n",
    "            \n",
    "        if not os.path.exists(os.path.join(dest_path,\"images\",folder)):\n",
    "            os.makedirs(os.path.join(dest_path,\"images\",folder))\n",
    "        \n",
    "        # label\n",
    "        shutil.copyfile(os.path.join(root_path,\"labels\",folder,txt_file), \n",
    "                       os.path.join(dest_path,\"labels\",folder,txt_file))\n",
    "        \n",
    "        # label\n",
    "        shutil.copyfile(os.path.join(root_path,\"images\",folder,txt_file.split('.')[0]+'.png'), \n",
    "                       os.path.join(dest_path,\"images\",folder,txt_file.split('.')[0]+'.png'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d5353fc",
   "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
}
