geolocation / app.py
abbylagar's picture
Update app.py
de53b63
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
import PIL.Image as Image
import tensorflow as tf
import tensorflow_hub as hub
import gradio as gr
from einops import rearrange
import s2cell
from geopy.geocoders import Nominatim
TF_MODEL_URL = 'https://tfhub.dev/google/planet/vision/classifier/planet_v2/1'
IMAGE_SHAPE = (299, 299)
labels=pd.read_csv('planet_v2_labelmap.csv')
classifier = tf.keras.Sequential([hub.KerasLayer(TF_MODEL_URL,
input_shape=IMAGE_SHAPE+(3,)
)])
def classify_image(image):
img = image/255.0
img = rearrange(img, 'h w c -> 1 h w c')
prediction = classifier.predict(img)
s2code = np.argmax(prediction)
loc=labels['S2CellId'][s2code]
location=s2cell.token_to_lat_lon(loc)
geolocator = Nominatim(user_agent="coordinateconverter")
address = location
location_add = geolocator.reverse(address)
return location,location_add
title = 'Photo Geolocation'
description = 'Just upload or drop an image to know where your photo is taken . '
article ='''PlaNet -Photo Geolocation with Convolutional Neural Networks. A gradio demo app for estimation of the address and coordinates of your photo.
<div style='text-align: center;'>PlaNet : <a href='https://tfhub.dev/google/planet/vision/classifier/planet_v2/1' target='_blank'>Model Repo</a> | <a href='https://arxiv.org/pdf/1602.05314v1.pdf' target='_blank'>Paper</a></div>'''
ex1 = '[email protected]'
ex2 = '[email protected]'
ex3 = '[email protected]'
ex4 = '[email protected]'
iface = gr.Interface(classify_image, inputs=gr.inputs.Image(shape=(299, 299), image_mode="RGB", type="numpy"),
outputs=[gr.outputs.Textbox(label='Latitude,Longitude'),gr.outputs.Textbox(label='Address')],examples=[ex1,ex2,ex3,ex4],
live=False,layout="horizontal", interpretation=None,title=title,
description=description, article=article)
iface.launch()