{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "08a4e34f-e844-43a8-acd1-3cf1d5cd2ef9",
   "metadata": {},
   "source": [
    "# Create Transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bb293515-f120-430e-9ff9-18fff322fbbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import numpy as np\n",
    "import natsort"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b0fc1f2-2566-41a8-9c59-69ec129c5492",
   "metadata": {},
   "source": [
    "### Intrinsic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "231bd004-1e82-4c29-bbd7-78a84d8e5012",
   "metadata": {},
   "outputs": [],
   "source": [
    "data={}\n",
    "\n",
    "# iPhone 15 pro\n",
    "data['fl_x'] = 1342\n",
    "data['fl_y'] = 1342\n",
    "data['k1'] = 0.14\n",
    "data['k2'] = -0.353\n",
    "data['p1'] = 0.0\n",
    "data['p2'] = -0.001\n",
    "data['cx'] = 959\n",
    "data['cy'] = 721\n",
    "data['w'] = 1920\n",
    "data['h'] = 1440\n",
    "data['camera_model'] = 'OPENCV'\n",
    "data['frames'] = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "119e01fa-610b-4d5b-9bda-de20a7025743",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3421cd20-cada-41b2-8523-d3af52ef9eb5",
   "metadata": {},
   "source": [
    "### Extrinsic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the folder path where images are stored and the path to the .txt file containing transform matrices\n",
    "folder_path = '../dataset/siai_room/images/'  # Update this to your images folder path\n",
    "matrix_file_path = '../dataset/siai_room/computeGeolocations_fused.txt'  # Update this to your matrix .txt file path\n",
    "\n",
    "\n",
    "# List all image files and sort them naturally\n",
    "image_files = [f for f in os.listdir(folder_path) if f.endswith(('.jpg', '.png'))]\n",
    "image_files_sorted = natsort.natsorted(image_files)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c737d62c-af89-4b6f-8b13-c94fd29bf29e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read the transform matrices from the .txt file\n",
    "matrices = []\n",
    "with open(matrix_file_path, 'r') as file:\n",
    "    block = []\n",
    "    for line in file:\n",
    "        if line.strip() == '':\n",
    "            if block:\n",
    "                matrices.append(np.vstack([np.array(block), np.array([0, 0, 0, 1])]))\n",
    "                block = []\n",
    "        else:\n",
    "            block.append(list(map(float, line.strip().split(', '))))\n",
    "    if block:  # Add the last block if file doesn't end with a newline\n",
    "        matrices.append(np.vstack([np.array(block), np.array([0, 0, 0, 1])]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0bc02247-436d-40b7-90cb-1b6cfeff3eea",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Combine the image file paths with their corresponding transform matrix\n",
    "for idx, (image_name, matrix) in enumerate(zip(image_files_sorted, matrices)):\n",
    "    # Image_000001.jpg\n",
    "    im_number = int((image_name.split('.')[0]).split('_')[-1])\n",
    "    frame_data = {\n",
    "        \"file_path\": './images/'+image_name,\n",
    "        \"transform_matrix\": matrix.tolist(),\n",
    "        \"colmap_im_id\": im_number\n",
    "    }\n",
    "    data[\"frames\"].append(frame_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    \n",
    "data[\"applied_transform\"] = [\n",
    "                                [\n",
    "                                    0.0,\n",
    "                                    1.0,\n",
    "                                    0.0,\n",
    "                                    0.0\n",
    "                                ],\n",
    "                                [\n",
    "                                    1.0,\n",
    "                                    0.0,\n",
    "                                    0.0,\n",
    "                                    0.0\n",
    "                                ],\n",
    "                                [\n",
    "                                    -0.0,\n",
    "                                    -0.0,\n",
    "                                    -1.0,\n",
    "                                    -0.0\n",
    "                                ]\n",
    "                            ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the path for the output JSON file\n",
    "output_json_path = '../dataset/siai_room/transforms.json'  # Update this to your desired output file path\n",
    "\n",
    "# Write the `data` dictionary to the specified JSON file\n",
    "with open(output_json_path, 'w') as json_file:\n",
    "    json.dump(data, json_file, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
