{"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":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":127283,"databundleVersionId":15634477}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install av","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-27T12:21:10.898488Z","iopub.execute_input":"2026-03-27T12:21:10.899383Z","iopub.status.idle":"2026-03-27T12:21:14.569377Z","shell.execute_reply.started":"2026-03-27T12:21:10.899351Z","shell.execute_reply":"2026-03-27T12:21:14.568534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import Dataset\nimport pandas as pd\nimport torchvision.io as io\nimport os\n\nclass AccidentDataset(Dataset):\n    def __init__(self, csv_file, root_dir, transform=None):\n        self.data = pd.read_csv(csv_file)\n        self.root_dir = root_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, idx):\n        row = self.data.iloc[idx]\n        video_path = os.path.join(self.root_dir, row['rgb_path'])\n        vframes, _, _ = io.read_video(video_path, pts_unit='sec', output_format=\"TCHW\")\n        start_frame = int(row['accident_frame'])\n        bbox = torch.tensor([\n            row['x1'], row['y1'], row['x2'], row['y2']\n        ], dtype=torch.float32)\n        label = row['type']\n        if self.transform:\n            vframes = self.transform(vframes)\n\n        sample = {\n            'video': vframes,              \n            'target_frame': start_frame,  \n            'target_bbox': bbox,         \n            'target_label': label,\n        }\n\n        return sample","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-27T12:21:14.571342Z","iopub.execute_input":"2026-03-27T12:21:14.571576Z","iopub.status.idle":"2026-03-27T12:21:14.579184Z","shell.execute_reply.started":"2026-03-27T12:21:14.571549Z","shell.execute_reply":"2026-03-27T12:21:14.578402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torchvision import transforms\n\nmy_transform = transforms.Compose([\n    transforms.Resize((224, 224)),\n])\n\nCSV_PATH = '/kaggle/input/competitions/accident/sim_dataset/labels.csv'\nROOT_DIR = '/kaggle/input/competitions/accident/sim_dataset/'\n\ndataset = AccidentDataset(\n    csv_file=CSV_PATH,\n    root_dir=ROOT_DIR,\n    transform=my_transform\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-27T12:21:14.581289Z","iopub.execute_input":"2026-03-27T12:21:14.581604Z","iopub.status.idle":"2026-03-27T12:21:14.614493Z","shell.execute_reply.started":"2026-03-27T12:21:14.581578Z","shell.execute_reply":"2026-03-27T12:21:14.613750Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Example","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\nsample = dataset[0]\nvframes = sample['video']          \nframe_idx = sample['target_frame'] \nbbox = sample['target_bbox']      \n\nframe_to_show = vframes[frame_idx].permute(1, 2, 0).numpy()\n\nfig, ax = plt.subplots(1, figsize=(10, 6))\nax.imshow(frame_to_show)\n\nbbox *= 224 # scale from 0-1 to width x height\nx1, y1, x2, y2 = bbox\nrect = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, \n                         linewidth=1, edgecolor='r', facecolor='none')\nax.add_patch(rect)\n\nplt.title(f\"Type: {sample['target_label']}, frame: {frame_idx}\")\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-27T12:22:43.668510Z","iopub.execute_input":"2026-03-27T12:22:43.669432Z","iopub.status.idle":"2026-03-27T12:22:56.018401Z","shell.execute_reply.started":"2026-03-27T12:22:43.669398Z","shell.execute_reply":"2026-03-27T12:22:56.017553Z"}},"outputs":[],"execution_count":null}]}