Files
object-detection-with-stree…/1_Data_Acquisition_GSV.ipynb
2021-05-08 12:39:13 +03:00

251 lines
7.3 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "street_view.ipynb",
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "KQSP7h-nrure"
},
"source": [
"#This code uses the google streetview.api and the haversine formula to download Google Street View."
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "DLUJdoRpr5vL"
},
"source": [
"!pip install google_streetview"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "XPEWOX8mr-Z5"
},
"source": [
"# Import google_streetview for the api module\n",
"import google_streetview.api\n",
"from google.colab import files as FILE\n",
"import requests\n",
"import os\n",
"import re"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "6ISp63NDsGhM"
},
"source": [
"'''\n",
"Calculate distance using the Haversine Formula\n",
"'''\n",
"#The haversine formula determines the great-circle distance between two points on a sphere given their longitudes and latitudes\n",
"\n",
"def haversine(coord1: object, coord2: object):\n",
" import math\n",
"\n",
" # Coordinates in decimal degrees (e.g. 2.89078, 12.79797)\n",
" lon1, lat1 = coord1\n",
" lon2, lat2 = coord2\n",
"\n",
" R = 6371000 # radius of Earth in meters\n",
" phi_1 = math.radians(lat1)\n",
" phi_2 = math.radians(lat2)\n",
"\n",
" delta_phi = math.radians(lat2 - lat1)\n",
" delta_lambda = math.radians(lon2 - lon1)\n",
"\n",
" a = math.sin(delta_phi / 2.0) ** 2 + math.cos(phi_1) * math.cos(phi_2) * math.sin(delta_lambda / 2.0) ** 2\n",
" \n",
" c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))\n",
" head= math.atan2(math.sin(delta_lambda)*math.cos(phi_2), math.cos(phi_1)*math.sin(phi_2)-math.sin(phi_1)*math.cos(phi_2)*math.cos(delta_lambda))\n",
" meters = R * c # output distance in meters\n",
" km = meters / 1000.0 # output distance in kilometers\n",
"\n",
" meters = round(meters, 3)\n",
" headd=head*57.295779513 #convert radian to degree\n",
" km = round(km, 3)\n",
" kms=km/100\n",
" stepsze=kms/111.2 #the number of grid points in each direction. \n",
" print(f\"Distance: {meters} m\")\n",
" print(f\"Distance: {km} km\")\n",
" print(f\"stepsize: {kms} km\")\n",
" print(f\"stepsizedegreee: {stepsze} degree\")\n",
" print(f\"head: {headd} degree\")\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Yc_enRjSsH5I"
},
"source": [
"# da and db are the lengths of the sides of the rectangle in the latitude and longitude direction\n",
"db=haversine([ 40.982199, 28.756978 ], [40.987382, 28.809335 ])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Zpgm8-IHsMiG"
},
"source": [
"da=haversine([ 40.987382, 28.809335 ], [40.982199, 28.756978 ])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "gkp1uS50sS2Q"
},
"source": [
"image_links = []\n",
"api_results=[]\n",
"api_list=[]\n",
"locs= []\n",
"\n",
"#Depending on the values above, the values they take should change.\n",
"db= 0.0005255395683453238\n",
"da= 0.0005255395683453238\n",
"#longitudes and latitudes of two point \n",
"rem= 41.016097\n",
"rbm= 29.205892 \n",
"ree= 41.127392\n",
"rbe=29.296529\n",
"while ree > rem and rbe > rbm:\n",
" rem=rem+db;\n",
" alst = list(str(rem)+ \",\")\n",
" rbm=rbm+db;\n",
" blst = list(str(rbm))\n",
" a = ''.join(alst); b = ''.join(blst) \n",
"#At this stage, the latitude and longitude are combined into a format that we can use. \n",
" my_list = [a,b]\n",
" my_string = ''.join(my_list)\n",
" my_string\n",
" locs.append(my_string)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "IbdfaTs-sZqA"
},
"source": [
"# Define parameters for street view api\n",
"for i in range(150):\n",
" params = {\n",
"\t'size': '300x300', # max 640x640 pixels\n",
"\t'location': locs[i],\n",
"\t'heading': '170;90;-90' #indicates the compass heading of the camera. Accepted values are from 0 to 360\n",
" 'pitch': '-0.76', #(default is 0) specifies the up or down angle of the camera relative to the Street View vehicle.\n",
"\t'key': '..... '\n",
"}\n",
"\n",
" api = google_streetview.helpers.api_list(params)\n",
" api_list.append(api)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "WPpRxcYgsdVP"
},
"source": [
"# Create a results object\n",
"for i in range(350):\n",
" api_result = google_streetview.api.results(api_list[i])\n",
" api_results.append(api_result)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "SGLdezbNsdoX"
},
"source": [
"#Download images as a link\n",
"for i in range(240):\n",
" link = api_results[i].links\n",
" image_links.append(link)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "n3_7gxlSslsX"
},
"source": [
"#Example link\n",
"image_links[2][2]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "i8hajtjqsdzl"
},
"source": [
"# We get the data on our own google drive.\n",
"from google.colab import drive\n",
"drive.mount('/content/gdrive')"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "dWx4-PL7srYE"
},
"source": [
"#Download images\n",
"for i in range(240):\n",
" img_data = requests.get(image_links[i][0]).content\n",
" with open('/content/gdrive/My Drive/buyukcekmece/lbktz' + str(i)+ '.jpg', 'wb') as handler:\n",
" handler.write(img_data)\n"
],
"execution_count": null,
"outputs": []
}
]
}