From 37094856d6a75a74049dfa80abb1987614ea1a4e Mon Sep 17 00:00:00 2001 From: Evening Date: Mon, 25 Mar 2024 15:06:46 +0800 Subject: [PATCH] Add circumvention for multiple annots & bad labs See FRML-128 issue for more details on this fix --- src/frdc/load/label_studio.py | 65 +++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/frdc/load/label_studio.py b/src/frdc/load/label_studio.py index 6383cfe4..0b587f19 100644 --- a/src/frdc/load/label_studio.py +++ b/src/frdc/load/label_studio.py @@ -16,35 +16,42 @@ class Task(dict): def get_bounds_and_labels(self) -> tuple[list[tuple[int, int]], list[str]]: bounds = [] labels = [] - for ann_ix, ann in enumerate(self["annotations"]): - results = ann["result"] - for r_ix, r in enumerate(results): - r: dict - - # We flatten the value dict into the result dict - v = r.pop("value") - r = {**r, **v} - - # Points are in percentage, we need to convert them to pixels - r["points"] = [ - ( - int(x * r["original_width"] / 100), - int(y * r["original_height"] / 100), - ) - for x, y in r["points"] - ] - - # Only take the first label as this is not a multi-label task - r["label"] = r.pop("polygonlabels")[0] - if not r["closed"]: - logger.warning( - f"Label for {r['label']} @ {r['points']} not closed. " - f"Skipping" - ) - continue - - bounds.append(r["points"]) - labels.append(r["label"]) + + # for ann_ix, ann in enumerate(self["annotations"]): + + ann = self["annotations"][0] + results = ann["result"] + for r_ix, r in enumerate(results): + r: dict + + # See Issue FRML-78: Somehow some labels are actually just metadata + if r["from_name"] != "label": + continue + + # We flatten the value dict into the result dict + v = r.pop("value") + r = {**r, **v} + + # Points are in percentage, we need to convert them to pixels + r["points"] = [ + ( + int(x * r["original_width"] / 100), + int(y * r["original_height"] / 100), + ) + for x, y in r["points"] + ] + + # Only take the first label as this is not a multi-label task + r["label"] = r.pop("polygonlabels")[0] + if not r["closed"]: + logger.warning( + f"Label for {r['label']} @ {r['points']} not closed. " + f"Skipping" + ) + continue + + bounds.append(r["points"]) + labels.append(r["label"]) return bounds, labels