{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from skimage import io\nimport os\nimport glob\nimport numpy as np\nimport random\nimport pandas as pd\nimport cv2\nimport imageio\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport matplotlib\nimport shutil\nimport git\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists(\"wheat_yolo_train_data\"):\n    os.makedirs(\"wheat_yolo_train_data\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for filename in glob.glob(os.path.join('../input/global-wheat-detection/train', '*.*')):\n    shutil.copy(filename, 'wheat_yolo_train_data')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls wheat_yolo_train_data/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.getcwd()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# path to your dataset\nDATASET_PATH = './global-wheat-detection'\nwheat_data = ['train', 'test']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_path_to_dataset = os.path.join(DATASET_PATH, wheat_data[0], '*')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_path_to_dataset = os.path.dirname(os.path.abspath(os.path.join(DATASET_PATH, wheat_data[0],'*')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(full_path_to_dataset)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#List of categories\n#Only one class i.e. wheat\nwheat = [0]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# CSV File Read","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df = pd.read_csv(os.path.join('../input/global-wheat-detection/train.csv'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df['bbox'] = wheat_df['bbox'].apply(lambda x: x[1:-1].split(\",\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df[['x_min','y_min','box_width','box_height']] = pd.DataFrame(wheat_df.bbox.tolist(), index= wheat_df.index)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df.drop(['bbox'], axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df['x_min'] = wheat_df['x_min'].astype(float)\nwheat_df['y_min'] = wheat_df['y_min'].astype(float)\nwheat_df['box_width'] = wheat_df['box_width'].astype(float)\nwheat_df['box_height'] = wheat_df['box_height'].astype(float)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df['x_max'] = wheat_df['x_min'] + wheat_df['box_width']\nwheat_df['y_max'] = wheat_df['y_min'] + wheat_df['box_height']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df = wheat_df[['image_id', 'width', 'height', 'x_min','y_min', 'x_max', 'y_max', 'box_width', 'box_height','source']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df[\"image_id\"] = wheat_df[\"image_id\"].apply(lambda x: str(x) + \".jpg\")\nwheat_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#drop the columns that are not required\nwheat_df.drop(['width', 'height', 'box_width', 'box_height', 'source'], axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Adding new empty columns to DataFrame to save numbers for YOLO formats\nwheat_df['CategoryID'] = ''\nwheat_df['center x'] = ''\nwheat_df['center y'] = ''\nwheat_df['width'] = ''\nwheat_df['height'] = ''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Getting category's ID according to the class's ID\nwheat_df['CategoryID'] = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Calculating bounding box's center in x and y for all rows\n# Saving results to appropriate columns\nwheat_df['center x'] = (wheat_df['x_max'] + wheat_df['x_min']) / 2\nwheat_df['center y'] = (wheat_df['y_max'] + wheat_df['y_min']) / 2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Calculating bounding box's width and height for all rows\n# Saving results to appropriate columns\nwheat_df['width'] = wheat_df['x_max'] - wheat_df['x_min']\nwheat_df['height'] = wheat_df['y_max'] - wheat_df['y_min']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_img_data = wheat_df.loc[:, ['image_id',\n                'CategoryID',\n                'center x',\n                'center y',\n                'width',\n                'height']].copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_img_data.rename(columns={\"image_id\": \"ImageID\"},inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_img_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir('wheat_yolo_train_data')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.getcwd())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for current_dir, dirs, files in os.walk('.'):\n    # Going through all files\n    for f in files:\n        # Checking if filename ends with '.ppm'\n        if f.endswith('.jpg'):\n            # Reading image and getting its real width and height\n            image_jpg = cv2.imread(f)\n\n            # Slicing from tuple only first two elements\n            h, w = image_jpg.shape[:2]\n\n            # Slicing only name of the file without extension\n            image_name = f[:-4]\n\n            \n            sub_wheat_img = wheat_img_data.loc[wheat_img_data['ImageID'] == f].copy()\n\n            # Normalizing calculated bounding boxes' coordinates\n            # according to the real image width and height\n            sub_wheat_img['center x'] = sub_wheat_img['center x'] / w\n            sub_wheat_img['center y'] = sub_wheat_img['center y'] / h\n            sub_wheat_img['width'] = sub_wheat_img['width'] / w\n            sub_wheat_img['height'] = sub_wheat_img['height'] / h\n\n            resulted_frame = sub_wheat_img.loc[:, ['CategoryID',\n                                           'center x',\n                                           'center y',\n                                           'width',\n                                           'height']].copy()\n\n            # Checking if there is no any annotations for current image\n            if resulted_frame.isnull().values.all():\n                # Skipping this image\n                continue\n\n            \n            path_to_save = image_name + '.txt'\n\n            # Saving resulted Pandas dataFrame into txt file\n            resulted_frame.to_csv(path_to_save, header=False, index=False, sep=' ')\n\n           \n            path_to_save = image_name + '.jpg'\n\n            # Saving image in jpg format by OpenCV function\n            # that uses extension to choose format to save with\n            cv2.imwrite(path_to_save, image_jpg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(glob.glob('*'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Defining list to write paths in\np = []\n\n\nfor current_dir, dirs, files in os.walk('.'):\n    # Going through all files\n    for f in files:\n        # Checking if filename ends with '.jpg'\n        if f.endswith('.jpg'):\n            \n            path_to_save_into_txt_files = f\n\n            p.append(path_to_save_into_txt_files + '\\n')\n\n\n# Slicing first 15% of elements from the list\n# to write into the test.txt file\np_test = p[:int(len(p) * 0.15)]\n\n# Deleting from initial list first 15% of elements\np = p[int(len(p) * 0.15):]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Creating file train.txt and writing 85% of lines in it\nwith open('train.txt', 'w') as train_txt:\n    # Going through all elements of the list\n    for e in p:\n        # Writing current path at the end of the file\n        train_txt.write(e)\n\n# Creating file test.txt and writing 15% of lines in it\nwith open('test.txt', 'w') as test_txt:\n    # Going through all elements of the list\n    for e in p_test:\n        # Writing current path at the end of the file\n        test_txt.write(e)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open(\"classes.txt\", \"w\") as file:\n    file.write(\"wheat\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Defining counter for classes\nc = 0\n\nwith open('classes.names', 'w') as names, \\\n     open('classes.txt', 'r') as txt:\n\n    # Going through all lines in txt file and writing them into names file\n    for line in txt:\n        names.write(line)  # Copying all info from file txt to names\n\n        # Increasing counter\n        c += 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('wheat_data.data', 'w') as data:\n    # Writing needed 5 lines\n    # Number of classes\n    # By using '\\n' we move to the next line\n    data.write('classes = ' + str(c) + '\\n')\n\n    # Location of the train.txt file\n    data.write('train = ' + full_path_to_dataset + '/' + 'train.txt' + '\\n')\n\n    # Location of the test.txt file\n    data.write('valid = ' + full_path_to_dataset + '/' + 'test.txt' + '\\n')\n\n    # Location of the classes.names file\n    data.write('names = ' + full_path_to_dataset + '/' + 'classes.names' + '\\n')\n\n    # Location where to save weights\n    data.write('backup = backup')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('train.txt','r') as fp:\n    Lines = fp.readlines() \n    for line in Lines: \n        print(\"{}\".format(line.strip())) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir(\"..\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.getcwd()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!git clone https://github.com/AlexeyAB/darknet.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cd darknet/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}