{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":19989,"databundleVersionId":1160143,"sourceType":"competition"},{"sourceId":175398870,"sourceType":"kernelVersion"},{"sourceId":177044926,"sourceType":"kernelVersion"}],"dockerImageVersionId":30698,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%%capture\n!pip install --no-index -f /kaggle/input/download-ultralytics /kaggle/input/download-ultralytics/ultralytics-8.2.7-py3-none-any.whl\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T12:12:10.703546Z","iopub.execute_input":"2024-05-12T12:12:10.703951Z","iopub.status.idle":"2024-05-12T12:12:24.949940Z","shell.execute_reply.started":"2024-05-12T12:12:10.703921Z","shell.execute_reply":"2024-05-12T12:12:24.948660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nfrom ultralytics import YOLO","metadata":{"execution":{"iopub.status.busy":"2024-05-12T12:12:24.952125Z","iopub.execute_input":"2024-05-12T12:12:24.952483Z","iopub.status.idle":"2024-05-12T12:12:29.446652Z","shell.execute_reply.started":"2024-05-12T12:12:24.952452Z","shell.execute_reply":"2024-05-12T12:12:29.445668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO('/kaggle/input/yolov8-model-gwd/runs/detect/train/weights/best.pt')\nresults = model('../input/global-wheat-detection/test')","metadata":{"execution":{"iopub.status.busy":"2024-05-12T12:12:29.447987Z","iopub.execute_input":"2024-05-12T12:12:29.448463Z","iopub.status.idle":"2024-05-12T12:12:35.389446Z","shell.execute_reply.started":"2024-05-12T12:12:29.448434Z","shell.execute_reply":"2024-05-12T12:12:35.388407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_len = len(results)\nids = np.empty(res_len, dtype='object')\nstrings = np.empty(res_len, dtype='object')\ndetection_threshold = 0.1\n\nfor i, result in enumerate(results):\n    ids[i] = os.path.basename(result.path).split('.')[0]\n    boxes = result.boxes  # Boxes object for bounding box outputs\n    string = []\n    for j, xywh in enumerate(boxes.xywh):\n        conf = round(boxes.conf[j].item(), 4)\n        if conf < detection_threshold:\n            continue\n        string.append(str(conf))\n        xywh[0] = xywh[0] - xywh[2] / 2\n        xywh[1] = xywh[1] - xywh[3] / 2\n        for e in xywh:\n            string.append(str(int(e.item())))\n    strings[i] = ' '.join(string)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T12:12:35.392106Z","iopub.execute_input":"2024-05-12T12:12:35.392756Z","iopub.status.idle":"2024-05-12T12:12:35.423244Z","shell.execute_reply.started":"2024-05-12T12:12:35.392719Z","shell.execute_reply":"2024-05-12T12:12:35.422254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'image_id': ids, 'PredictionString': strings },  columns=['image_id', 'PredictionString'])\n\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T12:12:35.424638Z","iopub.execute_input":"2024-05-12T12:12:35.425539Z","iopub.status.idle":"2024-05-12T12:12:35.437640Z","shell.execute_reply.started":"2024-05-12T12:12:35.425502Z","shell.execute_reply":"2024-05-12T12:12:35.436730Z"},"trusted":true},"execution_count":null,"outputs":[]}]}