Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
add helper function 'cv2_utils.findContours' to maintain version comp…
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronlelevier authored and fmassa committed Jan 21, 2019
1 parent eb3b079 commit e140067
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion demo/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from maskrcnn_benchmark.structures.image_list import to_image_list
from maskrcnn_benchmark.modeling.roi_heads.mask_head.inference import Masker
from maskrcnn_benchmark import layers as L
from maskrcnn_benchmark.utils import cv2_util


class COCODemo(object):
Expand Down Expand Up @@ -287,7 +288,7 @@ def overlay_mask(self, image, predictions):

for mask, color in zip(masks, colors):
thresh = mask[0, :, :, None]
_, contours, hierarchy = cv2.findContours(
contours, hierarchy = cv2_util.findContours(
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
image = cv2.drawContours(image, contours, -1, color, 3)
Expand Down
24 changes: 24 additions & 0 deletions maskrcnn_benchmark/utils/cv2_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Module for cv2 utility functions and maintaining version compatibility
between 3.x and 4.x
"""
import cv2


def findContours(*args, **kwargs):
"""
Wraps cv2.findContours to maintain compatiblity between versions
3 and 4
Returns:
contours, hierarchy
"""
if cv2.__version__.startswith('4'):
contours, hierarchy = cv2.findContours(*args, **kwargs)
elif cv2.__version__.startswith('3'):
_, contours, hierarchy = cv2.findContours(*args, **kwargs)
else:
raise AssertionError(
'cv2 must be either version 3 or 4 to call this method')

return contours, hierarchy
4 changes: 3 additions & 1 deletion tools/cityscapes/instances2dict_with_polygons.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from cityscapesscripts.evaluation.instance import *
from cityscapesscripts.helpers.csHelpers import *
import cv2
from maskrcnn_benchmark.utils import cv2_util


def instances2dict_with_polygons(imageFileList, verbose=False):
imgCount = 0
Expand Down Expand Up @@ -46,7 +48,7 @@ def instances2dict_with_polygons(imageFileList, verbose=False):
#instances[id2label[instanceObj.labelID].name].append(instanceObj.toDict())
if id2label[instanceObj.labelID].hasInstances:
mask = (imgNp == instanceId).astype(np.uint8)
im2, contour, hier = cv2.findContours(
contour, hier = cv2_util.findContours(
mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

polygons = [c.reshape(-1).tolist() for c in contour]
Expand Down

0 comments on commit e140067

Please sign in to comment.