Skip to content

Python Examples

"##Example: Post an image and a json to our image API.

This is a very basic python example script to post pictures to a our image API. You find a more examples here in our GitHub Repo. There are also more images for testing from our validation dataset in this repo.  

Single image processing

If you want to process a single image, you can use the following code snippet as basis for your application. Just replace the the string "" with your api key.

import os
import requests
from pprint import pprint as pp

IP = ''
VERSION = 'v1'
ROUTE = 'image_analysis'
URL = 'https://%s/%s/%s' % (IP, VERSION, ROUTE)

# Header of our requst. Replace <YOUR_API_KEY> with your api key.
HEADER = {'api_key': '<YOUR_API_KEY>'}

def single_processing():

    # make a dict with the picture
    image = os.path.join('..', 'data', 'tomato_nutrient', 'iron1.png')
    files = {"picture": open(image, 'rb')}

    # post both files to our API
    result =, files=files, headers=HEADER, timeout=200000)

    if result.status_code == 401:
        print('Authentication failed')
    elif result.status_code == 500:
        print('Internal server error...')
    elif result.status_code == 200:
        # load response that comes in JSON format and print the result
        json_data = result.json()

if __name__ == '__main__':

Batch Processing

The following examples shows a batch processing example for all files in a given directory

import os
import requests

# define some globals to build the API url
IP = ''
VERSION = 'v1'
ROUTE = 'image_analysis'
URL = 'https://%s/%s/%s' % (IP, VERSION, ROUTE)

# Header of our requst. Replace <YOUR_API_KEY> with your api key.
HEADER = {'api_key': '<YOUR_API_KEY>'}

def batch_processing(directory):
    this example is a bit more sophisticated than the simple single_processing function,
    it needs a base folder as argument
    and will iterate over every image in all subfolders of this directory

    # get a list of all the subfolders
    folderlist = [x[0] for x in os.walk(directory)]

    # iterate over all folder in a given directory
    for folder in folderlist:
        filelist = [i for i in os.listdir(folder) if i.endswith('.jpg') or i.endswith('.png')]

        # iterate over all files in a given subfolder
        for f in filelist:
            filepath = os.path.join(folder, f)
            files = {'picture': open(filepath, 'rb')}
            result =, files=files, headers=HEADER, timeout=10)

            # all data comes in json format
            json_data = result.json()

            # just printing
            print('filename:', f)
            print('input from folder:', folder)
            print('image API result:', json_data['image_analysis'][0]['name'], \
                  json_data['image_analysis'][0]['similarity'], 'peat_id', \

if __name__ == "__main__":

API response

the response on a valid API call would look like this.

    "big_enough": true,
    "code": 200,
    "image_analysis": [{
            "eppo": "COCHMI",
            "name": "Brown Spot of Rice",
            "peat_id": 100064,
            "rank": 1,
            "scientific_name": "Cochliobolus miyabeanus",
            "similarity": 87
            "eppo": "PYRIOR",
            "name": "Blast of Rice",
            "peat_id": 100058,
            "rank": 3,
            "scientific_name": "Magnaporthe oryzae",
            "similarity": 7
    "objects_net": [{
            "name": "eft",
            "rank": 1,
            "similarity": 32
            "name": "rule",
            "rank": 2,
            "similarity": 8
    "pic_id": "79937583-d368-45b9-87c1-010b55df0f5c",
    "plant_net": [{
        "name": "RICE",
        "rank": 1,
        "similarity": 94
    "recognized": "True",
    "recognized_bool": true