Skip to content

Commit da291e5

Browse files
authored
Bytes array, multiple annotations per frame SN-84,SN-85 (#1286)
1 parent 371dcc7 commit da291e5

File tree

2 files changed

+308
-156
lines changed

2 files changed

+308
-156
lines changed

examples/annotation_import/image.ipynb

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
"metadata": {},
8686
"source": [
8787
"import uuid\n",
88+
"import requests\n",
89+
"import base64\n",
8890
"import numpy as np\n",
8991
"import labelbox as lb\n",
9092
"import labelbox.types as lb_types"
@@ -297,7 +299,7 @@
297299
"bbox_annotation = lb_types.ObjectAnnotation(\n",
298300
" name=\"bounding_box\", # must match your ontology feature\"s name\n",
299301
" value=lb_types.Rectangle(\n",
300-
" start=lb_types.Point(x=1690, y=977), # x = left, y = top \n",
302+
" start=lb_types.Point(x=1690, y=977), # x = left, y = top\n",
301303
" end=lb_types.Point(x=1915, y=1307), # x= left + width , y = top + height\n",
302304
" ))\n",
303305
"\n",
@@ -330,7 +332,7 @@
330332
"bbox_with_radio_subclass_annotation = lb_types.ObjectAnnotation(\n",
331333
" name=\"bbox_with_radio_subclass\",\n",
332334
" value=lb_types.Rectangle(\n",
333-
" start=lb_types.Point(x=541, y=933), # x = left, y = top \n",
335+
" start=lb_types.Point(x=541, y=933), # x = left, y = top\n",
334336
" end=lb_types.Point(x=871, y=1124), # x= left + width , y = top + height\n",
335337
" ),\n",
336338
" classifications=[\n",
@@ -373,7 +375,7 @@
373375
"source": [
374376
"# Python annotation\n",
375377
"polygon_annotation = lb_types.ObjectAnnotation(\n",
376-
" name=\"polygon\", # must match your ontology feature\"s name \n",
378+
" name=\"polygon\", # must match your ontology feature\"s name\n",
377379
" value=lb_types.Polygon( # Coordinates for the vertices of your polygon\n",
378380
" points=[\n",
379381
" lb_types.Point(x=1489.581, y=183.934),\n",
@@ -430,23 +432,25 @@
430432
{
431433
"metadata": {},
432434
"source": [
433-
"# Identifying what values in the numpy array correspond to the mask annotation\n",
434-
"color = (255, 255, 255)\n",
435-
"mask_data = lb_types.MaskData(url=\"https://storage.googleapis.com/labelbox-datasets/image_sample_data/raster_seg.png\")\n",
435+
"### Raster Segmentation (Byte string array)\n",
436+
"url = \"https://storage.googleapis.com/labelbox-datasets/image_sample_data/raster_seg.png\"\n",
437+
"response = requests.get(url)\n",
436438
"\n",
437-
"# Python annotation\n",
439+
"mask_data = lb.types.MaskData(im_bytes=response.content) # You can also use \"url\" instead of img_bytes to pass the PNG mask url.\n",
438440
"mask_annotation = lb_types.ObjectAnnotation(\n",
439-
" name = \"mask\", # must match your ontology feature\"s name\n",
440-
" value=lb_types.Mask(mask=mask_data, color=color),\n",
441+
" name=\"mask\",\n",
442+
" value=lb_types.Mask(\n",
443+
" mask=mask_data,\n",
444+
" color=(255, 255, 255))\n",
441445
")\n",
442446
"\n",
443-
"# NDJSON\n",
447+
"# NDJSON using instanceURI, bytes array is not fully supported.\n",
444448
"mask_annotation_ndjson = {\n",
445449
" \"name\": \"mask\",\n",
446450
" \"classifications\": [],\n",
447-
" \"mask\": {\"instanceURI\": \"https://storage.googleapis.com/labelbox-datasets/image_sample_data/raster_seg.png\",\n",
451+
" \"mask\": {\"instanceURI\": url,\n",
448452
" \"colorRGB\": (255, 255, 255)}\n",
449-
"}"
453+
"}\n"
450454
],
451455
"cell_type": "code",
452456
"outputs": [],
@@ -591,7 +595,7 @@
591595
" type=lb_types.Relationship.Type.UNIDIRECTIONAL,\n",
592596
" ))\n",
593597
"\n",
594-
"## Only supported for MAL imports \n",
598+
"## Only supported for MAL imports\n",
595599
"uuid_source = str(uuid.uuid4())\n",
596600
"uuid_target = str(uuid.uuid4())\n",
597601
"\n",
@@ -855,7 +859,7 @@
855859
{
856860
"metadata": {},
857861
"source": [
858-
"ndjson_label = []\n",
862+
"label_ndjson = []\n",
859863
"annotations = [\n",
860864
" radio_annotation_ndjson,\n",
861865
" nested_radio_annotation_ndjson,\n",
@@ -870,15 +874,15 @@
870874
" polyline_annotation_ndjson,\n",
871875
" bbox_source_ndjson,\n",
872876
" bbox_target_ndjson,\n",
873-
" relationship_ndjson, ## Only supported for MAL imports \n",
877+
" relationship_ndjson, ## Only supported for MAL imports\n",
874878
"]\n",
875879
"for annotation in annotations:\n",
876880
" annotation.update({\n",
877881
" \"dataRow\": {\n",
878882
" \"globalKey\": global_key\n",
879-
" },\n",
883+
" }\n",
880884
" })\n",
881-
" ndjson_label.append(annotation)"
885+
" label_ndjson.append(annotation)"
882886
],
883887
"cell_type": "code",
884888
"outputs": [],
@@ -911,7 +915,7 @@
911915
")\n",
912916
"upload_job.wait_until_done()\n",
913917
"\n",
914-
"print(f\"Errors: {upload_job.errors}\", )\n",
918+
"print(f\"Errors: {upload_job.errors}\")\n",
915919
"print(f\"Status of uploads: {upload_job.statuses}\")"
916920
],
917921
"cell_type": "code",
@@ -928,14 +932,14 @@
928932
{
929933
"metadata": {},
930934
"source": [
931-
"# Uncomment if relationships are not being imported. \n",
932-
"# Relationships will be supported for label import in the near future. \n",
935+
"# Relationships are not supported with LabelImport\n",
936+
"# For this demo either run MAL or Ground Truth, not both\n",
933937
"\n",
934938
"# Upload label for this data row in project\n",
935939
"# upload_job = lb.LabelImport.create_from_objects(\n",
936-
"# client = client, \n",
937-
"# project_id = project.uid, \n",
938-
"# name=\"label_import_job\"+str(uuid.uuid4()), \n",
940+
"# client = client,\n",
941+
"# project_id = project.uid,\n",
942+
"# name=\"label_import_job\"+str(uuid.uuid4()),\n",
939943
"# labels=label)\n",
940944
"\n",
941945
"# print(\"Errors:\", upload_job.errors)\n",
@@ -954,6 +958,13 @@
954958
"cell_type": "code",
955959
"outputs": [],
956960
"execution_count": null
961+
},
962+
{
963+
"metadata": {},
964+
"source": [],
965+
"cell_type": "code",
966+
"outputs": [],
967+
"execution_count": null
957968
}
958969
]
959970
}

0 commit comments

Comments
 (0)