Box21 api

Interact with your Box21
import os
from dotenv import load_dotenv
load_dotenv()
API_USERNAME = os.environ.get('EMAIL')
API_PASSWORD = os.environ.get('PASSWORD')
API_PROJECT_ID = os.environ.get('PROJECT_ID')

source

Box21Api

 Box21Api (email:str, password:str, host:str, port:int, project_id:int)

Box21 Api class

box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)

Retrieve assets


source

Box21Api.get_asset

 Box21Api.get_asset (asset_id:int)

source

Box21Api.get_assets

 Box21Api.get_assets (offset:int=0, limit:int=None)
box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
assets = box21_api.get_assets(limit=15)
print('Number of assets retrieved', len(assets))
first_asset = assets[0]
print(first_asset)
Number of assets retrieved 7
Asset({"filename": "eca8f6bc-2090-409e-8e07-7482dba50b34_IMG_0106.JPG", "import": "true", "description": "Human"})
first_asset = box21_api.get_asset(first_asset.id)
print(first_asset)
Asset({"filename": "eca8f6bc-2090-409e-8e07-7482dba50b34_IMG_0106.JPG", "import": "true", "description": "Human"})

source

Box21Api.download_asset

 Box21Api.download_asset (asset_id:int)
box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
assets = box21_api.get_assets(limit=10)
image = box21_api.download_asset(assets[1].id)
image

Retrieve annotations


source

Box21Api.get_annotations

 Box21Api.get_annotations (asset_id:int)
box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
annotations = box21_api.get_annotations(assets[3].id)
print(len(annotations))
2

Update asset meta

An asset has meta which is a list of key value pairs. You can add, update and delete them via the api


source

Box21Api.delete_asset_meta_key

 Box21Api.delete_asset_meta_key (asset_id:int, key:str)

source

Box21Api.update_asset_meta

 Box21Api.update_asset_meta (asset_id:int, key:str, value:str)
box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
updated_asset = box21_api.update_asset_meta(assets[0].id, 'some_new_key', 'some new value')

print(json.loads(updated_asset.meta))
{'filename': 'eca8f6bc-2090-409e-8e07-7482dba50b34_IMG_0106.JPG', 'import': 'true', 'description': 'Human', 'some_new_key': 'some new value'}
box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
updated_asset = box21_api.delete_asset_meta_key(assets[0].id, 'some_new_key')

print(json.loads(updated_asset.meta))
{'filename': 'eca8f6bc-2090-409e-8e07-7482dba50b34_IMG_0106.JPG', 'import': 'true', 'description': 'Human'}

Get labels

Get all labels for a specific project


source

Box21Api.get_labels

 Box21Api.get_labels ()
box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
labels = box21_api.get_labels()
print(labels[0])
Label(('Bounding box', 'Chital', 1))

Get all annotations for a label:


source

Box21Api.get_label_annotations

 Box21Api.get_label_annotations (label:box21_api.label.Label)
box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
annotations = box21_api.get_label_annotations(labels[0])
print(len(annotations))
for annotation in annotations:
    print(annotation)
2
BoundingBox(Chital, (0.1641, 0.5291, 0.2766, 0.3381))
BoundingBox(Chital, (0.525, 0.3911, 0.3484, 0.3532))

Adding and deleting assets


source

Box21Api.delete_assets

 Box21Api.delete_assets (asset_ids:[<class'int'>])

source

Box21Api.add_asset

 Box21Api.add_asset (file_path:pathlib.Path, meta,
                     annotations:[<class'box21_api.annotation.Annotation'>
                     ]=[], validated=False, in_validation_set=False)
image.save('example.png')
file_path = Path('example.png')
box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
asset = box21_api.add_asset(file_path, {'testing': True})
{"deleted":null,"filename":"d4b7e858-8201-4352-a1f8-7d5a885cc4f4_example.png","id":974,"in_validation_set":false,"liked":false,"meta":"{\"testing\": true}","original_category":"","path":"d4b7e858-8201-4352-a1f8-7d5a885cc4f4_example.png","project_id":1,"unclear":null,"validated":"false"}
print('Number of assets before delete', len(box21_api.get_assets()))
box21_api.delete_assets([asset.id])
print('Number of assets after delete', len(box21_api.get_assets()))
Number of assets before delete 8
Number of assets after delete 7

Example adding a bounding box

bounding_box = BoundingBox(
    asset_id= 0,
    id= 0,
    certainty= 0.9,
    label_id= 0,
    label_name= 'Testing adding label',
    project_id= 0,
    validated= False,
    x= 0.0,
    y= 0.3,
    width= 0.1,
    height= 0.1
)

image.save('example.png')
file_path = Path('example.png')
box21_api = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
asset = box21_api.add_asset(file_path, {'testing': True}, annotations=[bounding_box])
print(asset)
{"deleted":null,"filename":"319a4aee-9551-42d5-bb6f-d728dc2f629a_example.png","id":975,"in_validation_set":false,"liked":false,"meta":"{\"testing\": true}","original_category":"","path":"319a4aee-9551-42d5-bb6f-d728dc2f629a_example.png","project_id":1,"unclear":null,"validated":"false"}

Asset({"testing": true})
asset_annotations = box21_api.get_annotations(asset.id)
asset_annotations
[BoundingBox(Testing adding label, (0.0, 0.3, 0.1, 0.1))]
print('Number of assets before delete', len(box21_api.get_assets()))
box21_api.delete_assets([asset.id])
print('Number of assets after delete', len(box21_api.get_assets()))
Number of assets before delete 8
Number of assets after delete 7

Updating jobs


source

Box21Api.update_job

 Box21Api.update_job (job_id, processing=None, processed=None,
                      progress=None)