{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"## First look at the data by drawing bounding boxes","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport os\nimport cv2\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\nfolder_path = \"/kaggle/input/global-wheat-detection/\"\ncsv_path = os.path.join(folder_path,\"train.csv\")\nIMAGE_PATH = os.path.join(folder_path,\"train\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Load csv","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"csv_df = pd.read_csv(csv_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"csv_df.head(10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Convert bbox column to list of floats","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import ast\ncsv_df['bbox']= csv_df['bbox'].apply(ast.literal_eval)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Group by image id to get a singel item for each image. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"combined_df = csv_df.groupby('image_id', as_index=False)['bbox','source'].aggregate(lambda col: col.tolist())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"combined_df.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"csv_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"combined_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"combined_df['source'].apply(lambda x: len(set(x))).unique()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Get unique categories","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"csv_df['source'].unique()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As there are seven categories , create seven colors for drawing bounding boxes","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"seven_colors = [(255, 0, 0), (255, 127, 0), (255, 255, 0), (0, 255, 0), (0, 0, 255), (46, 43, 95), (139, 0, 255)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"color_map = dict(zip(csv_df['source'].unique(),seven_colors ))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_record(row_num):\n    row = combined_df.iloc[row_num].to_dict()\n    category = row['source'][0]\n    print(\"Category:\",category, \"\\nBoxes:\", len(row['bbox']))\n    image = cv2.imread(os.path.join(IMAGE_PATH,f\"{row['image_id']}.jpg\"), -1)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n    image /= 255.0\n    for box in row['bbox']:\n        top_left = (int(box[0]), int(box[1]))\n        bottom_right = (int(box[0]+box[2]), int(box[1]+box[3]))\n        cv2.rectangle(img=image,pt1=top_left, pt2=bottom_right, color=color_map[category], thickness=2)\n    plt.figure(figsize = (10, 10))\n    plt.imshow(image)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_record(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_record(178)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# classification_df = combined_df[['image_id','source']]\n# classification_df['source'] = classification_df['source'].apply(lambda x:x[0])\n# classification_df.to_csv(f\"/kaggle/working/classification_data.csv\", index=False)\n","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}