{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":92399,"databundleVersionId":11038207,"sourceType":"competition"}],"dockerImageVersionId":30919,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"\n# 🚗 Optical Flow-Based Data Preprocessing for Nexar Dataset\n\n## 📌 Objective\nThis notebook is designed to prepare training data for motion-based modeling using Optical Flow techniques from the Nexar driving dataset. Optical flow helps track the motion of objects across video frames, making it valuable for applications like autonomous driving, object tracking, and motion prediction.\n\n## 🧠 Techniques Used\n\n### 1. Optical Flow (Farneback Method)\n- **Description**: A dense method that computes flow for all pixels using polynomial expansion.\n- **Benefits**:\n  - Provides rich pixel-level motion information.\n  - Useful for detecting object movement in driving scenes.\n\n### 2. Data Preprocessing\n- Frame extraction from videos\n- Optical flow vector computation\n- Saving of preprocessed arrays for efficient future training\n\n## 📈 Use Cases\n- Driver behavior modeling\n- Lane change prediction\n- Traffic pattern analysis\n\n---\n\n","metadata":{"_kg_hide-output":true}},{"cell_type":"code","source":"pip install --upgrade nbconvert traitlets\n","metadata":{"trusted":true,"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"###   **Data Loading Part**","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\n\n# Load the training and test data\ndf = pd.read_csv('/kaggle/input/nexar-collision-prediction/train.csv')\ndf_test = pd.read_csv('/kaggle/input/nexar-collision-prediction/test.csv')\n\n# Pad the 'id' column with leading zeros to ensure 5-digit IDs\ndf[\"id\"] = df[\"id\"].astype(str).str.zfill(5)\ndf_test[\"id\"] = df_test[\"id\"].astype(str).str.zfill(5)\n\n# Define directories containing train and test videos\ntrain_dir = \"/kaggle/input/nexar-collision-prediction/train/\"\ntest_dir = \"/kaggle/input/nexar-collision-prediction/test/\"\n\n# Create video filenames by appending \".mp4\" to the ID\ndf[\"train_videos\"] = df[\"id\"] + \".mp4\"\ndf_test[\"test_videos\"] = df_test[\"id\"] + \".mp4\"\n\n# Display sample IDs\nprint(f\"Sample Train IDs:\\n{df['id'].head()}\")\nprint(f\"Sample Test IDs:\\n{df_test['id'].head()}\")\n\n# Display the total number of videos\nprint(f\"Total Train Videos: {len(df['train_videos'])}\")\nprint(f\"Total Test Videos: {len(df_test['test_videos'])}\")\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Optical Flow Calculation (GPU Accelerated)**\n","metadata":{}},{"cell_type":"code","source":"import cv2\nfrom multiprocessing import Pool, cpu_count\n\n# Optical Flow calculation function (CPU version)\ndef compute_optical_flow_gpu(video_info):  \n    video_name, video_dir, alert_time, event_time = video_info\n    video_path = os.path.join(video_dir, video_name)\n\n    cap = cv2.VideoCapture(video_path)\n    if not cap.isOpened():\n        print(f\"❌ Video couldnt opened: {video_path}\")\n        return None  \n\n    fps = cap.get(cv2.CAP_PROP_FPS) or 30  # Default FPS 30\n    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n\n    alert_frame = int(alert_time * fps) if not pd.isna(alert_time) else 0\n    event_frame = int(event_time * fps) if not pd.isna(event_time) else 0\n\n    flow_features = []\n\n    ret, prev_frame = cap.read()\n    if not ret:\n        cap.release()\n        return None\n\n    prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)\n\n    for frame_count in range(1, total_frames):\n        ret, frame = cap.read()\n        if not ret:\n            break  \n        \n        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n\n        # CPU Optical Flow Calculation\n        flow = cv2.calcOpticalFlowFarneback(\n            prev_gray, gray, None,\n            pyr_scale=0.5,\n            levels=3,\n            winsize=15,\n            iterations=3,\n            poly_n=5,\n            poly_sigma=1.2,\n            flags=0\n        )\n\n        magnitude, angle = cv2.cartToPolar(flow[..., 0], flow[..., 1])\n        mean_magnitude = np.mean(magnitude)\n        max_magnitude = np.max(magnitude)\n        std_magnitude = np.std(magnitude)\n\n        flow_features.append([video_name, frame_count, mean_magnitude, max_magnitude, std_magnitude])\n\n        prev_gray = gray\n\n    cap.release()\n\n    df = pd.DataFrame(flow_features, columns=[\"video_id\", \"frame\", \"mean_magnitude\", \"max_magnitude\", \"std_magnitude\"])\n    return df\n\n# Process all videos and compute Optical Flow (CPU version)\ndef process_videos_cpu(df, train_dir):  \n    video_list = df[['train_videos', 'time_of_alert', 'time_of_event']].values.tolist()\n    video_list = [(video_name, train_dir, alert_time, event_time) for video_name, alert_time, event_time in video_list]\n\n    print(f\"🔄 {len(video_list)} videos will be processed with CPU...\")\n\n    with Pool(cpu_count()) as pool:\n        results = list(pool.map(compute_optical_flow_gpu, video_list))\n\n    all_results = pd.concat([df for df in results if df is not None], ignore_index=True)\n    \n    return all_results\n\n# Run CPU Optical Flow\noptical_flow_results = process_videos_cpu(df.iloc[0:2], train_dir)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-23T11:31:22.476490Z","iopub.execute_input":"2025-03-23T11:31:22.476672Z","iopub.status.idle":"2025-03-23T11:31:23.060407Z","shell.execute_reply.started":"2025-03-23T11:31:22.476653Z","shell.execute_reply":"2025-03-23T11:31:23.058955Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save results to CSV \noptical_flow_results.to_csv(\"optical_flow_results.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-23T11:31:23.061128Z","iopub.status.idle":"2025-03-23T11:31:23.061551Z","shell.execute_reply":"2025-03-23T11:31:23.061366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(optical_flow_results.head())","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}