{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "8b8d7b17-af50-42cd-b531-ef61c49c9e61", "metadata": {}, "outputs": [], "source": [ "# Set the work directory to the imaginaire root.\n", "import os, sys, time\n", "import pathlib\n", "root_dir = pathlib.Path().absolute().parents[2]\n", "os.chdir(root_dir)\n", "print(f\"Root Directory Path: {root_dir}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "2b5b9e2f-841c-4815-92e0-0c76ed46da62", "metadata": {}, "outputs": [], "source": [ "# Import Python libraries.\n", "import numpy as np\n", "import torch\n", "import k3d\n", "import json\n", "from collections import OrderedDict\n", "# Import imaginaire modules.\n", "from projects.nerf.utils import camera, visualize\n", "from third_party.colmap.scripts.python.read_write_model import read_model" ] }, { "cell_type": "code", "execution_count": null, "id": "76033016-2d92-4a5d-9e50-3978553e8df4", "metadata": {}, "outputs": [], "source": [ "# Read the COLMAP data.\n", "colmap_path = \"datasets/iphone/climbingnet_skip12\"\n", "cameras, images, points_3D = read_model(path=f\"{colmap_path}/dense/sparse\", ext=\".bin\")\n", "# Convert camera poses.\n", "images = OrderedDict(sorted(images.items()))\n", "qvecs = torch.from_numpy(np.stack([image.qvec for image in images.values()]))\n", "tvecs = torch.from_numpy(np.stack([image.tvec for image in images.values()]))\n", "Rs = camera.quaternion.q_to_R(qvecs)\n", "poses = torch.cat([Rs, tvecs[..., None]], dim=-1) # [N,3,4]\n", "print(f\"# images: {len(poses)}\")\n", "# Get the sparse 3D points and the colors.\n", "xyzs = torch.from_numpy(np.stack([point.xyz for point in points_3D.values()]))\n", "rgbs = np.stack([point.rgb for point in points_3D.values()])\n", "rgbs = (rgbs[:, 0] * 2**16 + rgbs[:, 1] * 2**8 + rgbs[:, 2]).astype(np.uint32)\n", "print(f\"# points: {len(xyzs)}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "b6cf60ec-fe6a-43ba-9aaf-e3c7afd88208", "metadata": {}, "outputs": [], "source": [ "# Visualize the bounding sphere.\n", "json_fname = f\"{colmap_path}/dense/transforms.json\"\n", "with open(json_fname) as file:\n", " meta = json.load(file)\n", "center = meta[\"sphere_center\"]\n", "radius = meta[\"sphere_radius\"]\n", "# ------------------------------------------------------------------------------------\n", "# These variables can be adjusted to make the bounding sphere fit the region of interest.\n", "# The adjusted values can then be set in the config as data.readjust.center and data.readjust.scale\n", "readjust_center = np.array([0., 0., 0.])\n", "readjust_scale = 0.25\n", "# ------------------------------------------------------------------------------------\n", "center += readjust_center\n", "radius *= readjust_scale\n", "# Make some points to hallucinate a bounding sphere.\n", "sphere_points = np.random.randn(100000, 3)\n", "sphere_points = sphere_points / np.linalg.norm(sphere_points, axis=-1, keepdims=True)\n", "sphere_points = sphere_points * radius + center" ] }, { "cell_type": "code", "execution_count": null, "id": "fdde170b-4546-4617-9162-a9fcb936347d", "metadata": {}, "outputs": [], "source": [ "# Visualize with K3D.\n", "vis_scale = 0.5\n", "plot = visualize.k3d_visualize_pose(poses,\n", " vis_depth=(0.5 * vis_scale),\n", " xyz_length=(0.1 * vis_scale),\n", " center_size=(0.05 * vis_scale),\n", " xyz_width=(0.02 * vis_scale))\n", "plot += k3d.points(xyzs, colors=rgbs, point_size=(0.05 * vis_scale), shader=\"flat\")\n", "plot += k3d.points(sphere_points, color=0x4488ff, point_size=0.02, shader=\"flat\")\n", "plot.display()\n", "plot.camera_fov = 30.0" ] } ], "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }