{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":19205,"databundleVersionId":1011244}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -q ultralytics\n\nimport os\nimport pandas as pd\nfrom ultralytics import YOLO\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-04-04T08:54:20.017275Z","iopub.execute_input":"2026-04-04T08:54:20.018299Z","iopub.status.idle":"2026-04-04T08:54:24.282198Z","shell.execute_reply.started":"2026-04-04T08:54:20.018251Z","shell.execute_reply":"2026-04-04T08:54:24.281021Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_PATH = \"/kaggle/input/competitions/open-images-object-detection-rvc-2020\"\nTEST_PATH = os.path.join(DATA_PATH, \"test\")\n\nimage_files = os.listdir(TEST_PATH)\n\nprint(\"Total images:\", len(image_files))\nprint(\"Sample:\", image_files[:5])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-04T08:54:24.284680Z","iopub.execute_input":"2026-04-04T08:54:24.285084Z","iopub.status.idle":"2026-04-04T08:54:24.999637Z","shell.execute_reply.started":"2026-04-04T08:54:24.285043Z","shell.execute_reply":"2026-04-04T08:54:24.998862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = YOLO(\"yolov8n.pt\")  # fast version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-04T08:54:25.000705Z","iopub.execute_input":"2026-04-04T08:54:25.001062Z","iopub.status.idle":"2026-04-04T08:54:25.062263Z","shell.execute_reply.started":"2026-04-04T08:54:25.001030Z","shell.execute_reply":"2026-04-04T08:54:25.061450Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_map = {\n    0: \"/m/01g317\",   # person\n    1: \"/m/0199g\",    # bicycle\n    2: \"/m/0k4j\",     # car\n    3: \"/m/04_sv\",    # motorcycle\n    5: \"/m/0bt9lr\",   # bus\n    7: \"/m/01bjv\",    # truck\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-04T08:54:25.063973Z","iopub.execute_input":"2026-04-04T08:54:25.064273Z","iopub.status.idle":"2026-04-04T08:54:25.068622Z","shell.execute_reply.started":"2026-04-04T08:54:25.064242Z","shell.execute_reply":"2026-04-04T08:54:25.067820Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = pd.read_csv(DATA_PATH + \"/sample_submission.csv\")\nsample[\"PredictionString\"] = \"\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-04T08:54:25.069709Z","iopub.execute_input":"2026-04-04T08:54:25.070042Z","iopub.status.idle":"2026-04-04T08:54:25.299815Z","shell.execute_reply.started":"2026-04-04T08:54:25.070010Z","shell.execute_reply":"2026-04-04T08:54:25.298968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for img_name in tqdm(image_files[:500]):  # keep 500 for now\n    \n    img_path = os.path.join(TEST_PATH, img_name)\n    results = model(img_path, verbose=False)\n\n    preds = []\n\n    for r in results:\n        h, w = r.orig_shape\n\n        for box in r.boxes:\n            cls = int(box.cls[0])\n            conf = float(box.conf[0])\n\n            if conf < 0.3:\n                continue\n\n            x1, y1, x2, y2 = box.xyxy[0]\n\n            x1, x2 = x1 / w, x2 / w\n            y1, y2 = y1 / h, y2 / h\n\n            label = label_map.get(cls, None)\n            if label is None:\n                continue\n\n            preds.append(f\"{label} {conf} {x1} {y1} {x2} {y2}\")\n\n    img_id = img_name.split('.')[0]\n    sample.loc[sample[\"ImageId\"] == img_id, \"PredictionString\"] = \" \".join(preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-04T08:54:25.301176Z","iopub.execute_input":"2026-04-04T08:54:25.301491Z","iopub.status.idle":"2026-04-04T08:55:42.463443Z","shell.execute_reply.started":"2026-04-04T08:54:25.301455Z","shell.execute_reply":"2026-04-04T08:55:42.462577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample.to_csv(\"submission.csv\", index=False)\nprint(\"✅ submission.csv created\")\nsample.head()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-04T08:55:42.464787Z","iopub.execute_input":"2026-04-04T08:55:42.465613Z","iopub.status.idle":"2026-04-04T08:55:42.574466Z","shell.execute_reply.started":"2026-04-04T08:55:42.465577Z","shell.execute_reply":"2026-04-04T08:55:42.573616Z"}},"outputs":[],"execution_count":null}]}