{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71885,"databundleVersionId":8015523,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%cd /kaggle/working/\n!rm -rf /kaggle/working/Hierarchical-Localization\n!git clone --quiet --recursive https://github.com/cvg/Hierarchical-Localization/\n%cd /kaggle/working/Hierarchical-Localization\n!pip install -e .\n\nfrom hloc import extract_features, match_features, reconstruction, visualization, pairs_from_exhaustive\nfrom hloc.visualization import plot_images, read_image\nfrom hloc.utils import viz_3d\nimport pycolmap\n%cd /kaggle/working/\n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:38:52.958981Z","iopub.execute_input":"2024-03-27T20:38:52.959420Z","iopub.status.idle":"2024-03-27T20:39:51.291459Z","shell.execute_reply.started":"2024-03-27T20:38:52.959386Z","shell.execute_reply":"2024-03-27T20:39:51.290244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image, ImageDraw\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport os\nfrom matplotlib import pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-27T20:39:51.294178Z","iopub.execute_input":"2024-03-27T20:39:51.294579Z","iopub.status.idle":"2024-03-27T20:39:51.301583Z","shell.execute_reply.started":"2024-03-27T20:39:51.294545Z","shell.execute_reply":"2024-03-27T20:39:51.300550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata = pd.read_csv(\"/kaggle/input/image-matching-challenge-2024/train/train_labels.csv\")\nmetadata['image_name'] = [p.split(\".\")[0] for p in metadata['image_name'].values]\nmetadata","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:39:51.303136Z","iopub.execute_input":"2024-03-27T20:39:51.303469Z","iopub.status.idle":"2024-03-27T20:39:51.356343Z","shell.execute_reply.started":"2024-03-27T20:39:51.303441Z","shell.execute_reply":"2024-03-27T20:39:51.355376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def find_images(base_path, extension='.png'):\n    image_paths = []\n    for root, dirs, files in os.walk(base_path):\n        for file in files:\n            if file.endswith(extension):\n                image_paths.append(os.path.join(root, file))\n    return image_paths\n\n# Usage\nbase_image_path = '/kaggle/input/image-matching-challenge-2024/train'  # Replace with the path to your 'train' directory\nall_image_paths = find_images(base_image_path)\n\nall_image_paths[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:39:51.358042Z","iopub.execute_input":"2024-03-27T20:39:51.358406Z","iopub.status.idle":"2024-03-27T20:39:51.420778Z","shell.execute_reply.started":"2024-03-27T20:39:51.358364Z","shell.execute_reply":"2024-03-27T20:39:51.419479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the full paths into a list of directories and file names\nimage_names = [os.path.basename(path).split(\".\")[0] for path in all_image_paths]\nimage_type = [path.split(\"/\")[5] for path in all_image_paths]\n# Create the DataFrame\ndf_images = pd.DataFrame({\n    'image_name': image_names,\n    'image_path': all_image_paths,\n    'scene':image_type\n})\nmetadata_df = metadata.merge(df_images, on = [\"image_name\", \"scene\"])\nmetadata_df","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:39:51.424486Z","iopub.execute_input":"2024-03-27T20:39:51.425006Z","iopub.status.idle":"2024-03-27T20:39:51.460068Z","shell.execute_reply.started":"2024-03-27T20:39:51.424972Z","shell.execute_reply":"2024-03-27T20:39:51.459047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata_df['scene'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:39:51.461271Z","iopub.execute_input":"2024-03-27T20:39:51.462403Z","iopub.status.idle":"2024-03-27T20:39:51.471562Z","shell.execute_reply.started":"2024-03-27T20:39:51.462358Z","shell.execute_reply":"2024-03-27T20:39:51.470080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_images(image_paths, rows=1, cols=2):\n    fig, axes = plt.subplots(rows, cols, figsize=(15, 10))\n    for ax, image_path in zip(axes.flatten(), image_paths):\n        if os.path.exists(image_path):  # Check if the file exists\n            img = cv2.imread(image_path)\n            if img is not None:  # Check if the image was loaded successfully\n                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n                ax.imshow(img)\n            else: \n                ax.set_title(f\"Could not load image at {image_path}\")\n        else:\n            ax.set_title(f\"File does not exist: {image_path}\")\n        ax.axis('off')\n    plt.tight_layout()\n    plt.show()\n\nscenes = metadata_df['scene'].unique()\nfor i in scenes:\n    print(f\"samples for scene {i}\")\n    sample_images = metadata_df.loc[metadata_df['scene']==i].sample(6)['image_path'].values\n    visualize_images(sample_images)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:39:51.473635Z","iopub.execute_input":"2024-03-27T20:39:51.474103Z","iopub.status.idle":"2024-03-27T20:40:12.948066Z","shell.execute_reply.started":"2024-03-27T20:39:51.474072Z","shell.execute_reply":"2024-03-27T20:40:12.946923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scene = 'church'\ndef visualize_scene_with_reconstruction(scene:str, limit = 12):\n    src = f'/kaggle/input/image-matching-challenge-2024/train/{scene}'\n    limit = limit\n    all_image_paths_dio = [k for k in all_image_paths if 'dioscuri' in k]\n    images = [cv2.cvtColor(cv2.imread(im), cv2.COLOR_BGR2RGB) for im in all_image_paths_dio[:limit]]\n\n    rec_gt = pycolmap.Reconstruction(f'{src}/smf')\n\n    fig = viz_3d.init_figure()\n    # viz_3d.plot_cameras(fig, rec_gt, color='rgba(50,255,50, 0.5)', name=\"Ground Truth\", size=10)\n    viz_3d.plot_reconstruction(fig, rec_gt, cameras = False, color='rgba(227,168,30,0.5)', name=\"Ground Truth\", cs=5)\n    fig.show()\nfor i in [\"church\", \"dioscuri\"]:\n    visualize_scene_with_reconstruction(i, limit = 25)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:40:12.949752Z","iopub.execute_input":"2024-03-27T20:40:12.950120Z","iopub.status.idle":"2024-03-27T20:40:32.536388Z","shell.execute_reply.started":"2024-03-27T20:40:12.950090Z","shell.execute_reply":"2024-03-27T20:40:32.535353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"to be continue....","metadata":{}},{"cell_type":"markdown","source":"Referances\n- https://www.kaggle.com/code/leonidkulyk/eda-imc-3d-plots-interactive-vis#--2.-Dataset-heritage","metadata":{}}]}