From b2f4130f8a981e94ee5a8fc626d4ded2d2c41ef9 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 21 Oct 2018 11:22:14 +0200 Subject: [PATCH] Check in Matterport code by Julia Pfitzer --- matterportsteal/matterport_dl.py | 107 +++++++++++++++++++++++++++++++ matterportsteal/matterport_dl.sh | 12 ++++ 2 files changed, 119 insertions(+) create mode 100644 matterportsteal/matterport_dl.py create mode 100644 matterportsteal/matterport_dl.sh diff --git a/matterportsteal/matterport_dl.py b/matterportsteal/matterport_dl.py new file mode 100644 index 0000000..8b87d14 --- /dev/null +++ b/matterportsteal/matterport_dl.py @@ -0,0 +1,107 @@ +import sys +import os +import argparse +from urllib import request +from urllib import parse +from bs4 import BeautifulSoup +import json +import subprocess +import math + +def pars_args(): + parser = argparse.ArgumentParser() + parser.add_argument( + "--url", + "-u", + type=str, + help="Download URL") + + parser.add_argument( + "--dir", + "-d", + type=str, + default = None, + help="Save directory if none is specified title will be used") + + parser.add_argument( + "--rot", + "-r", + type=str, + default = 0, + help = "Panorama rotation in degree") + + parser.add_argument( + "--disable-download", + "-dd", + action = "store_true", + help = "Disables Download") + + args = parser.parse_args() + args = vars(args) + + if not args["url"]: + print("+ + + Please provide URL + + +") + parser.print_help() + sys.exit(0) + + return args + +def get_url_data(url, directory): + # Load Data from URL + soup_from_url = BeautifulSoup(request.urlopen(url).read(), "html.parser") + pano_data = json.loads(soup_from_url.script.contents[0][33:-1]) + + print("Downloading: " + soup_from_url.title.string) + + # Create Directory if none is specified + if directory is None: + directory = soup_from_url.title.string + directory = directory.replace(" - Matterport 3D Showcase", "") + dl_url = create_dl_url(pano_data) + + return dl_url, directory + +def create_dl_url(pano_data): + # URL Download Creation + template_dl_url = pano_data["files"]["templates"][0] + pano_metadata = json.loads(pano_data["model"]["images"][-1]["metadata"]) + scan_id = pano_metadata["scan_id"] + custom_url_part = "tiles/" + scan_id + "/2k_face$i_$j_$k.jpg" + dl_url = template_dl_url.replace("{{filename}}", custom_url_part) + return dl_url + +def dl_tiles(dl_url, directory): + if not os.path.exists(directory): + os.makedirs(directory) + + picture_count = 0 + for i in range(0,6): + for j in range(0,4): + for k in range(0,4): + file_number = str(i) + "_" + str(k) + "_" + str(j) + temp_dl_url = dl_url.replace("$i_$j_$k", file_number) + request.urlretrieve(temp_dl_url, directory + \ + "/" + "part-" + str(i) + "-1-" + \ + str(j) + "_" + str(k) + ".jpg") + picture_count += 1 + print("Downloading Picture: " + str(picture_count) + "/96" + "\r", sep=' ', end='', flush=True) + print("Downloading Picture: " + str(picture_count) + "/96") + +def create_cube(directory, rotation): + print("Creating cube") + rc = subprocess.check_call(["./matterport_dl.sh", + str(directory), + str(int(math.degrees(rotation)))]) + + +def main(): + args = pars_args() + directory = args["dir"] + rotation = math.radians(-float(args["rot"])) + dl_url, directory = get_url_data(args["url"], directory) + if not args["disable_download"]: + dl_tiles(dl_url, directory) + create_cube(directory, rotation) + +if __name__ == "__main__": + main() diff --git a/matterportsteal/matterport_dl.sh b/matterportsteal/matterport_dl.sh new file mode 100644 index 0000000..029f79b --- /dev/null +++ b/matterportsteal/matterport_dl.sh @@ -0,0 +1,12 @@ +#!/bin/bash +mkdir "${1}/cube" -p + +for ((f=0;f<=5;f++)); do + convert "${1}/part-${f}-1-0_0.jpg" "${1}/part-${f}-1-0_1.jpg" "${1}/part-${f}-1-0_2.jpg" "${1}/part-${f}-1-0_3.jpg" +append "${1}/_part-${f}-1-0.jpg" + convert "${1}/part-${f}-1-1_0.jpg" "${1}/part-${f}-1-1_1.jpg" "${1}/part-${f}-1-1_2.jpg" "${1}/part-${f}-1-1_3.jpg" +append "${1}/_part-${f}-1-1.jpg" + convert "${1}/part-${f}-1-2_0.jpg" "${1}/part-${f}-1-2_1.jpg" "${1}/part-${f}-1-2_2.jpg" "${1}/part-${f}-1-2_3.jpg" +append "${1}/_part-${f}-1-2.jpg" + convert "${1}/part-${f}-1-3_0.jpg" "${1}/part-${f}-1-3_1.jpg" "${1}/part-${f}-1-3_2.jpg" "${1}/part-${f}-1-3_3.jpg" +append "${1}/_part-${f}-1-3.jpg" + convert "${1}/_part-${f}-1-0.jpg" "${1}/_part-${f}-1-1.jpg" "${1}/_part-${f}-1-2.jpg" "${1}/_part-${f}-1-3.jpg" -append "${1}/cube/${f}.jpg" +done + +cube2sphere -r 3840 2160 -R 0 0 $2 -f jpg -b blender "${1}/cube/3.jpg" "${1}/cube/1.jpg" "${1}/cube/2.jpg" "${1}/cube/4.jpg" "${1}/cube/0.jpg" "${1}/cube/5.jpg" -o "${1}/cube/${1}"