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
import json

def CreateProject(ontology):
# Create Project
project = client.create_project(name="Example Project")

# Create New Dataset
dataset = client.create_dataset(name="Test_Dataset1", projects=project)

# Create DataRows
DataRow = dataset.create_data_rows(["/Path/to/Data/ExampleImage.jpg"])

# Find Frontends or Editor
frontends = client.get_labeling_frontends()
# ImageEditor : 1
# LegacyEditor: 0
project.labeling_frontend.connect(list(frontends)[1])
project.setup(list(frontends)[1], ontology)
return(dataset)

def Ontology_Configuration():
# Get Ontology From Sample Project

# Get all projects
projects = client.get_projects()

# Find "Sample Project" uid
for project in projects:
if project.name == "Sample Project":
project_id = project.uid

# 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})

# Removing any instance of featureSchemaId and schemaNodeId
ontology = res_str['project']['ontology']['normalized']
ontology = re.sub(r'(\'featureSchemaId\'|\'schemaNodeId\'): \'\w*\',', '', str(ontology))

# Python boolean -> graphQL boolean
ontology = ontology.replace("False", "false")

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

# Example Ontology
ontologyEx = """
{
"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
}
]
}
"""
return ontology


def Asset_Overlay(dataset):

# Asset Overlay Metadata
dataset = client.get_dataset(dataset.uid)
# Find DataRow in dataSet based in External ID
data_row = dataset.data_row_for_external_id('<External_ ID>')

# Mutation to assign DataRow with Meta data
# - Metadata can be video, text or image
# - Can have multiple Metadata assets per DataRow
res_str = client.execute("""
mutation addAssetInfo ($data_row: ID!) {
createAssetMetadata(
data: {
dataRowId: $data_row,
metaValue:"This is textual asset information.",
metaType: TEXT
}
) {
id
}
}
""", {"data_row": data_row.uid})

# AssetData through JSON
# Completed through Cloud server, with External ID, Image URl, and info of Metadata
# Optimal way to upload a dataset with MetaData
Asset_Info_JSON = [
{
"externalId": "2017-Tesla-Model-S-P90D-102.jpg",
"imageUrl": "https://storage.googleapis.com/labelbox-example-datasets/tesla/2017-Tesla-Model-S-P90D-102.jpg",
"info": {
"type": "IMAGE",
"value": "https://storage.googleapis.com/labelbox-example-datasets/tesla/2017-Tesla-Model-S-P90D-102.jpg"
}
}]

def Export_Labels():
# Assign Project ID
ProjectID = "EXAMPLE 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": ProjectID})
# Print Label infromation
print(res_str['project']['labels'])
print(res_str['project']['createdBy']['email'])
print(res_str['project']['secondsToLabel'])

def Export_Project():
ProjectID = "PROJECT ID"
# Sets up an export that can be downloaded through UI
res_str = client.execute("""
mutation ($Project_ID : ID!) {
exportLabels(data:{
projectId: $Project_ID}){
downloadUrl
createdAt
shouldPoll
}
}""", {'Project_ID': ProjectID})

if __name__ == '__main__':
client = Client("INSERT API KEY IN QUOTATIONS")
ontology = Ontology_Configuration()
DataSet = CreateProject(ontology)
# Asset_Overlay(DataSet)
# Export_Labels()
# Export_Project()

Was this page helpful?

Creating your first project

Projects

Contact