#!/usr/bin/env python # # Copyright (c) 2011 Brian House # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # See for details. # """ http://blog.brianhouse.net/post/7359294387 http://openpaths.cc/api """ import os, oauth2, time, urllib, urllib2, json from PIL import Image ACCESS = "YOURACCESSKEYHERE" SECRET = "YOURSECRETKEYHERE" URL = "https://openpaths.cc/api" def get_points(): """Download JSON data from OpenPaths""" def build_request(url, method): params = { 'oauth_version': "1.0", 'oauth_nonce': oauth2.generate_nonce(), 'oauth_timestamp': int(time.time()), } consumer = oauth2.Consumer(key=ACCESS, secret=SECRET) params['oauth_consumer_key'] = consumer.key request = oauth2.Request(method=method, url=url, parameters=params) signature_method = oauth2.SignatureMethod_HMAC_SHA1() request.sign_request(signature_method, consumer, None) return request now = time.time() params = {'start_time': 0, 'end_time': now} params.update(build_request(URL, 'GET')) query = "%s?%s" % (URL, urllib.urlencode(params)) print(query) try: connection = urllib2.urlopen(query) points = json.loads(''.join(connection.readlines())) # print(json.dumps(points, indent=4)) except Exception as e: print(e) return points def get_streetviews(points): """For each lat/lon pair, pull Google Streetview panorama data and stitch together the choice tiles""" if not os.path.isdir("sv_images"): os.mkdir("sv_images") for i, point in enumerate(points): print("----------") panoid_url = "http://cbk0.google.com/cbk?output=json&ll=%s,%s" % (point['loc']['lat'], point['loc']['lon']) try: connection = urllib2.urlopen(panoid_url) json_data = json.loads(''.join(connection.readlines())) panoid = json_data['Location']['panoId'] except Exception as e: print("JSON download failed: %s" % panoid_url) continue url_left = "http://cbk0.google.com/cbk?output=tile&panoid=%s&zoom=3&x=2&y=1" % panoid url_right = "http://cbk0.google.com/cbk?output=tile&panoid=%s&zoom=3&x=3&y=1" % panoid filepath_left = "sv_images/%s_left.jpg" % i filepath_right = "sv_images/%s_right.jpg" % i try: urllib.urlretrieve(url_left, filepath_left) urllib.urlretrieve(url_right, filepath_right) except Exception as e: print("Image download failed") continue image_left = Image.open(filepath_left) image_right = Image.open(filepath_right) image = Image.new('RGB', (1024, 512)) image.paste(image_left, (0, 0)) image.paste(image_right, (512, 0)) os.remove(filepath_left) os.remove(filepath_right) filepath = "sv_images/%s.png" % i image.save(filepath, 'PNG') print(panoid_url) print(url_left) print(url_right) if not len(points): print("No points!") exit() def generate_video(): """Generate a video from the image sequence, using OpenCV""" import cv from cv import CV_FOURCC FPS = 30 IMAGE_FPS = 10 SIZE = 1024, 512 FILENAME = "streetview.mov" CODEC = CV_FOURCC('M', 'P', '4', '2') COLOR = True writer = cv.CreateVideoWriter(FILENAME, CODEC, FPS, SIZE, COLOR) files = [] for filename in os.listdir("sv_images"): if filename[-4:] != ".png": continue files.append(filename) files.sort(key=alphanum_key) for filename in files: path = "sv_images/%s" % filename print(path) image = Image.open(path) image = pil_to_ipl(image) for i in xrange(FPS / IMAGE_FPS): cv.WriteFrame(writer, image) def pil_to_ipl(pil_image): """Convert a PIL image to ipl (OpenCV)""" import cv cv_image = cv.CreateImageHeader(pil_image.size, cv.IPL_DEPTH_8U, 3) cv.SetData(cv_image, pil_image.rotate(180).tostring()[::-1]) return cv_image def alphanum_key(s): """Turn a string into a list of string and number chunks""" import re return [tryint(c) for c in re.split('([0-9]+)', s)] def tryint(s): try: return int(s) except: return s if __name__ == "__main__": points = get_points() get_streetviews(points) generate_video()