{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1679717e",
   "metadata": {},
   "source": [
    "# Fill the missing frames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f9641bf0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import pandas as pd\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fe16059d",
   "metadata": {},
   "outputs": [],
   "source": [
    "VIDEO_NUMBER = \"2\"\n",
    "PROJECT_ROOT = Path().resolve().parent.parent\n",
    "INPUT_VIDEO_PATH = str(\n",
    "    PROJECT_ROOT\n",
    "    / \"data\"\n",
    "    / f\"recording_{VIDEO_NUMBER}\"\n",
    "    / f\"Recording_{VIDEO_NUMBER}.mp4\"\n",
    ")\n",
    "INPUT_CSV_PATH = str(\n",
    "    PROJECT_ROOT\n",
    "    / \"notebook\"\n",
    "    / \"spin\"\n",
    "    / \"intermediate_data\"\n",
    "    / f\"Rotation_data_{VIDEO_NUMBER}.csv\"\n",
    ")\n",
    "OUTPUT_CSV_PATH = str(\n",
    "    PROJECT_ROOT\n",
    "    / \"notebook\"\n",
    "    / \"spin\"\n",
    "    / \"intermediate_data\"\n",
    "    / f\"Rotation_data_{VIDEO_NUMBER}.csv\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8e1af6de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total frames in video: 224\n"
     ]
    }
   ],
   "source": [
    "cap = cv2.VideoCapture(INPUT_VIDEO_PATH)\n",
    "\n",
    "if not cap.isOpened():\n",
    "    raise IOError(\"Error opening video file\")\n",
    "\n",
    "frame_count = 0\n",
    "while cap.isOpened():\n",
    "    ret, _ = cap.read()\n",
    "    if not ret:\n",
    "        break\n",
    "    frame_count += 1\n",
    "\n",
    "print(f\"Total frames in video: {frame_count}\")\n",
    "\n",
    "cap.release()\n",
    "\n",
    "df_coords = pd.read_csv(INPUT_CSV_PATH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "24bb716f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Saved updated CSV with all 224 frames to /home/davic/projects/IACV_project/bowling-analysis/notebook/spin/intermediate_data/Rotation_data_2.csv\n"
     ]
    }
   ],
   "source": [
    "# Initialize with NaNs for x, y, radius\n",
    "full_df = pd.DataFrame(\n",
    "    {\n",
    "        \"frame\": list(range(frame_count)),\n",
    "        \"x\": [None] * frame_count,\n",
    "        \"y\": [None] * frame_count,\n",
    "        \"radius\": [None] * frame_count,\n",
    "        \"x_axis\": [None] * frame_count,\n",
    "        \"y_axis\": [None] * frame_count,\n",
    "        \"z_axis\": [None] * frame_count,\n",
    "        \"angle\": [None] * frame_count,\n",
    "    }\n",
    ")\n",
    "\n",
    "# Set 'frame' as index for easier alignment\n",
    "df_coords.set_index(\"frame\", inplace=True)\n",
    "full_df.set_index(\"frame\", inplace=True)\n",
    "\n",
    "# Update the full_df with existing values from df_coords\n",
    "full_df.update(df_coords)\n",
    "\n",
    "# Reset index to save as CSV\n",
    "full_df.reset_index(inplace=True)\n",
    "\n",
    "# === Step 5: Save to new CSV ===\n",
    "full_df.to_csv(OUTPUT_CSV_PATH, index=False)\n",
    "\n",
    "print(f\"Saved updated CSV with all {frame_count} frames to {OUTPUT_CSV_PATH}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
