Skip to content

Commit 1aa9984

Browse files
Fix/interactive segmentation
2 parents c1b7fa6 + 4bd9713 commit 1aa9984

File tree

2 files changed

+71
-33
lines changed

2 files changed

+71
-33
lines changed

examples/image_segmentation/python/image_segmentation.ipynb

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@
5454
"cell_type": "code",
5555
"execution_count": null,
5656
"metadata": {
57-
"id": "gxbHBsF-8Y_l"
57+
"colab": {
58+
"base_uri": "https://localhost:8080/"
59+
},
60+
"id": "gxbHBsF-8Y_l",
61+
"outputId": "6077cc85-2400-4970-be66-8a18d61e6151"
5862
},
5963
"outputs": [],
6064
"source": [
@@ -81,7 +85,7 @@
8185
},
8286
{
8387
"cell_type": "code",
84-
"execution_count": null,
88+
"execution_count": 2,
8589
"metadata": {
8690
"id": "OMjuVQiDYJKF"
8791
},
@@ -104,7 +108,7 @@
104108
},
105109
{
106110
"cell_type": "code",
107-
"execution_count": null,
111+
"execution_count": 3,
108112
"metadata": {
109113
"id": "tzXuqyIBlXer"
110114
},
@@ -141,7 +145,12 @@
141145
"cell_type": "code",
142146
"execution_count": null,
143147
"metadata": {
144-
"id": "8rjHk72-lmHX"
148+
"colab": {
149+
"base_uri": "https://localhost:8080/",
150+
"height": 514
151+
},
152+
"id": "8rjHk72-lmHX",
153+
"outputId": "188b495f-3de7-41b8-8869-30f6b5ab33bf"
145154
},
146155
"outputs": [],
147156
"source": [
@@ -184,7 +193,12 @@
184193
"cell_type": "code",
185194
"execution_count": null,
186195
"metadata": {
187-
"id": "Yl_Oiye4mUuo"
196+
"colab": {
197+
"base_uri": "https://localhost:8080/",
198+
"height": 514
199+
},
200+
"id": "Yl_Oiye4mUuo",
201+
"outputId": "039d2f20-899d-4cb2-baf3-7c6d368f9675"
188202
},
189203
"outputs": [],
190204
"source": [
@@ -224,8 +238,9 @@
224238
" bg_image = np.zeros(image_data.shape, dtype=np.uint8)\n",
225239
" bg_image[:] = BG_COLOR\n",
226240
"\n",
227-
" condition = np.stack((category_mask.numpy_view(),) * 3, axis=-1) > 0.2\n",
228-
" output_image = np.where(condition, fg_image, bg_image)\n",
241+
" condition = category_mask.numpy_view().squeeze(-1) > 0.2\n",
242+
"\n",
243+
" output_image = np.where(condition[...,None], fg_image, bg_image)\n",
229244
"\n",
230245
" print(f'Segmentation mask of {name}:')\n",
231246
" resize_and_show(output_image)"
@@ -244,7 +259,12 @@
244259
"cell_type": "code",
245260
"execution_count": null,
246261
"metadata": {
247-
"id": "ciCGwCQ3gNDc"
262+
"colab": {
263+
"base_uri": "https://localhost:8080/",
264+
"height": 514
265+
},
266+
"id": "ciCGwCQ3gNDc",
267+
"outputId": "fe0fc953-af57-4a0e-f7c1-a1727d588862"
248268
},
249269
"outputs": [],
250270
"source": [
@@ -268,21 +288,13 @@
268288
"\n",
269289
" # Apply effects\n",
270290
" blurred_image = cv2.GaussianBlur(image_data, (55,55), 0)\n",
271-
" condition = np.stack((category_mask.numpy_view(),) * 3, axis=-1) > 0.1\n",
272-
" output_image = np.where(condition, image_data, blurred_image)\n",
291+
" condition = category_mask.numpy_view().squeeze(-1) > 0.1\n",
292+
"\n",
293+
" output_image = np.where(condition[...,None], image_data, blurred_image)\n",
273294
"\n",
274295
" print(f'Blurred background of {image_file_name}:')\n",
275296
" resize_and_show(output_image)"
276297
]
277-
},
278-
{
279-
"cell_type": "code",
280-
"execution_count": null,
281-
"metadata": {
282-
"id": "8evBYbOZk6u4"
283-
},
284-
"outputs": [],
285-
"source": []
286298
}
287299
],
288300
"metadata": {

examples/interactive_segmentation/python/interactive_segmenter.ipynb

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@
4545
"cell_type": "code",
4646
"execution_count": null,
4747
"metadata": {
48-
"id": "gxbHBsF-8Y_l"
48+
"colab": {
49+
"base_uri": "https://localhost:8080/"
50+
},
51+
"id": "gxbHBsF-8Y_l",
52+
"outputId": "6811bf9d-8d70-4311-878e-165a3742b65b"
4953
},
5054
"outputs": [],
5155
"source": [
@@ -72,7 +76,7 @@
7276
},
7377
{
7478
"cell_type": "code",
75-
"execution_count": null,
79+
"execution_count": 2,
7680
"metadata": {
7781
"id": "OMjuVQiDYJKF"
7882
},
@@ -102,7 +106,7 @@
102106
},
103107
{
104108
"cell_type": "code",
105-
"execution_count": null,
109+
"execution_count": 3,
106110
"metadata": {
107111
"id": "9gYTyXhTKy3H"
108112
},
@@ -142,7 +146,7 @@
142146
},
143147
{
144148
"cell_type": "code",
145-
"execution_count": null,
149+
"execution_count": 4,
146150
"metadata": {
147151
"id": "tzXuqyIBlXer"
148152
},
@@ -178,7 +182,12 @@
178182
"cell_type": "code",
179183
"execution_count": null,
180184
"metadata": {
181-
"id": "8rjHk72-lmHX"
185+
"colab": {
186+
"base_uri": "https://localhost:8080/",
187+
"height": 274
188+
},
189+
"id": "8rjHk72-lmHX",
190+
"outputId": "5269c698-f813-445b-b802-eae563a0d270"
182191
},
183192
"outputs": [],
184193
"source": [
@@ -220,7 +229,7 @@
220229
},
221230
{
222231
"cell_type": "code",
223-
"execution_count": null,
232+
"execution_count": 6,
224233
"metadata": {
225234
"id": "hl6ojNjl4i6o"
226235
},
@@ -234,7 +243,12 @@
234243
"cell_type": "code",
235244
"execution_count": null,
236245
"metadata": {
237-
"id": "Yl_Oiye4mUuo"
246+
"colab": {
247+
"base_uri": "https://localhost:8080/",
248+
"height": 274
249+
},
250+
"id": "Yl_Oiye4mUuo",
251+
"outputId": "48829df3-56d8-4766-b012-4f458c11e7a4"
238252
},
239253
"outputs": [],
240254
"source": [
@@ -254,7 +268,7 @@
254268
"\n",
255269
"# Create the options that will be used for InteractiveSegmenter\n",
256270
"base_options = python.BaseOptions(model_asset_path='model.tflite')\n",
257-
"options = vision.ImageSegmenterOptions(base_options=base_options,\n",
271+
"options = vision.InteractiveSegmenterOptions(base_options=base_options,\n",
258272
" output_category_mask=True)\n",
259273
"\n",
260274
"# Create the interactive segmenter\n",
@@ -279,8 +293,8 @@
279293
" bg_image = np.zeros(image_data.shape, dtype=np.uint8)\n",
280294
" bg_image[:] = BG_COLOR\n",
281295
"\n",
282-
" condition = np.stack((category_mask.numpy_view(),) * 3, axis=-1) > 0.1\n",
283-
" output_image = np.where(condition, fg_image, bg_image)\n",
296+
" condition = category_mask.numpy_view().squeeze(-1) > 0.1\n",
297+
" output_image = np.where(condition[..., None], fg_image, bg_image)\n",
284298
"\n",
285299
" # Draw a white dot with black border to denote the point of interest\n",
286300
" thickness, radius = 6, -1\n",
@@ -305,7 +319,12 @@
305319
"cell_type": "code",
306320
"execution_count": null,
307321
"metadata": {
308-
"id": "ciCGwCQ3gNDc"
322+
"colab": {
323+
"base_uri": "https://localhost:8080/",
324+
"height": 274
325+
},
326+
"id": "ciCGwCQ3gNDc",
327+
"outputId": "8d903a0d-11b6-48a3-f85c-b2ee1c2eea65"
309328
},
310329
"outputs": [],
311330
"source": [
@@ -332,7 +351,8 @@
332351
" # Apply effects\n",
333352
" blurred_image = cv2.GaussianBlur(image_data, (55,55), 0)\n",
334353
" condition = np.stack((category_mask.numpy_view(),) * 3, axis=-1) > 0.1\n",
335-
" output_image = np.where(condition, image_data, blurred_image)\n",
354+
" condition = category_mask.numpy_view().squeeze(-1) > 0.1\n",
355+
" output_image = np.where(condition[..., None], image_data, blurred_image)\n",
336356
"\n",
337357
" # Draw a white dot with black border to denote the point of interest\n",
338358
" thickness, radius = 6, -1\n",
@@ -357,7 +377,12 @@
357377
"cell_type": "code",
358378
"execution_count": null,
359379
"metadata": {
360-
"id": "szFMEFuymrHd"
380+
"colab": {
381+
"base_uri": "https://localhost:8080/",
382+
"height": 274
383+
},
384+
"id": "szFMEFuymrHd",
385+
"outputId": "6fdc6af9-dc91-4b67-eea6-d23fee03837a"
361386
},
362387
"outputs": [],
363388
"source": [
@@ -386,7 +411,8 @@
386411
" overlay_image[:] = OVERLAY_COLOR\n",
387412
"\n",
388413
" # Create the condition from the category_masks array\n",
389-
" alpha = np.stack((category_mask.numpy_view(),) * 3, axis=-1) > 0.1\n",
414+
" alpha = category_mask.numpy_view().squeeze(-1) > 0.1\n",
415+
" alpha = alpha[..., None]\n",
390416
"\n",
391417
" # Create an alpha channel from the condition with the desired opacity (e.g., 0.7 for 70%)\n",
392418
" alpha = alpha.astype(float) * 0.7\n",

0 commit comments

Comments
 (0)