Skip to content

Commit 693d22c

Browse files
🎨 Cleaned
1 parent 3ea9cca commit 693d22c

File tree

1 file changed

+63
-199
lines changed

1 file changed

+63
-199
lines changed
Lines changed: 63 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
{
2+
"nbformat": 4,
3+
"nbformat_minor": 2,
4+
"metadata": {},
25
"cells": [
36
{
4-
"cell_type": "markdown",
57
"metadata": {},
68
"source": [
7-
"<td>\n",
8-
" <a target=\"_blank\" href=\"https://labelbox.com\" ><img src=\"https://labelbox.com/blog/content/images/2021/02/logo-v4.svg\" width=256/></a>\n",
9+
"<td>",
10+
" <a target=\"_blank\" href=\"https://labelbox.com\" ><img src=\"https://labelbox.com/blog/content/images/2021/02/logo-v4.svg\" width=256/></a>",
911
"</td>\n"
10-
]
12+
],
13+
"cell_type": "markdown"
1114
},
1215
{
13-
"cell_type": "markdown",
1416
"metadata": {},
1517
"source": [
1618
"<td>\n",
@@ -22,10 +24,10 @@
2224
"<a href=\"https://github.com/Labelbox/labelbox-python/tree/develop/examples/exports/composite_mask_export.ipynb\" target=\"_blank\"><img\n",
2325
"src=\"https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white\" alt=\"GitHub\"></a>\n",
2426
"</td>"
25-
]
27+
],
28+
"cell_type": "markdown"
2629
},
2730
{
28-
"cell_type": "markdown",
2931
"metadata": {},
3032
"source": [
3133
"# Export composite masks \n",
@@ -34,63 +36,53 @@
3436
"Composite masks are a combination of mask instances grouped in a single mask URL. \n",
3537
"\n",
3638
"The purpose of this demo is to demonstrate how to transition from exporting single masks to exporting composite masks. "
37-
]
39+
],
40+
"cell_type": "markdown"
3841
},
3942
{
40-
"cell_type": "markdown",
4143
"metadata": {},
4244
"source": [
4345
"## Set up"
44-
]
46+
],
47+
"cell_type": "markdown"
4548
},
4649
{
47-
"cell_type": "code",
48-
"execution_count": null,
4950
"metadata": {},
51+
"source": "%pip install -q --upgrade \"labelbox[data]\"",
52+
"cell_type": "code",
5053
"outputs": [],
51-
"source": [
52-
"%pip install -q --upgrade \"labelbox[data]\""
53-
]
54+
"execution_count": null
5455
},
5556
{
56-
"cell_type": "code",
57-
"execution_count": null,
5857
"metadata": {},
58+
"source": "import labelbox as lb\nimport urllib.request\nfrom PIL import Image\nimport json",
59+
"cell_type": "code",
5960
"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
6662
},
6763
{
68-
"cell_type": "markdown",
6964
"metadata": {},
7065
"source": [
7166
"## API key and client\n",
7267
"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."
73-
]
68+
],
69+
"cell_type": "markdown"
7470
},
7571
{
76-
"cell_type": "code",
77-
"execution_count": null,
7872
"metadata": {},
73+
"source": "API_KEY = None\nclient = lb.Client(api_key=API_KEY)",
74+
"cell_type": "code",
7975
"outputs": [],
80-
"source": [
81-
"API_KEY = None\n",
82-
"client = lb.Client(api_key=API_KEY)"
83-
]
76+
"execution_count": null
8477
},
8578
{
86-
"cell_type": "markdown",
8779
"metadata": {},
8880
"source": [
8981
"## Key differences between single mask instance and composite mask."
90-
]
82+
],
83+
"cell_type": "markdown"
9184
},
9285
{
93-
"cell_type": "markdown",
9486
"metadata": {},
9587
"source": [
9688
"### Composite masks\n",
@@ -125,31 +117,17 @@
125117
" }\n",
126118
"```\n",
127119
"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"
129122
},
130123
{
131-
"cell_type": "code",
132-
"execution_count": null,
133124
"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",
134127
"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",
138-
"task_id = \"\"\n",
139-
"composite_mask_id = \"\"\n",
140-
"\n",
141-
"mask_url = f\"https://api.labelbox.com/api/v1/tasks/{task_id}/masks/{composite_mask_id}/index/1\"\n",
142-
"req = urllib.request.Request(mask_url, headers=client.headers)\n",
143-
"image = Image.open(urllib.request.urlopen(req))\n",
144-
"w, h = image.size\n",
145-
"new_w = w // 4\n",
146-
"new_h = h // 4\n",
147-
"\n",
148-
"image.resize((new_w, new_h), Image.BICUBIC)"
149-
]
128+
"execution_count": null
150129
},
151130
{
152-
"cell_type": "markdown",
153131
"metadata": {},
154132
"source": [
155133
"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 @@
173151
" }\n",
174152
"```\n",
175153
"- rgb(123,103,152) = Purple\n"
176-
]
154+
],
155+
"cell_type": "markdown"
177156
},
178157
{
179-
"cell_type": "markdown",
180158
"metadata": {},
181159
"source": [
182160
"### Single mask instance:\n",
@@ -191,193 +169,79 @@
191169
"\n",
192170
"```\n",
193171
"3. RGB color is not present"
194-
]
172+
],
173+
"cell_type": "markdown"
195174
},
196175
{
197-
"cell_type": "markdown",
198176
"metadata": {},
199177
"source": [
200178
"## Create an export from a project with mask annotations\n",
201179
"To better showcase composite masks, make sure you have different mask tools and mask annotations in your project"
202-
]
180+
],
181+
"cell_type": "markdown"
203182
},
204183
{
205-
"cell_type": "code",
206-
"execution_count": null,
207184
"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",
208187
"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
214189
},
215190
{
216-
"cell_type": "code",
217-
"execution_count": null,
218191
"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",
219194
"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",
234-
"# https://docs.labelbox.com/reference/label-export#export-v2-methods\n",
235-
"\n",
236-
"export_task = project.export(params=export_params, filters=filters)\n",
237-
"export_task.wait_till_done()\n",
238-
"\n",
239-
"if export_task.has_result():\n",
240-
" print(export_task.result)"
241-
]
195+
"execution_count": null
242196
},
243197
{
244-
"cell_type": "markdown",
245198
"metadata": {},
246199
"source": [
247200
"#### Get all the ```color_rgb``` associated with annotations that are using a specific mask tool "
248-
]
201+
],
202+
"cell_type": "markdown"
249203
},
250204
{
251-
"cell_type": "code",
252-
"execution_count": null,
253205
"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",
254208
"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",
265-
" project_labels = (output_json[\"projects\"].get(PROJECT_ID,\n",
266-
" {}).get(\"labels\", []))\n",
267-
"\n",
268-
" # Iterate through each label\n",
269-
" for label in project_labels:\n",
270-
" # Get the list of annotations (objects) for the label\n",
271-
" annotations = label[\"annotations\"].get(\"objects\", [])\n",
272-
"\n",
273-
" # Iterate through each annotation\n",
274-
" for annotation in annotations:\n",
275-
" # Check if the annotation is of type \"ImageSegmentationMask\"\n",
276-
" if annotation.get(\"annotation_kind\") == \"ImageSegmentationMask\":\n",
277-
" # Add the color RGB information to the mapping dictionary\n",
278-
" mask_tool_rgb_mapping.setdefault(annotation[\"name\"], []).append(\n",
279-
" annotation[\"composite_mask\"][\"color_rgb\"])\n",
280-
"\n",
281-
"print(mask_tool_rgb_mapping)"
282-
]
209+
"execution_count": null
283210
},
284211
{
285-
"cell_type": "markdown",
286212
"metadata": {},
287213
"source": [
288214
"## Create an export from a Video project with mask annotations "
289-
]
215+
],
216+
"cell_type": "markdown"
290217
},
291218
{
292-
"cell_type": "code",
293-
"execution_count": null,
294219
"metadata": {},
220+
"source": "VIDEO_PROJECT_ID = \"\"\nproject_video = client.get_project(VIDEO_PROJECT_ID)",
221+
"cell_type": "code",
295222
"outputs": [],
296-
"source": [
297-
"VIDEO_PROJECT_ID = \"\"\n",
298-
"project_video = client.get_project(VIDEO_PROJECT_ID)"
299-
]
223+
"execution_count": null
300224
},
301225
{
302-
"cell_type": "code",
303-
"execution_count": null,
304226
"metadata": {},
227+
"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_video = project_video.export(params=export_params, filters=filters)\nexport_task_video.wait_till_done()\n\nif export_task_video.has_result():\n print(export_task_video.result)",
228+
"cell_type": "code",
305229
"outputs": [],
306-
"source": [
307-
"export_params = {\n",
308-
" \"attachments\": True,\n",
309-
" \"metadata_fields\": True,\n",
310-
" \"data_row_details\": True,\n",
311-
" \"project_details\": True,\n",
312-
" \"label_details\": True,\n",
313-
" \"performance_details\": True,\n",
314-
" \"interpolated_frames\": True,\n",
315-
"}\n",
316-
"\n",
317-
"filters = {}\n",
318-
"\n",
319-
"# export() is the streamable option of exports V2, for more information please visit our documentation:\n",
320-
"# https://docs.labelbox.com/reference/label-export#export-v2-methods\n",
321-
"\n",
322-
"export_task_video = project_video.export(params=export_params, filters=filters)\n",
323-
"export_task_video.wait_till_done()\n",
324-
"\n",
325-
"if export_task_video.has_result():\n",
326-
" print(export_task_video.result)"
327-
]
230+
"execution_count": null
328231
},
329232
{
330-
"cell_type": "markdown",
331233
"metadata": {},
332234
"source": [
333235
"#### Get all the ```color_rgb``` associated with annotations that are using a specific mask tool from each frame"
334-
]
236+
],
237+
"cell_type": "markdown"
335238
},
336239
{
337-
"cell_type": "code",
338-
"execution_count": null,
339240
"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)",
242+
"cell_type": "code",
340243
"outputs": [],
341-
"source": [
342-
"tools_frames_color = {}\n",
343-
"stream = export_task_video.get_buffered_stream()\n",
344-
"\n",
345-
"# Iterate over each output in the stream\n",
346-
"for output in stream:\n",
347-
" output_json = output.json\n",
348-
"\n",
349-
" # Iterate over the labels in the specific project\n",
350-
" for dr in output_json[\"projects\"][VIDEO_PROJECT_ID][\"labels\"]:\n",
351-
" frames_data = dr[\"annotations\"][\"frames\"]\n",
352-
"\n",
353-
" # Iterate over each frame in the frames data\n",
354-
" for frame_key, frame_value in frames_data.items():\n",
355-
"\n",
356-
" # Iterate over each annotation in the frame\n",
357-
" for annotation_key, annotation_value in frame_value.items():\n",
358-
" if \"objects\" in annotation_key and annotation_value.values():\n",
359-
"\n",
360-
" # Iterate over each object in the annotation\n",
361-
" for object_key, object_value in annotation_value.items():\n",
362-
" if (object_value[\"annotation_kind\"] ==\n",
363-
" \"VideoSegmentationMask\"):\n",
364-
" # Update tools_frames_color with object information\n",
365-
" tools_frames_color.setdefault(\n",
366-
" object_value[\"name\"], []).append({\n",
367-
" frame_key:\n",
368-
" object_value[\"composite_mask\"]\n",
369-
" [\"color_rgb\"]\n",
370-
" })\n",
371-
"\n",
372-
"print(tools_frames_color)"
373-
]
374-
}
375-
],
376-
"metadata": {
377-
"language_info": {
378-
"name": "python"
244+
"execution_count": null
379245
}
380-
},
381-
"nbformat": 4,
382-
"nbformat_minor": 2
383-
}
246+
]
247+
}

0 commit comments

Comments
 (0)