Skip to content

Commit

Permalink
add try for skimage and pycocotools packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
ouyanglinke authored and HanxSmile committed Apr 18, 2023
1 parent 2158f44 commit 091f695
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions dsdl/converter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import networkx as nx
import yaml
import numpy as np
from skimage import measure
from pycocotools import mask as mask_util
try:
from skimage import measure
except ImportError:
measure = None
try:
from pycocotools import mask as mask_util
except ImportError:
mask_util = None
import xml.etree.ElementTree as ET
import shutil

Expand Down Expand Up @@ -168,6 +174,8 @@ def annToRLE(ann, img_height, img_width):
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
if mask_util is None:
raise RuntimeError('Package pycocotools.mask is not installed.')
h, w = img_height, img_width
segm = ann['segmentation']
if type(segm) == list:
Expand All @@ -189,6 +197,8 @@ def annToMask(ann, h, w):
Convert annotation which can be polygons, uncompressed RLE, or RLE to binary mask.
:return: binary mask (numpy 2D array)
"""
if mask_util is None:
raise RuntimeError('Package pycocotools.mask is not installed.')
rle = annToRLE(ann, h, w)
m = mask_util.decode(rle)
return m
Expand All @@ -199,6 +209,8 @@ def mask2polygon(mask):
# encoded_ground_truth = mask_util.encode(fortran_ground_truth_binary_mask)
# ground_truth_area = mask_util.area(encoded_ground_truth)
# ground_truth_bounding_box = mask_util.toBbox(encoded_ground_truth)
if measure is None:
raise RuntimeError('Package skimage.measure is not installed.')
contours = measure.find_contours(mask, 0.5)
segmentations = []
for contour in contours:
Expand Down

0 comments on commit 091f695

Please sign in to comment.