Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

fixup cv2.findContours to only return 2 values #336

Closed

Conversation

aaronlelevier
Copy link
Contributor

This PR is to fix an error that I ran in to when running demo/Mask_R-CNN_demo.ipynb

The PR removes the first _ assigned variable. Based on the docs for opencv cv2.findContours only returns 2 values. Here's the docs link:

https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0

Here is the stack trace that I got from the current code on master:

ValueError                                Traceback (most recent call last)
<ipython-input-10-ba06eae97bcc> in <module>
      1 # compute predictions
----> 2 predictions = coco_demo.run_on_opencv_image(image)
      3 imshow(predictions)

~/github/maskrcnn-benchmark/demo/predictor.py in run_on_opencv_image(self, image)
    177         result = self.overlay_boxes(result, top_predictions)
    178         if self.cfg.MODEL.MASK_ON:
--> 179             result = self.overlay_mask(result, top_predictions)
    180         result = self.overlay_class_names(result, top_predictions)
    181 

~/github/maskrcnn-benchmark/demo/predictor.py in overlay_mask(self, image, predictions)
    289             thresh = mask[0, :, :, None]
    290             _, contours, hierarchy = cv2.findContours(
--> 291                 thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
    292             )
    293             image = cv2.drawContours(image, contours, -1, color, 3)

ValueError: not enough values to unpack (expected 3, got 2)

I was using these package versions when I ran into the error:

Python 3.6.7
opencv-python==4.0.0.21

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Jan 12, 2019
@fmassa
Copy link
Contributor

fmassa commented Jan 14, 2019

Hi,

This is a problem with OpenCV versions.

In my case, I have OpenCV 3.4.3, and the function signature is

findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy

Could you maybe add a helper function findContours, which internally handles different versions of OpenCV instead?

Thanks!

@ardiya
Copy link

ardiya commented Jan 18, 2019

@aaronlelevier Would you mind changing it to this code? this won't break other opencv version

if cv2.__version__.startswith('2') or cv2.__version__.startswith('4'):
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
else:
    _, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

@fmassa
Copy link
Contributor

fmassa commented Jan 18, 2019

Yes, I'd be willing to merge such a PR, as propose by @ardiya

@csguanhu
Copy link

csguanhu commented Mar 7, 2022

fix this bug following these steps: 1 pip uninstall opencv-python; 2 conda install opencv;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed Do not delete this pull request or issue due to inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants