Introduction
Invoke the public and private network to classify crop phenophase.
Request Description
API address: https:
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
parameter | data type | description | value | required |
---|
accessID | string | accessID is required when sending requests | see Step I: Get AI&SK | True |
secretKey | string | secretKey is required when sending requests | see Step I: Get AI&SK | True |
task_id | string | task unique identification | | True |
task_name | string | | | True |
model_id | string | network unique identification | | True |
file_path | string | | | True |
Response Description
{
"code": "{{INTEGER}}",
"msg": "{{STRING}}",
"data": {
"task_id": "{{STRING}}"
}
}
Returned parameter description
parameter | data type | description |
---|
code | integer | state code |
msg | string | reference information |
task_id | string | task Unique Identification |
Example Code
Key code
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)
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const (
ACCESS_ID = ""
SECRET_KEY = ""
)
var (
HEADERS = map[string]string{
"accessID": ACCESS_ID,
"secretKey": SECRET_KEY,
}
)
type Task struct {
ID string `json:"task_id"`
Name string `json:"task_name"`
ModelID string `json:"model_id"`
FilePath string `json:"file_path"`
Device string `json:"device"`
}
func submitTask(task Task, headers map[string]string) (map[string]interface{}, error) {
url := "https://api.phenonet.org/api/v1/openapi/classification/"
task.FilePath = "https://files.phenonet.org/user/" + task.FilePath
jsonData, err := json.Marshal(task)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
for key, value := range headers {
req.Header.Set(key, value)
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result, nil
}
func main() {
task := Task{
ID: "",
Name: "",
ModelID: "",
FilePath: "",
Device: "1",
}
res, err := submitTask(task, HEADERS)
if err != nil {
fmt.Printf("Error submitting task: %v\n", err)
} else {
fmt.Println(res)
}
}
const https = require('https');
const ACCESS_ID = '';
const SECRET_KEY = '';
const HEADERS = {
'accessID': ACCESS_ID,
'secretKey': SECRET_KEY,
};
function submitTask(task, headers) {
const url = 'https://api.phenonet.org/api/v1/openapi/classification/';
task.file_path = 'https://files.phenonet.org/user/' + task.file_path;
const data = JSON.stringify(task);
const options = {
method: 'POST',
headers: {
...headers,
'Content-Type': 'application/json',
'Content-Length': data.length,
},
};
return new Promise((resolve, reject) => {
const req = https.request(url, options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(JSON.parse(data));
});
});
req.on('error', (error) => {
reject(error);
});
req.write(data);
req.end();
});
}
const task = {
task_id: '',
task_name: '',
model_id: '',
file_path: '',
device: '1',
};
submitTask(task, HEADERS)
.then((submitRes) => {
console.log(submitRes);
})
.catch((error) => {
console.error(`Error submitting task: ${error}`);
});
<?php
$ACCESS_ID = '';
$SECRET_KEY = '';
$HEADERS = array(
'accessID' => $ACCESS_ID,
'secretKey' => $SECRET_KEY,
);
function submitTask($task, $headers) {
$url = 'https://api.phenonet.org/api/v1/openapi/classification/';
$task['file_path'] = 'https://files.phenonet.org/user/' . $task['file_path'];
$jsonData = json_encode($task);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
$task = array(
'task_id' => '',
'task_name' => '',
'model_id' => '',
'file_path' => '',
'device' => '1',
);
$response = submitTask($task, $HEADERS);
if ($response) {
echo $response;
} else {
echo 'Error submitting task';
}
?>
Full code
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:
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))
{
"task_name": 'test',
"is_finished": 3,
"stage": 5
}
Error Code
code | description |
---|
403 | AI (Access ID) or SK (Secret Key) error |
500 | create task failed |