city-intelligence-lab / digital-futures-2020

The official repo for the Artificial Intelligence for Resilient Urban Planning workshop by the City Intelligence Lab.
https://www.digitalfutures.world/workshops-europe-mideast-africa-blog/chronis
MIT License
21 stars 7 forks source link

streetview-images.ipynb mapillary ConnectionError #1

Open boytjj opened 2 years ago

boytjj commented 2 years ago

hi Khean,

I just try your streetview-images notebook and follow the tutorial on Youtube by your DF2020 workshop, but at the stage of "First Request for Image Keys", I can not get the response URL, URL = f'https://a.mapillary.com/v3/images/?bbox={BBOX}&client_id={ID}&per_page=1000', even I had applied my ID and token successfully.

And I also check for some answers about this issue, maybe it is about the rules between papillary V3 AND V4?

So could u plz give some advice on how to solve this problem?

many thanks, TAO

this is the copy of the issue page :

import requests from pathlib import Path

from matplotlib import pyplot as plt

ID = '750452761958**' TOKEN = 'MLY|7504527619587760|5a2637b136b22fe8f8a7cef10***'

BBOX = '139.712641,35.686912,139.725924,35.695417' # http://bboxfinder.com/ OUT = 'output_images' SIZE = 320 # {320, 640, 1024, 2048}

from google.colab import drive drive.mount('/content/gdrive')

OUT = '/content/gdrive/My Drive/WS_Materials/Street_View_Images' Path(OUT).mkdir(parents=True, exist_ok=True)

First Request for Image Keys

url = f'https://a.mapillary.com/v3/images/?bbox={BBOX}&client_id={ID}&per_page=1000'

headers = {'Authorization': f'Bearer {TOKEN}'}

Counters

count, failed = 0, 0

coords_x, coords_y = [], []

while url: response = requests.get(url, headers=headers)

# Getting Next Page URL
try:
    links = response.headers['link'].split(', ')
    for link in links:
        if 'rel="next"' in link:
            url = link[1:].split('>')[0]
        else:
            url = None
except:
    url = None

try:
    data = response.json()
    page_count = len(data["features"])
    count += page_count

    for i in range(page_count):

        if i % 10 == 0:
            # Getting Image Data
            image_data   = data['features'][i]
            image_key    = image_data['properties']['key']
            if image_data['properties']['ca']:
                image_ca = str(int(image_data['properties']['ca']))
            image_coords = [str(x) for x in image_data['geometry']['coordinates']]
            image_url = f'https://images.mapillary.com/{image_key}/thumb-{SIZE}.jpg'

            # coords_x.append(image_data['geometry']['coordinates'][0])
            # coords_y.append(image_data['geometry']['coordinates'][1])

            # Downloading Images
            image_req = requests.get(image_url)
            if image_req.status_code == 200:
                image_name = image_coords[0] + '_' + image_coords[1]
                image_name += f'_{image_ca}' if image_ca else ''
                with open(f'{OUT}/{image_name}.jpg', 'wb') as f:
                    f.write(image_req.content)
                    print(f'{i} | Downloaded image:{image_key} as {image_name}.jpg')
            else:
                print(f'{i} | Ignored image:{image_key}')
                failed += 1

except Exception as e:
    print(e)

print(f'Done! Successfully downloaded {count - failed}/{count} images!')

Showing on plot

plt.scatter(coords_x, coords_y)

plt.show()

gaierror Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/urllib3/connection.py in _new_conn(self) 158 conn = connection.create_connection( --> 159 (self._dns_host, self.port), self.timeout, **extra_kw) 160

15 frames gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

NewConnectionError Traceback (most recent call last) NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f8fa9793ed0>: Failed to establish a new connection: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

MaxRetryError Traceback (most recent call last) MaxRetryError: HTTPSConnectionPool(host='a.mapillary.com', port=443): Max retries exceeded with url: /v3/images/?bbox=139.712641,35.686912,139.725924,35.695417&client_id=7504527619587760&per_page=1000 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f8fa9793ed0>: Failed to establish a new connection: [Errno -2] Name or service not known'))

During handling of the above exception, another exception occurred:

ConnectionError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies) 514 raise SSLError(e, request=request) 515 --> 516 raise ConnectionError(e, request=request) 517 518 except ClosedPoolError as e:

ConnectionError: HTTPSConnectionPool(host='a.mapillary.com', port=443): Max retries exceeded with url: /v3/images/?bbox=139.712641,35.686912,139.725924,35.695417&client_id=7504527619587760&per_page=1000 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f8fa9793ed0>: Failed to establish a new connection: [Errno -2] Name or service not known'))

boytjj commented 2 years ago

slove the issue useing V4:

https://graph.mapillary.com/images?access_token=$TOKEN={TOKEN}&fields=id={ID}&bbox={BBOX}&per_page=1000'