Error mapping labels from label-studio to their respective Images

165 views Asked by At

I've used Label Studio to label several images and exported the labels as a .json file. However, I'm encountering an error when attempting to map these labels to their respective images using Python. The error message mentions rle-var as an int which is not true, rle_var is a list data type. For reference, you can access the json_file.json containing the labels here: json_file.json, I have added the code & error below.

I am particularly curious in mapping the labels with the actual images and calculating the area covered by the labels on the respective images.

Code:

from pprint import pprint
from pycocotools import mask as coco_mask
import os, pandas as pd, json, cv2, numpy as np

filepath = r"/path/to/data/SPLIT_IMAGES_USED"
json_filepath = r"/path/to/data/json_file.json"


with open(json_filepath, 'r') as f:
      data = json.load(f)

print("{} file exists".format("-".join(data[0]['data']['image'].split('-')[1:])) 
      if "-".join(data[0]['data']['image'].split('-')[1:]) in os.listdir(filepath) 
      else "{} file does not exist".format("-".join(data[0]['data']['image'].split('-')[1:])))  # checking if the file from json exists in the filepath
print("The markers/labels are for the {}".format(data[0]['annotations'][0]['result'][0]['value']['brushlabels']))

for file in os.listdir(filepath):
    for element in range(0, len(data)):
        if file == "-".join(data[element]['data']['image'].split('-')[1:]):
            image = cv2.imread(filepath + os.sep + file)
            # rle_var = data[element]['annotations'][0]['result'][0]['value']['rle']
            for annotation in data[element]['annotations']:
                for result in annotation['result']:
                    if 'format' in result['value'] and result['value']['format'] == 'rle':
                        rle_var = result['value']['rle']
                        # print(type(rle_var))
                        mask = coco_mask.decode(rle_var)
                        mask_image = np.zeros((data[element]['annotations'][0]['result'][0]['original_height'], data[element]['annotations'][0]['result'][0]['original_width']), dtype=np.uint8)
                        mask_image[mask] = 255
                        overlay = cv2.addWeighted(image, 0.5, cv2.cvtColor(mask_image, cv2.COLOR_GRAY2BGR), 0.5, 0)
                        cv2.imshow('Image with Segmentation', overlay)
 cv2.destroyAllWindows()

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[90], line 11
      9 rle_var = result['value']['rle']
     10 # print(type(rle_var))
---> 11 mask = coco_mask.decode(rle_var)
     12 mask_image = np.zeros((data[element]['annotations'][0]['result'][0]['original_height'], data[element]['annotations'][0]['result'][0]['original_width']), dtype=np.uint8)
     13 mask_image[mask] = 255

File ~\AppData\Local\anaconda3\lib\site-packages\pycocotools\mask.py:89, in decode(rleObjs)
     87 def decode(rleObjs):
     88     if type(rleObjs) == list:
---> 89         return _mask.decode(rleObjs)
     90     else:
     91         return _mask.decode([rleObjs])[:,:,0]

File pycocotools\\_mask.pyx:145, in pycocotools._mask.decode()

File pycocotools\\_mask.pyx:127, in pycocotools._mask._frString()

TypeError: 'int' object is not subscriptable
0

There are 0 answers