fathomnet / fathomnet-py

FathomNet Python client
https://fathomnet-py.readthedocs.io
MIT License
24 stars 3 forks source link

How to extract longitid and latitud from images #11

Closed mi2celis closed 2 years ago

mi2celis commented 2 years ago

This is not a bug but a query.

I have a set of images from Fathomnet. I would like to extract the location (latiud, lontitud and maybe depth) of each image. How can this be done?

hohonuuli commented 2 years ago

@mi2celis Generally, when you fetch data from FathomNet, the lat, Lon, depth will be included in the data for an image. Can you tell me how you grabbed the images so I can better answer your question?

mi2celis commented 2 years ago

I grabbed the images from the COCO json file using grep grep -Po '"flickr_url": "\K[^"]*' dataset.json | sort | uniq | xargs wget The COCO json file does not have latitude, longitude or depth as far as I can see.

I have figured out that I can grab them from a python script using images.find_by_concept() and then for each image getting image.latitude, image.longitude and image.depthMeters. Is there a better way?

hohonuuli commented 2 years ago

@mi2celis, @kevinsbarnard can probably best answer that for fathomnet-py. There are a number of ways to fetch the image stuff with the REST API. If you have the UUID you can fetch the data in python using:

import requests
i = requests.get("http://fathomnet.org:8080/images/a25520db-910b-4bc1-b704-006c29c19885").json()

If you just have the image and no other info, you could generate a checksum and lookup the image data using that:

import requests
import hashlib

p = "/path/to/image.png"

with open(p, "rb") as f:
  bytes = f.read() # read file as bytes
  sha256 = hashlib.sha256(bytes).hexdigest()

i = requests.get(f"http://fathomnet.org:8080/images/query/sha256/{sha256}").json()
kevinsbarnard commented 2 years ago

Thanks for responding so quickly @hohonuuli!

@mi2celis, through the Python API, you can do the equivalent to what @hohonuuli described like so:

from fathomnet.api import images

image = images.find_by_uuid('a25520db-910b-4bc1-b704-006c29c19885')

or by checkshum

from fathomnet.api import images

# ... compute the sha256 checksum as above ...

image = images.find_by_sha256(sha256)

and from there you can get the fields as you said, e.g.

print(image.latitude, image.longitude, image.depthMeters)  # etc.

If you downloaded the images using the fathomnet-generate script, the image names are likely their UUIDs, so you can probably use the former approach.