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 31import argparse32import re33 34import requests35from lxml.html import soupparser36 37MAX_REQUEST_TRIALS = 1038 39 40def parse_args():41 parser = argparse.ArgumentParser()42 parser.add_argument("--lib_path", required=True)43 args = parser.parse_args()44 return args45 46 47def request_trial(func, *args, **kwargs):48 for i in range(MAX_REQUEST_TRIALS):49 try:50 response = func(*args, **kwargs)51 except: # noqa E72252 continue53 else:54 return response55 56 raise SystemError57 58 59def main():60 args = parse_args()61 62 ##########################################################################63 # Header file64 ##########################################################################65 66 with open(args.lib_path + ".h", "w") as f:67 f.write("#include <vector>\n")68 f.write("#include <string>\n")69 f.write("#include <unordered_map>\n\n")70 f.write("// { make1 : ({ model1 : sensor-width in mm }, ...), ... }\n")71 f.write(72 "typedef std::vector<std::pair<std::string, float>> make_specs_t;\n"73 )74 f.write(75 "typedef std::unordered_map<std::string, make_specs_t> camera_specs_t;;\n\n"76 )77 f.write("camera_specs_t InitializeCameraSpecs();\n\n")78 79 ##########################################################################80 # Source file81 ##########################################################################82 83 makes_response = requests.get("http://www.digicamdb.com")84 makes_tree = soupparser.fromstring(makes_response.text)85 makes_node = makes_tree.find('.//select[@id="select_brand"]')86 makes = [b.attrib["value"] for b in makes_node.iter("option")]87 88 with open(args.lib_path + ".cc", "w") as f:89 f.write("camera_specs_t InitializeCameraSpecs() {\n")90 f.write(" camera_specs_t specs;\n\n")91 for make in makes:92 f.write(" {\n")93 f.write(94 ' auto& make_specs = specs["%s"];\n'95 % make.lower().replace(" ", "")96 )97 98 models_response = request_trial(99 requests.post,100 "http://www.digicamdb.com/inc/ajax.php",101 data={"b": make, "role": "header_search"},102 )103 104 models_tree = soupparser.fromstring(models_response.text)105 models_code = ""106 num_models = 0107 for model_node in models_tree.iter("option"):108 model = model_node.attrib.get("value")109 model_name = model_node.text110 if model is None:111 continue112 113 url = "http://www.digicamdb.com/specs/{0}_{1}".format(114 make, model115 )116 specs_response = request_trial(requests.get, url)117 118 specs_tree = soupparser.fromstring(specs_response.text)119 for spec in specs_tree.findall('.//td[@class="info_key"]'):120 if spec.text.strip() == "Sensor:":121 sensor_text = spec.find("..").find(122 './td[@class="bold"]'123 )124 sensor_text = sensor_text.text.strip()125 m = re.match(".*?([\d.]+) x ([\d.]+).*?", sensor_text)126 sensor_width = m.group(1)127 data = (128 model_name.lower().replace(" ", ""),129 float(sensor_width.replace(" ", "")),130 )131 models_code += (132 ' make_specs.emplace_back("%s", %.4ff);\n' % data133 )134 135 print(make, model_name)136 print(" ", sensor_text)137 138 num_models += 1139 140 f.write(" make_specs.reserve(%d);\n" % num_models)141 f.write(models_code)142 f.write(" }\n\n")143 144 f.write(" return specs;\n")145 f.write("}\n")146 147 148if __name__ == "__main__":149 main()150 