# How to (use API) post an image with segmentation mask in RLE format to label studio project?

**URL:** https://community.labelstud.io/t/how-to-use-api-post-an-image-with-segmentation-mask-in-rle-format-to-label-studio-project/248
**Category:** Label Studio Support
**Tags:** annotations, api
**Created:** [June 14, 2024, 2:08am UTC](https://community.labelstud.io/t/how-to-use-api-post-an-image-with-segmentation-mask-in-rle-format-to-label-studio-project/248 "2024-06-14T02:08:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Lee](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.labelstud.io/lee/32/73_2.png) [@Lee](https://community.labelstud.io/u/Lee)
#### Post date: [June 14, 2024, 2:08am UTC](https://community.labelstud.io/t/how-to-use-api-post-an-image-with-segmentation-mask-in-rle-format-to-label-studio-project/248/1 "2024-06-14T02:08:21Z")

</div>

I use the MobileSAM to automative generate mask and use the function call “image2annotation” from brush.py ([label-studio-converter/label\_studio\_converter/brush.py at master · HumanSignal/label-studio-converter · GitHub](https://github.com/HumanSignal/label-studio-converter/blob/master/label_studio_converter/brush.py)) in order to upload the image with MobileSAM mask. Show the step below:

This is an raw image:

 ![6](https://canada1.discourse-cdn.com/flex035/uploads/labelstudio/original/1X/3ab73d3e44f05925f3f2938560c437437f12e141.jpeg)

And the image with MobileSAM mask:

 ![output](https://canada1.discourse-cdn.com/flex035/uploads/labelstudio/original/1X/327f1e96b38825ec276256660982265bddc732b0.jpeg)

And the image upload to Label studio from API post:

 ![LS_MASK.PNG](https://canada1.discourse-cdn.com/flex035/uploads/labelstudio/original/1X/2cd9acb02c5786097f774e8f4b9b2d0e2a417d8b.jpeg)

We found the mask be weird.

Share my code:

### Use MobileSAM to generate mask on given picture

mask\_generator\_2 = SamAutomaticMaskGenerator(  
model=mobile\_sam,  
points\_per\_side=3, # 在每側生成32個點，形成一個32 x 32的網格，共1024個點  
pred\_iou\_thresh=1,  
stability\_score\_thresh=0.97,  
crop\_n\_layers=1,  
crop\_n\_points\_downscale\_factor=2,  
min\_mask\_region\_area=100, # Requires open-cv to run post-processing  
output\_mode = ‘binary\_mask’ # Can be ‘binary\_mask’,‘uncompressed\_rle’, or ‘coco\_rle’. ‘coco\_rle’ requires pycocotools.  
)  
masks2 = mask\_generator\_2.generate(image\_rgb)

### Stack the SAM mask on image

def export\_mask(anns, image\_rgb):  
if len(anns) == 0:  
return image\_rgb  
# anns = masks2  
sorted\_anns = sorted(anns, key=(lambda x: x[‘area’]), reverse=True)

```
img = np.ones((sorted_anns[0]['segmentation'].shape[0], sorted_anns[0]['segmentation'].shape[1], 4))
img[:,:,3] = 0
for ann in sorted_anns:
    m = ann['segmentation']
    color_mask = np.concatenate([np.random.random(3), [0.35]])
    img[m] = color_mask
# 將遮罩圖像疊加在原圖像上
combined_img = image_rgb.copy()
mask = img[:, :, 3] > 0
combined_img[mask] = combined_img[mask] * (1 - img[mask, 3, None]) + img[mask, :3] * img[mask, 3, None]
return combined_img
# return img

```

img = export\_mask(masks2, image\_rgb)  
if img is not None:  
plt.imsave(‘output.png’, img)

### Convert image with mask to RLE format and json format

from label\_studio\_converter.brush import image2annotation  
result = image2annotation(path=‘output.png’, label\_name=“Cat”, from\_name=‘tag’, to\_name=‘image’  
, model\_version=‘SamAutomaticMaskGenerator’, score=masks2[0][‘predicted\_iou’])  
result

### Create json format

def create\_data\_dict(image\_path, annotations=None, predictions=None):  
if annotations is None:  
annotations =   
if predictions is None:  
predictions =

```
data_dict = {
    "data": {
        "image": image_path
    },
    "annotations": annotations,
    "predictions": predictions
}

return data_dict

```

### Post image with mask to label studio

import json  
data\_dict = create\_data\_dict(image\_path=‘/data/local-files/?d=data/6.jpg’, predictions=[result])  
test = json.dumps(data\_dict, indent=4)  
with open(f’test.json’, ‘w’) as file:  
file.write(test)

hostname = ‘[http://localhost:8083/](http://localhost:8083/)’  
api\_token = {“Content-Type”: “application/json”, “Authorization”: “Token 65ef79347799b2e7c3891e9b5a15f713c809e37b”}  
project\_id = 1  
api\_url = f’{hostname}api/projects/{project\_id}/import’  
import requests  
response = requests.post(url=api\_url, headers=api\_token, data=json.dumps(data\_dict, indent=4))  
print(response)

**What version of Label Studio you’re using _(for example, 1.10.0)_.**  
version : 1.12.0  
**How you installed Label Studio _(for example, pip, brew, Docker, etc.)_.**  
Docker

---

<div class="post-metadata">

### Author: ![sajarin](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.labelstud.io/sajarin/32/56_2.png) [@sajarin](https://community.labelstud.io/u/sajarin)
#### Post date: [June 18, 2024, 7:57pm UTC](https://community.labelstud.io/t/how-to-use-api-post-an-image-with-segmentation-mask-in-rle-format-to-label-studio-project/248/2 "2024-06-18T19:57:43Z")

</div>

Hey @Leo thanks for the question.

1. **Check the Mask Generation and Export:**  
Ensure that the mask generated by MobileSAM is correctly formatted and aligned with the original image. The mask should be in the same dimensions as the original image.
2. **Verify the RLE Conversion:**  
The image2annotation function from label\_studio\_converter.brush should correctly convert the mask to RLE format. Ensure that the RLE data is correctly generated and matches the expected format.
3. **Label Studio Configuration:**  
Ensure that your Label Studio project configuration matches the expected input format. The from\_name and to\_name attributes in your labeling configuration should match those in your prediction JSON.
4. **Debugging the JSON Payload:**  
Can you share the JSON for the annotations you get?
