Project setup script

Alex Cota Updated by Alex Cota

Below is an end-to-end python script that includes most of the common actions customers do when configuring Labelbox programmatically with the Python SDK.

from labelbox import Client, Project, schema
import json
import re


def Copy_Ontology(project_name):
project_id = ""
try:
project = next(iter(client.get_projects(where=Project.name == project_name)))
project_id = project.uid
except:
print("Couldn't find project with name " + project_name)
exit()


# Use uid, to Query the Ontology for project
res_str = client.execute("""
query get_ontology($proj_id: ID!) {
project(where: {id: $proj_id}) {
ontology {
normalized
}
}
}
""", {"proj_id": project_id})

# Make small adjustments to format
ontology = res_str['project']['ontology']['normalized']
ontology = re.sub(r'(\'featureSchemaId\'|\'schemaNodeId\'): \'\w*\',', '', str(ontology))
ontology = ontology.replace("False", "false")

# Python dict -> JSON format (or use json.dumps())
ontology = ontology.replace("\'", "\"")

return ontology

def CreateProject(ontology, project_name):
project = client.create_project(name=project_name)

frontends = client.get_labeling_frontends()
# ImageEditor : 1 STRONGLY RECOMMENDED
# LegacyEditor: 0
project.labeling_frontend.connect(list(frontends)[1])
project.setup(list(frontends)[1], ontology)
return project

# View all ways to create data rows in API documentation
# https://labelbox.com/docs/python-api/data-rows
def Create_Dataset_From_Remote_Images(project, dataset_name, images):
dataset = client.create_dataset(name=dataset_name, projects=project)
mapped_images = list(map(lambda image: { schema.data_row.DataRow.row_data: image.url,schema.data_row.DataRow.external_id: image.external_id }, images))
dataset.create_data_rows(mapped_images)
return dataset


# Metadata can be video, text or image
def Asset_Text_Metadata(dataset, external_id, text):
dataset = client.get_dataset(dataset.uid)
data_row = dataset.data_row_for_external_id(external_id)
data_row.create_metadata("TEXT", text)

def Get_Labels(project_id):
# Query for Label Information
# - Limit of Labels to Query for is 100, a skip function can also be used
# - labels(first:5, skip: 10)
# - Will grab Labels 10-15
res_str = client.execute("""
query APIGetPageOfLabels ($Project_ID : ID!){
project(where:{id: $Project_ID}) {
labels(first: 5){
id
label
createdBy{
id
email }
type {
id
name }
secondsToLabel
agreement
dataRow {
id
rowData
}
}
}
}
""",{"Project_ID": project_id})
# Print Label infromation
print(res_str['project']['labels'])

info = [(label['id'], label['createdBy']['email'], label['secondsToLabel']) for label in res_str['project']['labels']]
print(info)

def Export_Project(project):
url = project.export_labels()
print(url)


def Get_Example_Ontology():
return """
{
"tools": [
{
"required": false,
"name": "Test Polygon ",
"color": "#FF0000",
"tool": "polygon",
"classifications": [
{
"name": "nested_classification",
"instructions": "Nested Classification",
"type": "radio",
"options": [
{
"value": "option_1",
"label": "Option 1"
},
{
"value": "option_2_",
"label": "Option 2 "
}
],
"required": false
}
]
}
],
"classifications": [
{
"name": "test_classification",
"instructions": "Test Classification",
"type": "radio",
"options": [
{
"value": "yes",
"label": "Yes"
},
{
"value": "no",
"label": "No"
}
],
"required": false
}
]
}
"""

class External_Image:
def __init__(self, url, external_id = None):
self.url = url
self.external_id = external_id

if __name__ == '__main__':
client = Client()

### CHANGE THESE VARIABLES
test_project_name = "Test Project"
test_dataset_name = "Astronomy Dataset"
test_images = [
External_Image("https://www.universetoday.com/wp-content/uploads/2009/09/bluemarble-e1452178366615.jpg","earth_image"),
External_Image("http://www.astronomytrek.com/wp-content/uploads/2017/09/Crab-Nebula.jpg", "crab_nebula")
]
test_annotation_label = "earth_image"
test_annotation_text = "A photo of Earth"



# Choose either copying ontology from project or using example
# Comment out whichever isn't used
# ontology = Copy_Ontology("Explore the example project")
ontology = Get_Example_Ontology()

project = CreateProject(ontology, test_project_name)
dataset = Create_Dataset_From_Remote_Images(project, test_dataset_name, test_images)
Asset_Text_Metadata(dataset, test_annotation_label, test_annotation_text)

input("Create label(s) and then press enter")
Get_Labels(project.uid)
Export_Project(project)

Was this page helpful?

Creating your first project

Projects

Contact