beverley-gorry/colmap-vslamlab
0
1# Copyright (c), ETH Zurich and UNC Chapel Hill.2# All rights reserved.3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions are met:6#7# * Redistributions of source code must retain the above copyright8# notice, this list of conditions and the following disclaimer.9#10# * Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of15# its contributors may be used to endorse or promote products derived16# from this software without specific prior written permission.17#18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28# POSSIBILITY OF SUCH DAMAGE.29 30 31from tempfile import mkdtemp32 33import numpy as np34from read_write_model import read_model, write_model35 36 37def compare_cameras(cameras1, cameras2):38 assert len(cameras1) == len(cameras2)39 for camera_id1 in cameras1:40 camera1 = cameras1[camera_id1]41 camera2 = cameras2[camera_id1]42 assert camera1.id == camera2.id43 assert camera1.width == camera2.width44 assert camera1.height == camera2.height45 assert np.allclose(camera1.params, camera2.params)46 47 48def compare_images(images1, images2):49 assert len(images1) == len(images2)50 for image_id1 in images1:51 image1 = images1[image_id1]52 image2 = images2[image_id1]53 assert image1.id == image2.id54 assert np.allclose(image1.qvec, image2.qvec)55 assert np.allclose(image1.tvec, image2.tvec)56 assert image1.camera_id == image2.camera_id57 assert image1.name == image2.name58 assert np.allclose(image1.xys, image2.xys)59 assert np.array_equal(image1.point3D_ids, image2.point3D_ids)60 61 62def compare_points(points3D1, points3D2):63 for point3D_id1 in points3D1:64 point3D1 = points3D1[point3D_id1]65 point3D2 = points3D2[point3D_id1]66 assert point3D1.id == point3D2.id67 assert np.allclose(point3D1.xyz, point3D2.xyz)68 assert np.array_equal(point3D1.rgb, point3D2.rgb)69 assert np.allclose(point3D1.error, point3D2.error)70 assert np.array_equal(point3D1.image_ids, point3D2.image_ids)71 assert np.array_equal(point3D1.point2D_idxs, point3D2.point2D_idxs)72 73 74def main():75 import sys76 77 if len(sys.argv) != 3:78 print(79 "Usage: python read_model.py "80 "path/to/model/folder/txt path/to/model/folder/bin"81 )82 return83 84 print("Comparing text and binary models ...")85 86 path_to_model_txt_folder = sys.argv[1]87 path_to_model_bin_folder = sys.argv[2]88 cameras_txt, images_txt, points3D_txt = read_model(89 path_to_model_txt_folder, ext=".txt"90 )91 cameras_bin, images_bin, points3D_bin = read_model(92 path_to_model_bin_folder, ext=".bin"93 )94 compare_cameras(cameras_txt, cameras_bin)95 compare_images(images_txt, images_bin)96 compare_points(points3D_txt, points3D_bin)97 98 print("... text and binary models are equal.")99 print("Saving text model and reloading it ...")100 101 tmpdir = mkdtemp()102 write_model(cameras_bin, images_bin, points3D_bin, tmpdir, ext=".txt")103 cameras_txt, images_txt, points3D_txt = read_model(tmpdir, ext=".txt")104 compare_cameras(cameras_txt, cameras_bin)105 compare_images(images_txt, images_bin)106 compare_points(points3D_txt, points3D_bin)107 108 print("... saved text and loaded models are equal.")109 print("Saving binary model and reloading it ...")110 111 write_model(cameras_bin, images_bin, points3D_bin, tmpdir, ext=".bin")112 cameras_bin, images_bin, points3D_bin = read_model(tmpdir, ext=".bin")113 compare_cameras(cameras_txt, cameras_bin)114 compare_images(images_txt, images_bin)115 compare_points(points3D_txt, points3D_bin)116 117 print("... saved binary and loaded models are equal.")118 119 120if __name__ == "__main__":121 main()122 