diff --git a/231123imageAugmentation.ipynb b/231123imageAugmentation.ipynb new file mode 100644 index 0000000..cbcdb2f --- /dev/null +++ b/231123imageAugmentation.ipynb @@ -0,0 +1,361 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 버스만 있는 이미지" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "\n", + "valid_total_df = pd.read_csv('valid_total_txt_label.csv')\n", + "\n", + "condition1 = valid_total_df['car_cnt'].isnull()\n", + "condition2 = valid_total_df['truck_cnt'].isnull()\n", + "\n", + "busonlyList = list(valid_total_df[(condition1) & (condition2)]['txt_in_valid_folder'].values)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Suwon_CH05_20200720_1830_MON_9m_NH_highway_OW5_sunny_FHD_066.txt',\n", + " 'Suwon_CH05_20200720_1830_MON_9m_NH_highway_OW5_sunny_FHD_085.txt',\n", + " 'Suwon_CH06_20200722_1900_WED_9m_NH_highway_OW5_sunny_FHD_033.txt',\n", + " 'Suwon_CH10_20201213_0752_SUN_9m_NH_highway_OW5_snow_FHD_041.txt',\n", + " 'Suwon_CH10_20201213_0752_SUN_9m_NH_highway_OW5_snow_FHD_082.txt']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import os\n", + "\n", + "valid_filename = []\n", + "for i in busonlyList:\n", + " basename = os.path.basename(i)\n", + " valid_filename.append(basename)\n", + "valid_filename" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from glob import glob\n", + "import cv2\n", + "import numpy as np\n", + "\n", + "\n", + "def glob_files(path, file_type=\"*\"):\n", + " search_string = os.path.join(path, file_type)\n", + " files = glob(search_string)\n", + "\n", + " # print('searching ', path)\n", + " paths = []\n", + " for f in files:\n", + " if os.path.isdir(f):\n", + " sub_paths = glob_files(f + '/')\n", + " paths += sub_paths\n", + " else:\n", + " paths.append(f)\n", + "\n", + " # We sort the images in alphabetical order to match them\n", + " # to the annotation files\n", + " paths.sort()\n", + "\n", + " return paths\n", + "\n", + "\n", + "# IMAGE_SIZE = 600\n", + "\n", + "def load_images(path):\n", + " files = glob_files(path, \"*.png\")\n", + "\n", + " # print(files)\n", + " X_data = []\n", + " for file in files:\n", + " image = cv2.imread(file)\n", + " # print(image.shape)\n", + " # x = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)\n", + "\n", + " X_data.append(image)\n", + " \n", + " return np.array(X_data)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(5, 1920, 1080, 3)\n" + ] + } + ], + "source": [ + "basePath_image = 'highway/valid/image/'\n", + "\n", + "X_test = load_images(basePath_image)\n", + "print(X_test.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "WIDTH = 1080\n", + "HEIGHT = 1920\n", + "\n", + "def load_labels(path):\n", + " files = glob_files(path, \"*.txt\")\n", + "\n", + " Y_data = []\n", + " for file in files:\n", + " with open(file) as f:\n", + " lines = f.readlines()\n", + "\n", + " boxes = []\n", + " for line in lines:\n", + " tokens = line.split()\n", + "\n", + " class_id = int(tokens[0])\n", + " xc = float(tokens[1]) * WIDTH\n", + " yc = float(tokens[2]) * HEIGHT\n", + " width = float(tokens[3]) * WIDTH\n", + " height = float(tokens[4]) * HEIGHT\n", + "\n", + " boxes.append(np.array([class_id, xc, yc, width, height]))\n", + " # print(class_id, xc, yc, width, height)\n", + "\n", + " Y_data.append(np.array(boxes))\n", + " # print(lines)\n", + " return Y_data ## 이미지마다 탐지 대상 개수가 달라서(행 개수가 달라서) 정해진 shape의 넘파이 어레이로 변환할 수 없다! 변환하려면 패딩 등이 필요.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## car, truck, bus 가 모두 포함된 이미지" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
txt_in_valid_foldercar_cnttruck_cntbus_cnt
2highway/valid/label/Suwon_CH01_20200720_1830_M...5.09.01.0
3highway/valid/label/Suwon_CH01_20200720_1830_M...8.07.01.0
5highway/valid/label/Suwon_CH01_20200720_1830_M...9.04.01.0
6highway/valid/label/Suwon_CH01_20200720_1830_M...9.04.01.0
7highway/valid/label/Suwon_CH01_20200720_1830_M...7.04.03.0
...............
9290highway/valid/label/Suwon_CH10_20201213_0752_S...2.01.01.0
9311highway/valid/label/Suwon_CH10_20201213_0752_S...2.02.01.0
9312highway/valid/label/Suwon_CH10_20201213_0752_S...1.01.01.0
9315highway/valid/label/Suwon_CH10_20201213_0752_S...1.01.01.0
9324highway/valid/label/Suwon_CH10_20201213_0752_S...4.01.01.0
\n", + "

1175 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " txt_in_valid_folder car_cnt truck_cnt \\\n", + "2 highway/valid/label/Suwon_CH01_20200720_1830_M... 5.0 9.0 \n", + "3 highway/valid/label/Suwon_CH01_20200720_1830_M... 8.0 7.0 \n", + "5 highway/valid/label/Suwon_CH01_20200720_1830_M... 9.0 4.0 \n", + "6 highway/valid/label/Suwon_CH01_20200720_1830_M... 9.0 4.0 \n", + "7 highway/valid/label/Suwon_CH01_20200720_1830_M... 7.0 4.0 \n", + "... ... ... ... \n", + "9290 highway/valid/label/Suwon_CH10_20201213_0752_S... 2.0 1.0 \n", + "9311 highway/valid/label/Suwon_CH10_20201213_0752_S... 2.0 2.0 \n", + "9312 highway/valid/label/Suwon_CH10_20201213_0752_S... 1.0 1.0 \n", + "9315 highway/valid/label/Suwon_CH10_20201213_0752_S... 1.0 1.0 \n", + "9324 highway/valid/label/Suwon_CH10_20201213_0752_S... 4.0 1.0 \n", + "\n", + " bus_cnt \n", + "2 1.0 \n", + "3 1.0 \n", + "5 1.0 \n", + "6 1.0 \n", + "7 3.0 \n", + "... ... \n", + "9290 1.0 \n", + "9311 1.0 \n", + "9312 1.0 \n", + "9315 1.0 \n", + "9324 1.0 \n", + "\n", + "[1175 rows x 4 columns]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "condition3 = valid_total_df['car_cnt'].notnull()\n", + "condition4 = valid_total_df['truck_cnt'].notnull()\n", + "condition5 = valid_total_df['bus_cnt'].notnull()\n", + "\n", + "valid_notnull_df = valid_total_df[(condition3) & (condition4) & (condition5)]\n", + "valid_notnull_df[valid_notnull_df['car_cnt'] <= 10]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "yolov8", + "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.8.18" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}