Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting Labelbox segmentation json to coco #39

Open
DeTandtThibaut opened this issue Mar 28, 2023 · 5 comments
Open

Converting Labelbox segmentation json to coco #39

DeTandtThibaut opened this issue Mar 28, 2023 · 5 comments

Comments

@DeTandtThibaut
Copy link

{"ID":"xxx","DataRow ID":"xxx","Labeled Data":"https://xxx","Label":{"objects":[{"featureId":"xxx","schemaId":"xxx","color":"#1CE6FF","title":"xxx","value":"xxx","instanceURI":"https:/xxx"}],"classifications":[],"relationships":[]},"Created By":"xxx","Project Name":"xxx","Created At":"xxx","Updated At":"xxx","Seconds to Label":xxx,"Seconds to Review":0,"Seconds to Create":xxx,"External ID":"xxx","Global Key":null,"Agreement":-1,"Is Benchmark":0,"Benchmark Agreement":-1,"Benchmark ID":null,"Dataset Name":"xxx","Reviews":[],"View Label":"xxx","Has Open Issues":0,"Skipped":false,"DataRow Workflow Info":{"taskName":"Done","Workflow History":[{"actorId":"xxx","action":"MOVE","createdAt":"xxx"},{"actorId":"xxx","action":"MOVE","createdAt":"xxx","previousTaskId":"xxx","previousTaskName":"Initial labeling task","nextTaskId":"xxx","nextTaskName":"xxx"},{"actorId":"xxx","action":"MOVE","createdAt":"xxx","nextTaskId":"xxx","nextTaskName":"Initial labeling task"}]}}, {"ID":"xxx","DataRow ID":"xxx","Labeled Data": ...

Above is the way labelbox exports the segmentation labels, in labelbox terms they call it a Mask. Is there any example code on how this can be converted to work with yolo?
if not, would anyone be so kind to help out?

@DeTandtThibaut
Copy link
Author

instanceURl is what contains a link to the mask

@DeTandtThibaut
Copy link
Author

I figured this out myself, feel free to add this to the repository in the labelbox2json file

The function below takes the URL from calling the function in the convert function with getPolygons(label['instanceURI'])
and converts it into an array of polygon coordinates which can be processed in the labelbox2json.py code easily.


    # Download the image from the URL
    with urllib.request.urlopen(url) as url_response:
        img_array = np.asarray(bytearray(url_response.read()), dtype=np.uint8)
    image = cv2.imdecode(img_array, cv2.IMREAD_COLOR)

    h, w = image.shape[:2]
    line_width = int((h + w) * 0.5 * 0.0025)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    contours, _ = cv2.findContours(gray_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS)
    
    contours_approx = []
    polygons = []
    for contour in contours:
        epsilon = 0.001 * cv2.arcLength(contour, True)
        contour_approx = cv2.approxPolyDP(contour, epsilon, True)
        
        contours_approx.append(contour_approx)
        polygon = (contour_approx.flatten().reshape(-1, 2) / np.array([w, h])).tolist()
        polygons.append(polygon)
    cv2.drawContours(image, contours_approx, -1, 128, line_width)
    result = [[f"{x},{y}" for x, y in sublist] for sublist in polygons]
    polygons = [[float(x) for pair in sublist for x in pair.split(',')] for sublist in result]

    return polygons```

@glenn-jocher
Copy link
Member

@DeTandtThibaut oh awesome! Can you please submit a PR to add your functionality?

@AyushExel see labelbox segment info above :)

@ryouchinsa
Copy link

Thanks for using our rle2polygon code to convert the Labelbox segmentation mask to polygon array.
We are happy that we could contribute to your labelbox2json script.
#38

@glenn-jocher
Copy link
Member

Thank you for the feedback on the rle2polygon code. We're glad to hear that it has been helpful for your labelbox2json script. If you have any further questions or need assistance with anything else, feel free to ask. We're here to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants