Skip to main content

Phenology classification

PhenoNetAbout 3 min

Introduction

Invoke the public and private network to classify crop phenophase.

Request Description

API address: https://api.phenonet.org/api/v1/openapi/classification/
Request method: HTTPS POST
Return format: JSON
Content-Type: multipart/form-data
Headers: {"accessID": {{STRING}}, "secretKey": {{STRING}}}
Body: {"task_id": {{STRING}}, "task_name": {{STRING}}, "model_id": {{STRING}}, "file_path": {{STRING}}}

Parameter Description

parameterdata typedescriptionvaluerequired
accessIDstringaccessID is required when sending requestssee Step I: Get AI&SKTrue
secretKeystringsecretKey is required when sending requestssee Step I: Get AI&SKTrue
task_idstringtask unique identificationTrue
task_namestringTrue
model_idstringnetwork unique identificationTrue
file_pathstringTrue

Response Description

{
    "code": "{{INTEGER}}",
    "msg": "{{STRING}}",
    "data": {
        "task_id": "{{STRING}}"
    }
}

Returned parameter description

parameterdata typedescription
codeintegerstate code
msgstringreference information
task_idstringtask Unique Identification

Example Code

Key code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests

ACCESS_ID = '{your ACCESS_ID}'
SECRET_KEY = '{your SECRET_KEY}'

HEADERS = {
    "accessID": ACCESS_ID,
    "secretKey": SECRET_KEY
}


def submit_task(task_id, task_name, model_id, file_path, headers):
    url = "https://api.phenonet.org/api/v1/openapi/classification/"
    data = {
        'task_id': task_id,
        'model_id': model_id,
        'file_path': 'https://files.phenonet.org/user/' + file_path,
        'task_name': task_name,
        'device': '1'
    }
    try:
        response = requests.post(url, headers=headers, data=data)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"Error submit task: {e}")
        return None


if __name__ == '__main__':
    task_id = ''
    task_name = ''
    model_id = ''
    file_path = ''
    submit_res = submit_task(task_id, task_name, model_id, file_path, HEADERS)
    if submit_res:
        print(submit_res)

Full code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
from requests.exceptions import RequestException
import time

ACCESS_ID = ''
SECRET_KEY = ''
TASK_NAME = ''
IMG_PATH = ''
HEADERS = {
    "accessID": ACCESS_ID,
    "secretKey": SECRET_KEY
}


def upload_file(file_path):
    url = "https://api.phenonet.org/api/v1/openapi/upload/"
    files = {'file': open(file_path, 'rb')}
    try:
        response = requests.post(url, headers=HEADERS, files=files)
        response.raise_for_status()
        return response.json()
    except RequestException as e:
        print('An exception occurred while uploading the file: ', e)


def get_networks():
    url = "https://api.phenonet.org/api/v1/openapi/models"
    try:
        response = requests.get(url, headers=HEADERS)
        response.raise_for_status()
        return response.json()
    except RequestException as e:
        print('An exception occurred while obtaining the network list: ', e)


def submit_task(task_id, task_name, model_id, file_path):
    url = "https://api.phenonet.org/api/v1/openapi/classification/"
    data = {
        'task_id': task_id,
        'model_id': model_id,
        'file_path': file_path,
        'task_name': task_name,
        'device': '1'
    }
    try:
        response = requests.post(url, headers=HEADERS, data=data)
        response.raise_for_status()
        return response.json()
    except RequestException as e:
        print('An exception occurred while submitting the task: ', e)


def get_task_status(task_id):
    url = "https://api.phenonet.org/api/v1/openapi/classification"
    params = {
        'task_id': task_id,
    }
    try:
        response = requests.get(url, headers=HEADERS, params=params)
        response.raise_for_status()
        return response.json()
    except RequestException as e:
        print('Exception occurred while obtaining task status: ', e)


def wait_for_task_completion(task_id):
    while True:
        task_status = get_task_status(task_id)['data'][0]
        if task_status['is_finished'] in (2, 3):
            return {
                'task_name':  task_status['task_name'],
                'is_finished': task_status['is_finished'],
                'stage': task_status['stage']
            }
        else:
            time.sleep(5)


upload_response = upload_file(IMG_PATH)
if upload_response:
    task_id = upload_response['data']['task_id']
    file_path = 'https://files.phenonet.org/user/' + \
        upload_response['data']['file_path']
    network_list = get_networks()
    if network_list:
        # You can print the network list to see the available networks and select the one you want to use
        model_id = list(network_list['data'].keys())[0]
        submit_task_response = submit_task(
            task_id, TASK_NAME, model_id, file_path)
        if submit_task_response:
            print(wait_for_task_completion(task_id))

Error Code

codedescription
403AI (Access ID) or SK (Secret Key) error
500create task failed
Contributors: PhenoNet