{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nimport re\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-02T16:46:05.378091Z","iopub.execute_input":"2021-08-02T16:46:05.378652Z","iopub.status.idle":"2021-08-02T16:46:05.631034Z","shell.execute_reply.started":"2021-08-02T16:46:05.378561Z","shell.execute_reply":"2021-08-02T16:46:05.630108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Read the CSV Files","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('../input/global-wheat-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:05.632395Z","iopub.execute_input":"2021-08-02T16:46:05.632719Z","iopub.status.idle":"2021-08-02T16:46:05.957728Z","shell.execute_reply.started":"2021-08-02T16:46:05.632688Z","shell.execute_reply":"2021-08-02T16:46:05.956746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Number of rows in training CSV: {len(train_df)}\")","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:05.959827Z","iopub.execute_input":"2021-08-02T16:46:05.960284Z","iopub.status.idle":"2021-08-02T16:46:05.965883Z","shell.execute_reply.started":"2021-08-02T16:46:05.960242Z","shell.execute_reply":"2021-08-02T16:46:05.964758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:05.967930Z","iopub.execute_input":"2021-08-02T16:46:05.968371Z","iopub.status.idle":"2021-08-02T16:46:06.000887Z","shell.execute_reply.started":"2021-08-02T16:46:05.968274Z","shell.execute_reply":"2021-08-02T16:46:05.999884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(train_df))\ntrain_df['image_id'][1]","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:06.002556Z","iopub.execute_input":"2021-08-02T16:46:06.002999Z","iopub.status.idle":"2021-08-02T16:46:06.012547Z","shell.execute_reply.started":"2021-08-02T16:46:06.002956Z","shell.execute_reply":"2021-08-02T16:46:06.011394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df['bbox'][0])","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:06.014009Z","iopub.execute_input":"2021-08-02T16:46:06.014410Z","iopub.status.idle":"2021-08-02T16:46:06.025184Z","shell.execute_reply.started":"2021-08-02T16:46:06.014379Z","shell.execute_reply":"2021-08-02T16:46:06.024236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def expand_bbox(x):\n    \"\"\"\n    Function to separate the `[834.0, 222.0, 56.0, 36.0` string to\n    separate `['834.0' '222.0' '56.0' '36.0']`.\n    \"\"\"\n    r = np.array(re.findall(\"([0-9]+[.]?[0-9]*)\", x))\n    if len(r) == 0:\n        r = [-1, -1, -1, -1]\n    return r\n\nbox = expand_bbox(train_df['bbox'][0])\nprint(box)\nbox_0 = box[0].astype(np.float)\nprint(box_0)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:06.026470Z","iopub.execute_input":"2021-08-02T16:46:06.026789Z","iopub.status.idle":"2021-08-02T16:46:06.038270Z","shell.execute_reply.started":"2021-08-02T16:46:06.026746Z","shell.execute_reply":"2021-08-02T16:46:06.037330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Seperate the X, Y, W, and H for Each BBox","metadata":{}},{"cell_type":"code","source":"print(train_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:06.040142Z","iopub.execute_input":"2021-08-02T16:46:06.040481Z","iopub.status.idle":"2021-08-02T16:46:06.051096Z","shell.execute_reply.started":"2021-08-02T16:46:06.040451Z","shell.execute_reply":"2021-08-02T16:46:06.050072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['x'] = -1\ntrain_df['y'] = -1\ntrain_df['w'] = -1\ntrain_df['h'] = -1","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:06.052752Z","iopub.execute_input":"2021-08-02T16:46:06.053332Z","iopub.status.idle":"2021-08-02T16:46:06.067167Z","shell.execute_reply.started":"2021-08-02T16:46:06.053289Z","shell.execute_reply":"2021-08-02T16:46:06.066378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[['x', 'y', 'w', 'h']] = np.stack(train_df['bbox'].apply(lambda x: expand_bbox(x)))\ntrain_df.drop(columns=['bbox'], inplace=True)\ntrain_df['x'] = train_df['x'].astype(np.float)\ntrain_df['y'] = train_df['y'].astype(np.float)\ntrain_df['w'] = train_df['w'].astype(np.float)\ntrain_df['h'] = train_df['h'].astype(np.float)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:06.068556Z","iopub.execute_input":"2021-08-02T16:46:06.069000Z","iopub.status.idle":"2021-08-02T16:46:08.010203Z","shell.execute_reply.started":"2021-08-02T16:46:06.068961Z","shell.execute_reply":"2021-08-02T16:46:08.009309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:08.011628Z","iopub.execute_input":"2021-08-02T16:46:08.012118Z","iopub.status.idle":"2021-08-02T16:46:08.029720Z","shell.execute_reply.started":"2021-08-02T16:46:08.012067Z","shell.execute_reply":"2021-08-02T16:46:08.028687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save the CSV \ntrain_df.to_csv('global_wheat_detection_formatted.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:50:05.098176Z","iopub.execute_input":"2021-08-02T16:50:05.098672Z","iopub.status.idle":"2021-08-02T16:50:06.073734Z","shell.execute_reply.started":"2021-08-02T16:50:05.098641Z","shell.execute_reply":"2021-08-02T16:50:06.072992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Group the CSV File According to the image_id","metadata":{}},{"cell_type":"code","source":"grouped = train_df.groupby(['image_id'])\nprint(len(grouped))\ngrouped.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:08.030811Z","iopub.execute_input":"2021-08-02T16:46:08.031078Z","iopub.status.idle":"2021-08-02T16:46:08.130053Z","shell.execute_reply.started":"2021-08-02T16:46:08.031053Z","shell.execute_reply":"2021-08-02T16:46:08.128956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor key, item in grouped:\n    print(key)\n    print(type(item))\n    print(item)\n    print(len(item))\n    print('\\n')\n    count += 1\n    if count == 2:\n        break\n\nprint(count)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:08.131751Z","iopub.execute_input":"2021-08-02T16:46:08.132155Z","iopub.status.idle":"2021-08-02T16:46:08.173790Z","shell.execute_reply.started":"2021-08-02T16:46:08.132113Z","shell.execute_reply":"2021-08-02T16:46:08.172651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize Images ","metadata":{}},{"cell_type":"code","source":"DIR_TRAIN = '../input/global-wheat-detection/train'","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:08.177029Z","iopub.execute_input":"2021-08-02T16:46:08.177338Z","iopub.status.idle":"2021-08-02T16:46:08.182275Z","shell.execute_reply.started":"2021-08-02T16:46:08.177300Z","shell.execute_reply":"2021-08-02T16:46:08.181113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\n# number of images to show\nviz_thres = 25\nfor key, item in grouped:\n    print(key)\n    print('\\n')\n    image = cv2.imread(f\"{DIR_TRAIN}/{key}.jpg\")\n    cv2.putText(image, f\"{len(item)} wheat heads\", (10, 50), \n                cv2.FONT_HERSHEY_SIMPLEX, 2.0, (255, 255, 0), 5)\n    for i in range(len(item)):\n        x1 = int(item.iloc[i]['x'])\n        y1 = int(item.iloc[i]['y'])\n        x2 = int(x1 + item.iloc[i]['w'])\n        y2 = int(y1 + item.iloc[i]['h'])\n        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 3)\n    plt.figure(figsize=(15, 12))\n    plt.imshow(image[:, :, ::-1])\n    plt.axis('off')\n    plt.show()\n    count += 1\n    if count == viz_thres:\n        break","metadata":{"execution":{"iopub.status.busy":"2021-08-02T16:46:08.183988Z","iopub.execute_input":"2021-08-02T16:46:08.184807Z","iopub.status.idle":"2021-08-02T16:46:21.867082Z","shell.execute_reply.started":"2021-08-02T16:46:08.184693Z","shell.execute_reply":"2021-08-02T16:46:21.866139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}