{"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 os\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt\n\n!ls -R /kaggle/input/output/yolov5/runs/detect/exp18","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:58:00.005406Z","iopub.execute_input":"2023-04-19T01:58:00.006918Z","iopub.status.idle":"2023-04-19T01:58:01.417353Z","shell.execute_reply.started":"2023-04-19T01:58:00.006865Z","shell.execute_reply":"2023-04-19T01:58:01.415785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_files = []\nfor (dirpath, dirnames, filenames) in os.walk(\"/kaggle/input/output/yolov5/runs/detect/exp18\"):\n    predicted_files.extend([f for f in filenames if f.endswith('.jpg')])","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:58:01.420604Z","iopub.execute_input":"2023-04-19T01:58:01.420999Z","iopub.status.idle":"2023-04-19T01:58:01.429156Z","shell.execute_reply.started":"2023-04-19T01:58:01.420953Z","shell.execute_reply":"2023-04-19T01:58:01.427759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predicted_files)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:58:01.430756Z","iopub.execute_input":"2023-04-19T01:58:01.431102Z","iopub.status.idle":"2023-04-19T01:58:01.441707Z","shell.execute_reply.started":"2023-04-19T01:58:01.431059Z","shell.execute_reply":"2023-04-19T01:58:01.440187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\npath=\"/kaggle/input/global-wheat-detection/test\"\n\n\nindex=random.choice(predicted_files)\n\noriginal_path=str(path)+\"/\"+str(index)\nimg=cv2.imread(original_path)\nimg=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n\ndirpath=\"/kaggle/input/output/yolov5/runs/detect/exp18\"\n\n\ndetected_path=str(dirpath)+\"/\"+str(index)\ndetected_img=cv2.imread(detected_path)\ndetected_img=cv2.cvtColor(detected_img,cv2.COLOR_BGR2RGB)\n\n\n\n#plotting:\nplt.figure(figsize=(25,17))\nplt.subplot(1,2,1)\nplt.imshow(img)\nplt.title(\"Original\")\nplt.axis(\"off\")\nplt.subplot(1,2,2)\nplt.imshow(detected_img)\nplt.title(\"predicted\")\nplt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:58:01.445603Z","iopub.execute_input":"2023-04-19T01:58:01.446302Z","iopub.status.idle":"2023-04-19T01:58:03.620397Z","shell.execute_reply.started":"2023-04-19T01:58:01.446260Z","shell.execute_reply":"2023-04-19T01:58:03.619234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n# import numpy as np\n# import os, re\n# import glob\n\n\n# width = 1024\n# height = 1024\n# results = []\n\n# result_dir = '/kaggle/input/output/yolov5/runs/detect/exp18/labels'\n# for filename in glob.glob(os.path.join(result_dir, '*.txt')):\n#     prefix = filename.split(\"/\")[-1].split(\".\")[0]\n#     predict_str = \"\"\n\n#     with open(filename, 'r') as fp:\n#         lines = fp.readlines()[::-1]\n#         for line in lines:\n#             entries = np.array(line.strip('\\n').split()).astype(np.float)\n#             bbox = [round((entries[1]*width)-entries[3]/2.0), round((entries[2]*height)-entries[4]/2.0), \n#                     round(entries[3]*width), round(entries[4]*height)]\n#             conf = round(entries[-1], 1)\n#             predict_str = predict_str + str(conf) +\" \" + \" \".join(map(str,bbox)) + \" \" \n\n#     results.append({'image_id':prefix, 'PredictionString':predict_str.rstrip()}) \n        \n# resultFrame = pd.DataFrame(results)\n\nimport pandas as pd\nimport numpy as np\nimport os, re\nimport glob\n\n\nwidth = 1024\nheight = 1024\nresults = []\n\nresult_dir = '/kaggle/input/output/yolov5/runs/detect/exp18/labels'\nfor filename in glob.glob(os.path.join(result_dir,'*.txt')):\n    prefix = filename.split(\"/\")[-1].split(\".\")[0]\n    with open(filename, 'r') as fp:\n        lines = fp.readlines()[::-1]\n        predict_str = \"\"\n        for line in sorted(lines, key=lambda x: float(x.split()[0]), reverse=True):\n            entries = np.array(line.strip('\\n').split()).astype(np.float)\n            bbox = [round((entries[1]*width)-((entries[3]*width)/2.0)), round((entries[2]*height)-((entries[4]*height)/2.0)), \n                    round(entries[3]*width), round(entries[4]*height)]\n            conf = round(entries[-1], 1)\n            predict_str = predict_str + str(conf) +\" \" + \" \".join(map(str,bbox)) + \" \" \n        results.append({'image_id':prefix, 'PredictionString':predict_str.rstrip()}) \n        \nresultFrame = pd.DataFrame(results)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:58:03.622040Z","iopub.execute_input":"2023-04-19T01:58:03.622695Z","iopub.status.idle":"2023-04-19T01:58:03.712544Z","shell.execute_reply.started":"2023-04-19T01:58:03.622645Z","shell.execute_reply":"2023-04-19T01:58:03.711525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# temp = []\n# for filename in glob.glob('/kaggle/input/global-wheat-detection/test/*.jpg'):\n    \n#     prefix = filename.split(\"/\")[-1].split(\".\")[0]\n#     if resultFrame['image_id'].str.contains(prefix).any():\n#         pass\n#     else:\n#         temp.append({'image_id':prefix, 'PredictionString':\"\"})\n        \n# df = pd.DataFrame(temp)\n# resultFrame = resultFrame.append(df)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:58:03.714176Z","iopub.execute_input":"2023-04-19T01:58:03.715000Z","iopub.status.idle":"2023-04-19T01:58:03.719720Z","shell.execute_reply.started":"2023-04-19T01:58:03.714958Z","shell.execute_reply":"2023-04-19T01:58:03.718580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_dir = '/kaggle/working'\nresultFrame.to_csv(submission_dir + \"/submission.csv\", index = False)\nresultFrame.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:58:03.721326Z","iopub.execute_input":"2023-04-19T01:58:03.722530Z","iopub.status.idle":"2023-04-19T01:58:03.762505Z","shell.execute_reply.started":"2023-04-19T01:58:03.722464Z","shell.execute_reply":"2023-04-19T01:58:03.761086Z"},"trusted":true},"execution_count":null,"outputs":[]}]}