Project Setup

Here is an end to end python example of how to pragmatically setup a new project in Labelbox.

# Two things to run this script
# 1. run "pip install graphqlclient"
# 2. Fill in <API-KEY-HERE>

# This script will create and setup a new project
# - A project called "Create Project Example"
# - A new dataset "Create Project Example: Ollie Dataset"
# - Three datarows in that dataset

import json
from graphqlclient import GraphQLClient
client = GraphQLClient('https://api.labelbox.com/graphql')
client.inject_token('Bearer <API-KEY-HERE>')

def me():
res_str = client.execute("""
query GetUserInformation {
user {
id
organization{
id
}
}
}
""")

res = json.loads(res_str)
return res['data']['user']


def createDataset(name):
res_str = client.execute("""
mutation CreateDatasetFromAPI($name: String!) {
createDataset(data:{
name: $name
}){
id
}
}
""", {'name': name})

res = json.loads(res_str)
return res['data']['createDataset']['id']


def createProject(name):
res_str = client.execute("""
mutation CreateProjectFromAPI($name: String!) {
createProject(data:{
name: $name
}){
id
}
}
""", {'name': name})

res = json.loads(res_str)
return res['data']['createProject']['id']


def completeSetupOfProject(project_id, dataset_id, labeling_frontend_id):
res_str = client.execute("""
mutation CompleteSetupOfProject($projectId: ID!, $datasetId: ID!, $labelingFrontendId: ID!){
updateProject(
where:{
id:$projectId
},
data:{
setupComplete: "2018-11-29T20:46:59.521Z",
datasets:{
connect:{
id:$datasetId
}
},
labelingFrontend:{
connect:{
id:$labelingFrontendId
}
}
}
){
id
}
}
""", {
'projectId': project_id,
'datasetId': dataset_id,
'labelingFrontendId': labeling_frontend_id
})

res = json.loads(res_str)
return res['data']['updateProject']['id']


def configure_interface_for_project(ontology, project_id, interface_id, organization_id):
res_str = client.execute("""
mutation ConfigureInterfaceFromAPI($projectId: ID!, $customizationOptions: String!, $labelingFrontendId: ID!, $organizationId: ID!) {
createLabelingFrontendOptions(data:{
customizationOptions: $customizationOptions,
project:{
connect:{
id: $projectId
}
}
labelingFrontend:{
connect:{
id:$labelingFrontendId
}
}
organization:{
connect:{
id: $organizationId
}
}
}){
id
}
}
""", {
'projectId': project_id,
'customizationOptions': json.dumps(ontology),
'labelingFrontendId': interface_id,
'organizationId': organization_id,
})

res = json.loads(res_str)
return res['data']['createLabelingFrontendOptions']['id']


def get_image_labeling_interface_id():
res_str = client.execute("""
query GetImageLabelingInterfaceId {
labelingFrontends(where:{
iframeUrlPath:"https://image-segmentation-v4.labelbox.com"
}){
id
}
}
""")

res = json.loads(res_str)
return res['data']['labelingFrontends'][0]['id']


def create_datarow(row_data, external_id,dataset_id):
res_str = client.execute("""
mutation CreateDataRowFromAPI(
$rowData: String!,
$externalId: String,
$datasetId: ID!
) {
createDataRow(data:{
externalId: $externalId,
rowData: $rowData,
dataset:{
connect:{
id: $datasetId
}
}
}){
id
}
}
""", {
'rowData': row_data,
'externalId': external_id,
'datasetId': dataset_id
})

res = json.loads(res_str)
return res['data']['createDataRow']['id']

data = [
{
"image_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjccycyq6hpje0134aktmtshq%2Fdd850fd1-b529-4ca7-850d-54889aaaa57f%2FImage%20from%20iOS.jpg?alt=media&token=2e9d93f1-0379-4b8d-a387-4b7131db1e01",
"external_id": "dog_image_one"
},
{
"image_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjccycyq6hpje0134aktmtshq%2Fdd850fd1-b529-4ca7-850d-54889aaaa57f%2FImage%20from%20iOS%20(2).jpg?alt=media&token=b0a28837-1fa9-443f-8819-4d33b80510b9",
"external_id": "dog_image_two"
},
{
"image_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjccycyq6hpje0134aktmtshq%2Fdd850fd1-b529-4ca7-850d-54889aaaa57f%2FImage%20from%20iOS%20(1).jpg?alt=media&token=4d5a221f-92e9-4aed-b3c2-466e100c8d91",
"external_id": "dog_image_three"
}
]


if __name__ == "__main__":
user_info = me()
org_id = user_info['organization']['id']
project_id = createProject('Create Project Example')
print('Created Project: %s' % (project_id))
dataset_id = createDataset(
'Ollie Dataset for Create Project Example')
print('Created Dataset: %s' % (dataset_id))
interface_id = get_image_labeling_interface_id()
ontology = {
"tools": [
{
"color": "navy",
"tool": "polygon",
"name": "Ollie"
}
],
"classifications": [
{
"name": "appearance",
"instructions": "What is this dogs appearance?",
"type": "radio",
"options": [
{
"value": "cute",
"label": "Cute"
},
{
"value": "stoic",
"label": "Stoic"
}
]
}
]
}

configure_interface_for_project(
ontology, project_id, interface_id, org_id)
completeSetupOfProject(project_id, dataset_id, interface_id)
print('Attached Dataset and Interface to Created Project')

for row in data:
data_row_id = create_datarow(row['image_url'], row['external_id'], dataset_id)
print('Created DataRow: %s' % (data_row_id))

print('Rebuilt labeling queue since data was added')
print('Go to https://app.labelbox.com/projects/%s/overview and click start labeling' % (project_id))


How did we do?