2023-01-20 04:19:37 +01:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import asyncio
|
|
|
|
import argparse
|
2023-09-10 20:04:26 +02:00
|
|
|
from datetime import datetime
|
2023-09-11 22:56:21 +02:00
|
|
|
import writer
|
2023-09-12 14:31:10 +02:00
|
|
|
from model import PictureType
|
2023-01-20 04:19:37 +01:00
|
|
|
|
|
|
|
def parse_args(argv =None):
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--sequence_id', type=str, help='The mapillary sequence id to download')
|
|
|
|
parser.add_argument('--access_token', type=str, help='Your mapillary access token')
|
2023-09-12 14:31:10 +02:00
|
|
|
parser.add_argument('--image_count', type=int, default=None, help='How many images you want to download')
|
2023-01-20 04:19:37 +01:00
|
|
|
|
|
|
|
global args
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
def background(f):
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
return asyncio.get_event_loop().run_in_executor(None, f, *args, **kwargs)
|
|
|
|
return wrapped
|
|
|
|
|
2023-09-10 21:02:33 +02:00
|
|
|
#@background
|
|
|
|
def download(url, fn, metadata=None):
|
2023-09-12 14:31:10 +02:00
|
|
|
r = requests.get(url, stream=True)
|
|
|
|
image = write_exif(r.content, metadata)
|
|
|
|
with open(str(fn), "wb") as f:
|
|
|
|
f.write(image)
|
2023-01-20 04:19:37 +01:00
|
|
|
|
2023-09-12 14:31:10 +02:00
|
|
|
def write_exif(picture, img_metadata):
|
2023-09-10 21:02:33 +02:00
|
|
|
'''
|
|
|
|
Write exif metadata
|
|
|
|
'''
|
|
|
|
#{'thumb_original_url': 'https://scontent-cdg4-2.xx.fbcdn.net/m1/v/t6/An9Zy2SrH9vXJIF01QkBODyUbg7XSKfwL48UwHyvihSwvECGjVbG0vSw9uhxe2-Dq-k2eUcigb83buO6zo-7eVbykfp5aQIe1kgd-MJr66nU_H-o_mwBLZXgVbj5I_5WX-C9c6FxJruHkV962F228O0?ccb=10-5&oh=00_AfDOKD869DxL-4ZNCbVo8Rn29vsc0JyjMAU2ctx4aAFVMQ&oe=65256C25&_nc_sid=201bca',
|
|
|
|
# 'captured_at': 1603459736644, 'geometry': {'type': 'Point', 'coordinates': [2.5174596904057, 48.777089857534]}, 'id': '485924785946693'}
|
|
|
|
|
2023-09-12 14:31:10 +02:00
|
|
|
picture = writer.writePictureMetadata(picture, img_metadata)
|
|
|
|
picture = writer.add_altitude(picture, img_metadata)
|
|
|
|
picture = writer.add_direction(picture, img_metadata)
|
|
|
|
|
|
|
|
return picture
|
2023-09-11 20:21:20 +02:00
|
|
|
|
2023-01-20 04:19:37 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parse_args()
|
|
|
|
|
|
|
|
if args.sequence_id == None:
|
|
|
|
print('please provide the sequence_id')
|
|
|
|
exit()
|
|
|
|
|
|
|
|
if args.access_token == None:
|
|
|
|
print('please provide the access_token')
|
|
|
|
exit()
|
|
|
|
|
|
|
|
sequence_id= args.sequence_id
|
|
|
|
access_token = args.access_token
|
|
|
|
|
|
|
|
# create the data folder
|
|
|
|
if not os.path.exists('data'):
|
|
|
|
os.makedirs('data')
|
|
|
|
|
|
|
|
# create a folder for each unique sequence ID to group images by sequence
|
|
|
|
if not os.path.exists('data/{}'.format(sequence_id)):
|
|
|
|
os.makedirs('data/{}'.format(sequence_id))
|
|
|
|
|
2023-01-20 04:21:02 +01:00
|
|
|
header = {'Authorization' : 'OAuth {}'.format(access_token)}
|
|
|
|
url = 'https://graph.mapillary.com/image_ids?sequence_id={}'.format(sequence_id)
|
|
|
|
r = requests.get(url, headers=header)
|
|
|
|
data = r.json()
|
2023-01-20 04:19:37 +01:00
|
|
|
|
2023-01-20 04:21:02 +01:00
|
|
|
image_ids = data['data']
|
2023-09-12 14:31:10 +02:00
|
|
|
img_num = args.image_count if args.image_count is not None else len(image_ids)
|
2023-01-20 04:21:02 +01:00
|
|
|
urls = []
|
|
|
|
print(img_num)
|
|
|
|
print('getting urls')
|
2023-09-12 14:31:10 +02:00
|
|
|
for x in range(0, img_num):
|
|
|
|
#for x in range(0, 5):
|
2023-01-20 04:21:02 +01:00
|
|
|
image_id = image_ids[x]['id']
|
2023-09-10 20:04:26 +02:00
|
|
|
req_url = 'https://graph.mapillary.com/{}?fields=thumb_original_url,altitude,camera_type,captured_at,compass_angle,geometry,exif_orientation'.format(image_id)
|
2023-01-20 04:21:02 +01:00
|
|
|
r = requests.get(req_url, headers=header)
|
|
|
|
data = r.json()
|
|
|
|
print('getting url {} of {}'.format(x, img_num))
|
2023-09-11 20:21:20 +02:00
|
|
|
#print(data)
|
2023-09-10 20:04:26 +02:00
|
|
|
urls.append(data)
|
2023-01-20 04:19:37 +01:00
|
|
|
|
2023-01-20 04:21:02 +01:00
|
|
|
print('downloading.. this process will take a while. please wait')
|
|
|
|
for i,url in enumerate(urls):
|
2023-09-11 22:56:21 +02:00
|
|
|
date_time_image_filename = datetime.utcfromtimestamp(int(url['captured_at'])/1000).strftime('%Y-%m-%d_%HH%Mmn%S.%f')
|
|
|
|
path = 'data/{}/{}.jpg'.format(sequence_id, date_time_image_filename)
|
|
|
|
img_metadata = writer.PictureMetadata(
|
2023-09-12 14:31:10 +02:00
|
|
|
capture_time = datetime.utcfromtimestamp(int(url['captured_at'])/1000),
|
|
|
|
longitude = url['geometry']['coordinates'][0],
|
|
|
|
latitude = url['geometry']['coordinates'][1],
|
|
|
|
picture_type = PictureType("equirectangular") if url['camera_type'] == 'spherical' else None,
|
|
|
|
direction = url['compass_angle'],
|
|
|
|
altitude = url['altitude'],
|
2023-09-11 22:56:21 +02:00
|
|
|
)
|
|
|
|
download(url['thumb_original_url'],path, img_metadata)
|