{"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/exp","metadata":{"execution":{"iopub.status.busy":"2023-04-17T08:04:38.904358Z","iopub.execute_input":"2023-04-17T08:04:38.904799Z","iopub.status.idle":"2023-04-17T08:04:40.310025Z","shell.execute_reply.started":"2023-04-17T08:04:38.904761Z","shell.execute_reply":"2023-04-17T08:04:40.308777Z"},"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/exp\"):\n    predicted_files.extend([f for f in filenames if f.endswith('.jpg')])","metadata":{"execution":{"iopub.status.busy":"2023-04-17T08:04:40.313032Z","iopub.execute_input":"2023-04-17T08:04:40.314051Z","iopub.status.idle":"2023-04-17T08:04:40.323896Z","shell.execute_reply.started":"2023-04-17T08:04:40.313991Z","shell.execute_reply":"2023-04-17T08:04:40.322743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predicted_files)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T08:04:40.325562Z","iopub.execute_input":"2023-04-17T08:04:40.326336Z","iopub.status.idle":"2023-04-17T08:04:40.336083Z","shell.execute_reply.started":"2023-04-17T08:04:40.326295Z","shell.execute_reply":"2023-04-17T08:04:40.334898Z"},"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/exp\"\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-17T08:04:40.338619Z","iopub.execute_input":"2023-04-17T08:04:40.339622Z","iopub.status.idle":"2023-04-17T08:04:42.906694Z","shell.execute_reply.started":"2023-04-17T08:04:40.339580Z","shell.execute_reply":"2023-04-17T08:04:42.905407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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/exp/labels'\nfor 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]*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\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-17T08:04:42.907778Z","iopub.execute_input":"2023-04-17T08:04:42.908148Z","iopub.status.idle":"2023-04-17T08:04:43.005589Z","shell.execute_reply.started":"2023-04-17T08:04:42.908109Z","shell.execute_reply":"2023-04-17T08:04:43.004028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = []\nfor 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        \ndf = pd.DataFrame(temp)\nresultFrame = resultFrame.append(df)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T08:04:43.007321Z","iopub.execute_input":"2023-04-17T08:04:43.008733Z","iopub.status.idle":"2023-04-17T08:04:43.036404Z","shell.execute_reply.started":"2023-04-17T08:04:43.008672Z","shell.execute_reply":"2023-04-17T08:04:43.035106Z"},"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-17T08:04:43.038276Z","iopub.execute_input":"2023-04-17T08:04:43.039536Z","iopub.status.idle":"2023-04-17T08:04:43.074978Z","shell.execute_reply.started":"2023-04-17T08:04:43.039483Z","shell.execute_reply":"2023-04-17T08:04:43.073576Z"},"trusted":true},"execution_count":null,"outputs":[]}]}