{"cells":[{"metadata":{},"cell_type":"markdown","source":"## FIRST PART (model training and predicting) is [HERE](https://www.kaggle.com/imakaruamikurah/detect-wheat-train-kerasretinanet-intermediate)","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\n\ndf = pd.read_csv(\"/kaggle/input/wheat-detect/wheat_detect.csv\")\ndf = df.drop(df.columns[0], axis=1)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = []\nfor i in range(len(df)):\n    img = df.iloc[i][\"img\"]\n    prediction = df.iloc[i][\"prediction\"]\n    bbox = df.iloc[i][\"bbox\"]\n    \n    submission.append([img, prediction, bbox])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom matplotlib import rcParams\n\nimg_path = \"../input/detected-wheat-plots\"\n\nrcParams[\"figure.figsize\"] = 15, 15\nfor i in os.listdir(img_path):\n    img = cv2.imread(img_path + \"/\" + i)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.axis('off')\n    plt.imshow(img, interpolation='nearest')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from ast import literal_eval","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"boxes = []\n\nfor vals in submission:\n    cur_id, score, box = vals[0], vals[1], vals[2]\n    \n    box = literal_eval(box)\n    \n    boxes.append([cur_id, f\"{round(score, 4)} {box[0]} {box[1]} {box[2]-box[0]} {box[3]-box[1]}\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_new = pd.DataFrame(boxes)\ndf_new.columns = [\"pic_id\", \"bbox\"]\n\ndf_new","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prev_pid = df_new.iloc[0][\"pic_id\"]\nbbox_string = \"\"\nbbox_array = []\n\nfor row_id in range(len(df_new)):\n    pid = df_new.iloc[row_id][\"pic_id\"]\n    bbox = df_new.iloc[row_id][\"bbox\"]\n    \n    bbox_string += \"  \" + bbox    \n    \n    if prev_pid != pid:\n        bbox_array.append([prev_pid[:-4], bbox_string])\n        prev_pid = pid\n\n        bbox_string = \"\"\nbbox_array.append([prev_pid[:-4], bbox_string])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(bbox_array, columns=[\"image_id\", \"PredictionString\"])\ndf","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Quick look at how the submission file should look like","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df = pd.read_csv(\"/kaggle/input/global-wheat-detection/sample_submission.csv\")\nsub_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(10):\n    sub = sub_df.iloc[i][\"image_id\"]\n    for j in range(10):\n        pre = df.iloc[j][\"image_id\"]\n        if pre == sub:\n            sub_df.iloc[i][\"PredictionString\"] = df.iloc[j][\"PredictionString\"]\n            break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\", index=False)","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}