Introduction This step will guide you on how to upload images for inference. After uploading the files, the PhenoNet platform will return the parameters file_path
and task_id
, which serve as an identifier for inference.
Request Description API address: https:
Request method: HTTPS POST
Return format: JSON
Content-Type: multipart/form-data
Headers: { "accessID" : { { STRING} } , "secretKey" : { { STRING} } }
Body: { "file" : { { BINARY_STREAM} } }
Parameter Description parameter data type description value required accessID string accessID
is required when sending requestssee Step I: Get AI&SK True secretKey string secretKey
is required when sending requestssee Step I: Get AI&SK True file binary stream File content binary stream, only a single image is allowed per uploading. - True
Response Description {
"code" : "{{INTEGER}}" ,
"msg" : "{{STRING}}" ,
"data" : {
"file_path" : "{{STRING}}" ,
"task_id" : "{{STRING}}"
}
}
Returned parameter description parameter data type description code integer state code msg string reference information data object returned content file_path string file save path task_id string task unique identification
Example Code Python GO (alpha) JavaScript (alpha) PHP (alpha)
import requests
ACCESS_ID = ''
SECRET_KEY = ''
HEADERS = {
"accessID" : ACCESS_ID,
"secretKey" : SECRET_KEY
}
def upload_file ( file_path, headers) :
url = "https://api.phenonet.org/api/v1/openapi/upload/"
try :
with open ( file_path, 'rb' ) as f:
files = { 'file' : f}
response = requests. post( url, headers= headers, files= files)
response. raise_for_status( )
return response. json( )
except requests. exceptions. RequestException as e:
print ( f"Error uploading file: { e} " )
return None
if __name__ == '__main__' :
img_path = ''
upload_res = upload_file( img_path, HEADERS)
if upload_res:
print ( upload_res)
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
)
const (
accessID = ""
secretKey = ""
apiBaseURL = "https://api.phenonet.org/api/v1/openapi"
)
func main ( ) {
imgPath := ""
headers := map [ string ] string {
"accessID" : accessID,
"secretKey" : secretKey,
}
res, err := uploadFile ( imgPath, headers)
if err != nil {
fmt. Printf ( "Error uploading file: %v\n" , err)
return
}
fmt. Println ( res)
}
func uploadFile ( filePath string , headers map [ string ] string ) ( map [ string ] interface { } , error ) {
url := fmt. Sprintf ( "%s/upload/" , apiBaseURL)
file, err := os. Open ( filePath)
if err != nil {
return nil , err
}
defer file. Close ( )
body := & bytes. Buffer{ }
writer := multipart. NewWriter ( body)
part, err := writer. CreateFormFile ( "file" , filePath)
if err != nil {
return nil , err
}
_ , err = io. Copy ( part, file)
if err != nil {
return nil , err
}
err = writer. Close ( )
if err != nil {
return nil , err
}
req, err := http. NewRequest ( "POST" , url, body)
if err != nil {
return nil , err
}
req. Header. Set ( "Content-Type" , writer. FormDataContentType ( ) )
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 ( )
resBody, err := ioutil. ReadAll ( res. Body)
if err != nil {
return nil , err
}
var resData map [ string ] interface { }
err = json. Unmarshal ( resBody, & resData)
if err != nil {
return nil , err
}
return resData, nil
}
const axios = require ( 'axios' ) ;
const fs = require ( 'fs' ) ;
const ACCESS_ID = '' ;
const SECRET_KEY = '' ;
const HEADERS = {
'accessID' : ACCESS_ID ,
'secretKey' : SECRET_KEY ,
} ;
async function uploadFile ( filePath, headers ) {
const url = 'https://api.phenonet.org/api/v1/openapi/upload/' ;
try {
const file = fs. createReadStream ( filePath) ;
const formData = new FormData ( ) ;
formData. append ( 'file' , file) ;
const response = await axios. post ( url, formData, {
headers : headers,
maxContentLength : Infinity ,
maxBodyLength : Infinity ,
} ) ;
return response. data;
} catch ( error) {
console. error ( ` Error uploading file: ${ error} ` ) ;
return null ;
}
}
const imgPath = '' ;
uploadFile ( imgPath, HEADERS )
. then ( ( uploadRes ) => {
console. log ( uploadRes) ;
} )
. catch ( ( error ) => {
console. error ( ` Error uploading file: ${ error} ` ) ;
} ) ;
<?php
$ACCESS_ID = '' ;
$SECRET_KEY = '' ;
$HEADERS = array (
'accessID' => $ACCESS_ID ,
'secretKey' => $SECRET_KEY ,
) ;
function uploadFile ( $filePath , $headers ) {
$url = 'https://api.phenonet.org/api/v1/openapi/upload/' ;
$file = fopen ( $filePath , 'r' ) ;
$filesize = filesize ( $filePath ) ;
$fields = array (
'file' => new CURLFile ( $filePath ) ,
) ;
$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 , $fields ) ;
curl_setopt ( $ch , CURLOPT_INFILESIZE , $filesize ) ;
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 ) ;
}
$imgPath = '' ;
$response = uploadFile ( $imgPath , $HEADERS ) ;
if ( $response ) {
echo $response ;
} else {
echo 'Error uploading file' ;
}
?>
Error Code code description 403 AI (Access ID)
or SK (Secret Key)
error
Prev
Get AI&SK
Next
Get network