import os
from dotenv import load_dotenv
Box21 api
Interact with your Box21
load_dotenv()= os.environ.get('EMAIL')
API_USERNAME = os.environ.get('PASSWORD')
API_PASSWORD = os.environ.get('PROJECT_ID') API_PROJECT_ID
Box21Api
Box21Api (email:str, password:str, host:str, port:int, project_id:int)
Box21 Api class
= Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID) box21_api
Retrieve assets
Box21Api.get_asset
Box21Api.get_asset (asset_id:int)
Box21Api.get_assets
Box21Api.get_assets (offset:int=0, limit:int=None)
= Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
box21_api = box21_api.get_assets(limit=15)
assets print('Number of assets retrieved', len(assets))
= assets[0]
first_asset print(first_asset)
Number of assets retrieved 7
Asset({"filename": "eca8f6bc-2090-409e-8e07-7482dba50b34_IMG_0106.JPG", "import": "true", "description": "Human"})
= box21_api.get_asset(first_asset.id)
first_asset print(first_asset)
Asset({"filename": "eca8f6bc-2090-409e-8e07-7482dba50b34_IMG_0106.JPG", "import": "true", "description": "Human"})
Box21Api.download_asset
Box21Api.download_asset (asset_id:int)
= Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
box21_api = box21_api.get_assets(limit=10)
assets = box21_api.download_asset(assets[1].id) image
image
Retrieve annotations
Box21Api.get_annotations
Box21Api.get_annotations (asset_id:int)
= Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
box21_api = box21_api.get_annotations(assets[3].id)
annotations 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
Box21Api.delete_asset_meta_key
Box21Api.delete_asset_meta_key (asset_id:int, key:str)
Box21Api.update_asset_meta
Box21Api.update_asset_meta (asset_id:int, key:str, value:str)
= Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
box21_api = box21_api.update_asset_meta(assets[0].id, 'some_new_key', 'some new value')
updated_asset
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'}
= Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
box21_api = box21_api.delete_asset_meta_key(assets[0].id, 'some_new_key')
updated_asset
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
Box21Api.get_labels
Box21Api.get_labels ()
= Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
box21_api = box21_api.get_labels()
labels print(labels[0])
Label(('Bounding box', 'Chital', 1))
Get all annotations for a label:
Box21Api.get_label_annotations
Box21Api.get_label_annotations (label:box21_api.label.Label)
= Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
box21_api = box21_api.get_label_annotations(labels[0])
annotations 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
Box21Api.delete_assets
Box21Api.delete_assets (asset_ids:[<class'int'>])
Box21Api.add_asset
Box21Api.add_asset (file_path:pathlib.Path, meta, annotations:[<class'box21_api.annotation.Annotation'> ]=[], validated=False, in_validation_set=False)
'example.png')
image.save(= Path('example.png')
file_path = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
box21_api = box21_api.add_asset(file_path, {'testing': True}) asset
{"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()))
id])
box21_api.delete_assets([asset.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
= BoundingBox(
bounding_box = 0,
asset_idid= 0,
= 0.9,
certainty= 0,
label_id= 'Testing adding label',
label_name= 0,
project_id= False,
validated= 0.0,
x= 0.3,
y= 0.1,
width= 0.1
height
)
'example.png')
image.save(= Path('example.png')
file_path = Box21Api(API_USERNAME, API_PASSWORD, 'https://box21.ai', 443, API_PROJECT_ID)
box21_api = box21_api.add_asset(file_path, {'testing': True}, annotations=[bounding_box])
asset 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})
= box21_api.get_annotations(asset.id)
asset_annotations 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()))
id])
box21_api.delete_assets([asset.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
Box21Api.update_job
Box21Api.update_job (job_id, processing=None, processed=None, progress=None)