You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"source": "import labelbox as lb\nimport urllib.request\nfrom PIL import Image\nimport json",
59
+
"cell_type": "code",
59
60
"outputs": [],
60
-
"source": [
61
-
"import labelbox as lb\n",
62
-
"import urllib.request\n",
63
-
"from PIL import Image\n",
64
-
"import json"
65
-
]
61
+
"execution_count": null
66
62
},
67
63
{
68
-
"cell_type": "markdown",
69
64
"metadata": {},
70
65
"source": [
71
66
"## API key and client\n",
72
67
"Provide a valid API key below in order to properly connect to the Labelbox client. Please review [Create API key guide](https://docs.labelbox.com/reference/create-api-key) for more information."
"## Key differences between single mask instance and composite mask."
90
-
]
82
+
],
83
+
"cell_type": "markdown"
91
84
},
92
85
{
93
-
"cell_type": "markdown",
94
86
"metadata": {},
95
87
"source": [
96
88
"### Composite masks\n",
@@ -125,31 +117,17 @@
125
117
" }\n",
126
118
"```\n",
127
119
"3. A unique RGB color is assigned to each mask instance. The example below shows a composite mask of a label, and while it contains all mask instances, only the RGB color associated with this particular annotation will be filled in under the ```color_rgb``` field."
128
-
]
120
+
],
121
+
"cell_type": "markdown"
129
122
},
130
123
{
131
-
"cell_type": "code",
132
-
"execution_count": null,
133
124
"metadata": {},
125
+
"source": "# Example on how to fetch a composite mask\n# The mask here shows all the mask instances associated with a label\ntask_id = \"\"\ncomposite_mask_id = \"\"\n\nmask_url = f\"https://api.labelbox.com/api/v1/tasks/{task_id}/masks/{composite_mask_id}/index/1\"\nreq = urllib.request.Request(mask_url, headers=client.headers)\nimage = Image.open(urllib.request.urlopen(req))\nw, h = image.size\nnew_w = w // 4\nnew_h = h // 4\n\nimage.resize((new_w, new_h), Image.BICUBIC)",
126
+
"cell_type": "code",
134
127
"outputs": [],
135
-
"source": [
136
-
"# Example on how to fetch a composite mask\n",
137
-
"# The mask here shows all the mask instances associated with a label\n",
"Here's an example of an entry featuring a composite mask (see image above) containing the mask instance's RGB color uniquely associated with the annotation.\n",
@@ -173,10 +151,10 @@
173
151
" }\n",
174
152
"```\n",
175
153
"- rgb(123,103,152) = Purple\n"
176
-
]
154
+
],
155
+
"cell_type": "markdown"
177
156
},
178
157
{
179
-
"cell_type": "markdown",
180
158
"metadata": {},
181
159
"source": [
182
160
"### Single mask instance:\n",
@@ -191,193 +169,79 @@
191
169
"\n",
192
170
"```\n",
193
171
"3. RGB color is not present"
194
-
]
172
+
],
173
+
"cell_type": "markdown"
195
174
},
196
175
{
197
-
"cell_type": "markdown",
198
176
"metadata": {},
199
177
"source": [
200
178
"## Create an export from a project with mask annotations\n",
201
179
"To better showcase composite masks, make sure you have different mask tools and mask annotations in your project"
202
-
]
180
+
],
181
+
"cell_type": "markdown"
203
182
},
204
183
{
205
-
"cell_type": "code",
206
-
"execution_count": null,
207
184
"metadata": {},
185
+
"source": "# Insert the project ID of the project from which you wish to export data rows.\nPROJECT_ID = \"\"\nproject = client.get_project(PROJECT_ID)",
186
+
"cell_type": "code",
208
187
"outputs": [],
209
-
"source": [
210
-
"# Insert the project ID of the project from which you wish to export data rows.\n",
211
-
"PROJECT_ID = \"\"\n",
212
-
"project = client.get_project(PROJECT_ID)"
213
-
]
188
+
"execution_count": null
214
189
},
215
190
{
216
-
"cell_type": "code",
217
-
"execution_count": null,
218
191
"metadata": {},
192
+
"source": "export_params = {\n\"attachments\": True,\n\"metadata_fields\": True,\n\"data_row_details\": True,\n\"project_details\": True,\n\"label_details\": True,\n\"performance_details\": True,\n\"interpolated_frames\": True,\n}\n\nfilters = {}\n\n# export() is the streamable option of exports V2, for more information please visit our documentation:\n# https://docs.labelbox.com/reference/label-export#export-v2-methods\n\nexport_task = project.export(params=export_params, filters=filters)\nexport_task.wait_till_done()\n\nif export_task.has_result():\n print(export_task.result)",
193
+
"cell_type": "code",
219
194
"outputs": [],
220
-
"source": [
221
-
"export_params = {\n",
222
-
"\"attachments\": True,\n",
223
-
"\"metadata_fields\": True,\n",
224
-
"\"data_row_details\": True,\n",
225
-
"\"project_details\": True,\n",
226
-
"\"label_details\": True,\n",
227
-
"\"performance_details\": True,\n",
228
-
"\"interpolated_frames\": True,\n",
229
-
"}\n",
230
-
"\n",
231
-
"filters = {}\n",
232
-
"\n",
233
-
"# export() is the streamable option of exports V2, for more information please visit our documentation:\n",
"#### Get all the ```color_rgb``` associated with annotations that are using a specific mask tool "
248
-
]
201
+
],
202
+
"cell_type": "markdown"
249
203
},
250
204
{
251
-
"cell_type": "code",
252
-
"execution_count": null,
253
205
"metadata": {},
206
+
"source": "stream = export_task.get_buffered_stream()\n\nmask_tool_rgb_mapping = {}\n\nfor output in stream:\n # Parse the JSON string from the output\n output_json = output.json\n\n # Get the labels for the specified project ID or an empty list if the project ID is not found\n project_labels = (output_json[\"projects\"].get(PROJECT_ID,\n {}).get(\"labels\", []))\n\n # Iterate through each label\n for label in project_labels:\n # Get the list of annotations (objects) for the label\n annotations = label[\"annotations\"].get(\"objects\", [])\n\n # Iterate through each annotation\n for annotation in annotations:\n # Check if the annotation is of type \"ImageSegmentationMask\"\n if annotation.get(\"annotation_kind\") == \"ImageSegmentationMask\":\n # Add the color RGB information to the mapping dictionary\n mask_tool_rgb_mapping.setdefault(annotation[\"name\"], []).append(\n annotation[\"composite_mask\"][\"color_rgb\"])\n\nprint(mask_tool_rgb_mapping)",
207
+
"cell_type": "code",
254
208
"outputs": [],
255
-
"source": [
256
-
"stream = export_task.get_buffered_stream()\n",
257
-
"\n",
258
-
"mask_tool_rgb_mapping = {}\n",
259
-
"\n",
260
-
"for output in stream:\n",
261
-
" # Parse the JSON string from the output\n",
262
-
" output_json = output.json\n",
263
-
"\n",
264
-
" # Get the labels for the specified project ID or an empty list if the project ID is not found\n",
"#### Get all the ```color_rgb``` associated with annotations that are using a specific mask tool from each frame"
334
-
]
236
+
],
237
+
"cell_type": "markdown"
335
238
},
336
239
{
337
-
"cell_type": "code",
338
-
"execution_count": null,
339
240
"metadata": {},
241
+
"source": "tools_frames_color = {}\nstream = export_task_video.get_buffered_stream()\n\n# Iterate over each output in the stream\nfor output in stream:\n output_json = output.json\n\n # Iterate over the labels in the specific project\n for dr in output_json[\"projects\"][VIDEO_PROJECT_ID][\"labels\"]:\n frames_data = dr[\"annotations\"][\"frames\"]\n\n # Iterate over each frame in the frames data\n for frame_key, frame_value in frames_data.items():\n\n # Iterate over each annotation in the frame\n for annotation_key, annotation_value in frame_value.items():\n if \"objects\" in annotation_key and annotation_value.values():\n\n # Iterate over each object in the annotation\n for object_key, object_value in annotation_value.items():\n if (object_value[\"annotation_kind\"] ==\n \"VideoSegmentationMask\"):\n # Update tools_frames_color with object information\n tools_frames_color.setdefault(\n object_value[\"name\"], []).append({\n frame_key:\n object_value[\"composite_mask\"]\n [\"color_rgb\"]\n })\n\nprint(tools_frames_color)",
0 commit comments