Skip to content

Instantly share code, notes, and snippets.

@xorwen
Last active December 10, 2022 08:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xorwen/896975f8dbcf11e4bf487e2298c656f4 to your computer and use it in GitHub Desktop.
Save xorwen/896975f8dbcf11e4bf487e2298c656f4 to your computer and use it in GitHub Desktop.
How to work with plant.id API
"""
The purpose of this code is to show how to work with plant.id API.
You'll find API documentation at https://plantid.docs.apiary.io and https://plant.id/api
"""
import base64
import requests
from time import sleep
secret_access_key = '-- ask for one at business@plant.id --'
def send_for_identificattion(file_names):
files_encoded = []
for file_name in file_names:
with open(file_name, 'rb') as file:
files_encoded.append(base64.b64encode(file.read()).decode('ascii'))
params = {
'latitude': 49.194161,
'longitude': 16.603017,
'week': 23,
'images': files_encoded,
'key': secret_access_key,
'parameters': ["crops_fast"]
}
# see the docs for more optinal atrributes; for example 'custom_id' allows you to work
# with your own identifiers
headers = {
'Content-Type': 'application/json'
}
response = requests.post('https://plant.id/api/identify', json=params, headers=headers)
if response.status_code != 200:
raise("send_for_identificattion error: {}".format(response.text))
# this reference allows you to gather the identification result (once its ready)
return response.json().get('id')
def get_suggestions(request_id):
params = {
"key": secret_access_key,
"ids": [request_id]
}
headers = {
'Content-Type': 'application/json'
}
# To keep it simple, we are pooling the API waiting for the server to finish the identification.
# The better way would be to utilise "callback_url" parameter in /identify call to tell the our server
# to call your's server enpoint once the identificatin is done.
while True:
print("Waiting for suggestions...")
sleep(5)
resp = requests.post('https://plant.id/api/check_identifications', json=params, headers=headers).json()
if resp[0]["suggestions"]:
return resp[0]["suggestions"]
# more photos of the same plant increases the accuracy
request_id = send_for_identificattion(['photo1.jpg','photo2.jpg'])
# just listing the suggested plant names here (without the certainty values)
for suggestion in get_suggestions(request_id):
print(suggestion["plant"]["name"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment