From 8f9f7281184f0451a748007d62fd36880326a29a Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 21 Oct 2018 14:21:48 +0200 Subject: [PATCH] Implement Matterport downloads --- hallmonitor.sh | 0 handler.py | 3 +- krpanosteal/__init__.py | 14 +++- matterportsteal/__init__.py | 109 ++++++++++--------------------- matterportsteal/matterport_dl.sh | 12 ---- server/daemon.py | 2 +- server/hallmonitor.py | 2 +- 7 files changed, 52 insertions(+), 90 deletions(-) mode change 100644 => 100755 hallmonitor.sh delete mode 100644 matterportsteal/matterport_dl.sh diff --git a/hallmonitor.sh b/hallmonitor.sh old mode 100644 new mode 100755 diff --git a/handler.py b/handler.py index f0efc4e..08a3991 100755 --- a/handler.py +++ b/handler.py @@ -6,7 +6,8 @@ import argparse regs = { "\d/\d/\d_\d\.jpg": "krpanosteal", - "pano\_[frblud].jpg": "krpanosteal" + "pano\_[frblud].jpg": "krpanosteal", + "my.matterport.com/show/": "matterportsteal" } diff --git a/krpanosteal/__init__.py b/krpanosteal/__init__.py index 5dcd2f8..e7f7465 100644 --- a/krpanosteal/__init__.py +++ b/krpanosteal/__init__.py @@ -7,6 +7,18 @@ import tempfile import pathlib import os from stitching import tiles_to_equirectangular_blender, multistitch +import robobrowser + +def krpano_detect(url): + ''' + Takes any URL and tries to detect a krpano panorama embedded in the site. + + :param url: URL of a site containing a krpano panorama + :return: URL of an image contained in krpano panorama if found, else False + ''' + + juha = bs4.BeautifulSoup(request.urlopen(url).read(), "html.parser") + def krpano_normalize(url): ''' @@ -139,7 +151,7 @@ def krpano_make_tiles(url): raise ValueError("%s does not seem to be a valid krpano URL." % url) -def krpano_to_equirectangular(url, rotation=[0,0,0], resolution=[0,0]): +def krpano_to_equirectangular(url, rotation=[0,0,0], resolution=[3840,1920]): ''' Takes the URL of any image in a krpano panorama and returns a finished stitched image. diff --git a/matterportsteal/__init__.py b/matterportsteal/__init__.py index 8b87d14..7ce464f 100644 --- a/matterportsteal/__init__.py +++ b/matterportsteal/__init__.py @@ -1,65 +1,21 @@ import sys import os -import argparse from urllib import request from urllib import parse from bs4 import BeautifulSoup import json import subprocess import math +import io +import PIL.Image +from stitching import multistitch, tiles_to_equirectangular_blender -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): +def get_url_data(url): # 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 + return create_dl_url(pano_data) def create_dl_url(pano_data): # URL Download Creation @@ -70,38 +26,43 @@ def create_dl_url(pano_data): 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) +def dl_tiles(dl_url): + output = [] - picture_count = 0 for i in range(0,6): + i_array = [] + for j in range(0,4): + j_array = [] + 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") + res = request.urlopen(temp_dl_url) + assert res.getcode() == 200 + fo = io.BytesIO(res.read()) + img = PIL.Image.open(fo) + j_array.append(img) -def create_cube(directory, rotation): - print("Creating cube") - rc = subprocess.check_call(["./matterport_dl.sh", - str(directory), - str(int(math.degrees(rotation)))]) + i_array.append(j_array) + output.append(i_array) -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) + return output -if __name__ == "__main__": - main() +def matterport_make_tiles(url): + dl_url = get_url_data(url) + images = dl_tiles(dl_url) + return multistitch(images) + +def matterport_to_equirectangular(url, rotation=[0,0,0], resolution=[3840,1920]): + stitched = matterport_make_tiles(url) + function = tiles_to_equirectangular_blender + + ordered = stitched[1:5] + [stitched[0], stitched[5]] + + rx, ry, rz = rotation + width, height = resolution + return function(*ordered, rx=rx, ry=ry, rz=rz, width=width, height=height) + +process_url = matterport_to_equirectangular diff --git a/matterportsteal/matterport_dl.sh b/matterportsteal/matterport_dl.sh deleted file mode 100644 index 029f79b..0000000 --- a/matterportsteal/matterport_dl.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/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}" diff --git a/server/daemon.py b/server/daemon.py index 7765cbf..5eef01d 100644 --- a/server/daemon.py +++ b/server/daemon.py @@ -39,7 +39,7 @@ def static(req): def addjob(req): jobid = str(uuid.uuid4()) - config = configparser.ConfigParser() + config = configparser.ConfigParser(strict=False, interpolation=None) try: title = re.sub(r"[^a-zA-Z0-9_\-]", "_", req.args["title"][0]) or output diff --git a/server/hallmonitor.py b/server/hallmonitor.py index 3972d99..2c134f1 100644 --- a/server/hallmonitor.py +++ b/server/hallmonitor.py @@ -13,7 +13,7 @@ processes = {} def handleIncoming(filename): print(filename) time.sleep(1) - config = configparser.ConfigParser() + config = configparser.ConfigParser(strict=False, interpolation=None) config.read(filename) p = subprocess.Popen(["./handler.py", config["Job"]["url"], "--title", filename.split("/")[-1] + "---" + config["Job"]["title"],