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", + " | txt_in_valid_folder | \n", + "car_cnt | \n", + "truck_cnt | \n", + "bus_cnt | \n", + "
---|---|---|---|---|
2 | \n", + "highway/valid/label/Suwon_CH01_20200720_1830_M... | \n", + "5.0 | \n", + "9.0 | \n", + "1.0 | \n", + "
3 | \n", + "highway/valid/label/Suwon_CH01_20200720_1830_M... | \n", + "8.0 | \n", + "7.0 | \n", + "1.0 | \n", + "
5 | \n", + "highway/valid/label/Suwon_CH01_20200720_1830_M... | \n", + "9.0 | \n", + "4.0 | \n", + "1.0 | \n", + "
6 | \n", + "highway/valid/label/Suwon_CH01_20200720_1830_M... | \n", + "9.0 | \n", + "4.0 | \n", + "1.0 | \n", + "
7 | \n", + "highway/valid/label/Suwon_CH01_20200720_1830_M... | \n", + "7.0 | \n", + "4.0 | \n", + "3.0 | \n", + "
... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "
9290 | \n", + "highway/valid/label/Suwon_CH10_20201213_0752_S... | \n", + "2.0 | \n", + "1.0 | \n", + "1.0 | \n", + "
9311 | \n", + "highway/valid/label/Suwon_CH10_20201213_0752_S... | \n", + "2.0 | \n", + "2.0 | \n", + "1.0 | \n", + "
9312 | \n", + "highway/valid/label/Suwon_CH10_20201213_0752_S... | \n", + "1.0 | \n", + "1.0 | \n", + "1.0 | \n", + "
9315 | \n", + "highway/valid/label/Suwon_CH10_20201213_0752_S... | \n", + "1.0 | \n", + "1.0 | \n", + "1.0 | \n", + "
9324 | \n", + "highway/valid/label/Suwon_CH10_20201213_0752_S... | \n", + "4.0 | \n", + "1.0 | \n", + "1.0 | \n", + "
1175 rows × 4 columns
\n", + "