Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
check if pycocotools is imported
Browse files Browse the repository at this point in the history
  • Loading branch information
cdpath committed Feb 24, 2023
1 parent 0a1fe2b commit e01120c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
17 changes: 16 additions & 1 deletion label_studio_converter/brush.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@
from PIL import Image
from collections import defaultdict
from itertools import groupby
import pycocotools.mask

try:
import pycocotools.mask
except ImportError:
pycocotools_imported = False
else:
pycocotools_imported = True


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -188,6 +195,14 @@ def ls_rle_to_coco_rle(ls_rle, height, width):
return result


def get_cocomask_area(segmentation):
return int(pycocotools.mask.area(segmentation))


def get_cocomask_bounding_box(segmentation):
return pycocotools.mask.toBbox(segmentation).tolist()


# convert_task_dir('/ls/test/completions', '/ls/test/completions/output', 'numpy')


Expand Down
7 changes: 3 additions & 4 deletions label_studio_converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from label_studio_converter.utils import (
parse_config, create_tokens_and_tags, download, get_image_size, get_image_size_and_channels, ensure_dir,
get_polygon_area, get_polygon_bounding_box, _get_annotator,
get_cocomask_area, get_cocomask_bounding_box,
)
from label_studio_converter import brush
from label_studio_converter.audio import convert_to_asr_json_manifest
Expand Down Expand Up @@ -586,13 +585,13 @@ def add_image(images, width, height, image_id, image_path):
'iscrowd': 0,
'area': get_polygon_area(x, y)
})
elif 'brushlabels' in label:
elif 'brushlabels' in label and brush.pycocotools_imported:
segmentation = brush.ls_rle_to_coco_rle(label["rle"], height, width)
annotations.append({
"image_id": image_id,
"segmentation": segmentation,
"area": get_cocomask_area(segmentation),
"bbox": get_cocomask_bounding_box(segmentation),
"area": brush.get_cocomask_area(segmentation),
"bbox": brush.get_cocomask_bounding_box(segmentation),
"iscrowd": 1,
"category_id": category_id,
})
Expand Down
9 changes: 0 additions & 9 deletions label_studio_converter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import argparse
import re
import datetime
import pycocotools.mask

from operator import itemgetter
from PIL import Image
Expand Down Expand Up @@ -274,14 +273,6 @@ def get_polygon_bounding_box(x, y):
return [x1, y1, x2 - x1, y2 - y1]


def get_cocomask_area(segmentation):
return int(pycocotools.mask.area(segmentation))


def get_cocomask_bounding_box(segmentation):
return pycocotools.mask.toBbox(segmentation).tolist()


def _get_annotator(item, default=None, int_id=False):
""" Get annotator id or email from annotation
"""
Expand Down

0 comments on commit e01120c

Please sign in to comment.