All jobs

Fix "ValueError: not enough values to unpack (expected 3, got 2)" from cv2.findContours

Yes. An agent finds every three-value unpack of cv2.findContours, rewrites it for OpenCV 4, replaces the OpenCV 3 pin that no longer installs, and tests it on masks.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

OpenCV 3 returns three values from cv2.findContours: the modified image, the contours and the hierarchy. OpenCV 4 returns two. On opencv-python-headless 3.4.18 the call on a small binary image returns a tuple of three; on 4.6.0 and the current 5.0.0 it returns two, so _, contours, hierarchy = cv2.findContours(...) raises ValueError: not enough values to unpack (expected 3, got 2). The line that fails usually sat behind a pin for years, and the pin is what gives way: opencv_python==3.2.0.7, for instance, has no build for a current Python, and uv reports no installable version of it on 3.11. The first fresh install on a new machine pulls a 4.x or 5.x wheel instead, and the unpack breaks.

asanakoy/kaggle_carvana_segmentation is the first-place solution to the Carvana image masking challenge, an ensemble of several U-Net variants with a post-processing step over the predicted masks. The root requirements.txt pins opencv_python==3.2.0.7, and albu/requirements.txt and ternaus/requirements.txt pin opencv-python==3.3.0.9 for their own Python 3.5 environments, which setup_env.sh creates with conda beside a Python 2.7 one for the root. generate_sub_final_ensemble.py, the script predict.sh runs last to build the submission, has the broken call on line 20 inside biggest_contour: im2, contours, hierarchy = cv2.findContours(np.copy(im), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE). check_if_top_is_unreliable on line 29 calls it twice, once on an eroded uncertainty mask and once on a thresholded prediction, then uses cv2.contourArea, cv2.boundingRect, cv2.convexHull and cv2.drawContours on what comes back. The repository's other cv2 calls, imread, imwrite, resize, cvtColor, copyMakeBorder, flip, warpPerspective and the rest of the augmentation code, return a single value, so line 20 is the only call whose return shape changed.

The agent's first move is a search, not an edit: it lists every findContours call in the repository with its unpacking pattern, because a repository that has one usually has others, and it checks each against the installed version by running the call on a small array rather than reading the documentation. The fix it writes handles both return shapes, since the three environments in setup_env.sh pin different releases: take the last two values of the result, contours, hierarchy = cv2.findContours(...)[-2:], which is correct on 3.x and 4.x alike and leaves the rest of biggest_contour untouched. The root pin moves to a release that installs on the Python the team uses now, and the pull request says that the two sub-project pins were left for their own environments.

It then proves the change on masks rather than on a toy image. The ensemble script reads PNG probability maps from submissions_dir, which the agent does not have, so it draws a few masks with known shapes with NumPy, runs biggest_contour and check_if_top_is_unreliable on them under the current OpenCV, and asserts the biggest contour's area and bounding box are what the shapes dictate. It runs the same test once more in an environment with an OpenCV 3 release that still installs, 3.4.18 on Python 3.11, to show the two-value slice behaves the same there. No model is trained or run; train.sh and predict.sh are left alone.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This repository fails with ValueError: not enough values to unpack (expected 3, got 2) at a cv2.findContours call. Read every requirements file and report how opencv is pinned in each and whether each pin installs on the current Python (try it, do not guess). Find every cv2.findContours call in the repository with file, line and how its result is unpacked, and every other cv2 function the code uses. Install the current opencv-python-headless and reproduce the error by calling the failing function on a small binary mask. Fix each findContours call so it works on OpenCV 3 and 4 alike, taking the last two values of the result, and change nothing else in those functions. Update the root opencv pin to a release that installs. Write a test that builds a few masks with known shapes using numpy, runs the contour functions on them, and asserts the areas and bounding boxes; run it on the current OpenCV and on an OpenCV 3 release that still installs. Do not train or run any model and do not download the competition data. Open a pull request with the list of calls, the install results per pin, and the test output on both versions.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request with a one-line change on line 20 of generate_sub_final_ensemble.py, a new test file, and a new root pin. The description starts with the table of pins: which file pins which OpenCV, and whether each still installed in the sandbox on the current Python, with the resolver's message for the ones that did not. Then the reproduction, and the list of every findContours call found.

The session's record keeps the test run on two OpenCV releases, with the printed return-value count from each, so a reviewer sees that the slice is not a guess. It also keeps the search output, which is the evidence that the one call on line 20 is the only one in the repository.

Before merging, a reviewer decides what to do with the two sub-project pins the pull request left alone, and whether the root environment should move to Python 3 at the same time, since predict.sh still activates a Python 2.7 conda environment. The pull request raises both and changes neither.

Run it on every repository in your org

The same unpack sits in most image-processing code written against OpenCV 3, and a team usually finds out one repository at a time. A code search for findContours across the org lists them, and a script sends this request once per repository with POST /o/{org}/sessions, the repository in resources and a request_id that is a UUID v5 of the repository name, so running the script twice returns the runs it already started.

Each run is its own sandbox with that repository's own pins and calls, and runs past the key's max_concurrent limit are accepted with status queued and start as slots free. A webhook on the key posts run.completed for each, with the session URL to read the pull request from, and a pin that does not install is reported as such in that repository's pull request instead of guessed around.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

The error is one line and the evidence is three environments. Showing that the slice is right means installing OpenCV 3 and OpenCV 4 or 5 side by side and running the same function on the same arrays, and finding out which old pins still install means trying each on the Python the team runs. A sandbox does that without touching the conda environments a research repository accumulates on a shared machine.

The result comes with its proof attached: the search that found the calls, the resolver output for each pin, and the test output on both releases are all in the session, and the pull request quotes them. A reviewer who last touched this script years ago reads what was found rather than what was assumed.

Frequently asked questions

Why does the same line work on OpenCV 3?

Because OpenCV 3's findContours returns the image, the contours and the hierarchy, and OpenCV 4 dropped the image: 3.4.18 returns three values and 4.6.0 and 5.0.0 return two. Code that unpacks three names works only on 3.x.

Is slicing the last two values safe on both?

Yes. cv2.findContours(...)[-2:] gives (contours, hierarchy) on 3.x and on 4.x and 5.x, so one line serves every environment the repository's three requirements files describe. The test runs on both to show it.

Why not just pin OpenCV 3 again?

The root pin, opencv_python==3.2.0.7, has no build for a current Python and does not resolve; that is how the error surfaced. A later 3.4 release installs today, but the slice removes the dependency on the return shape, which is the cheaper fix.

Does the agent run the segmentation models?

No. The failing function post-processes predicted masks, so the test feeds it masks drawn with NumPy and checks areas and bounding boxes. The competition data and the trained weights are not downloaded.

What about the other cv2 calls in the repository?

imread, imwrite, resize, cvtColor, copyMakeBorder, flip, warpPerspective and the rest return one value and did not change shape between OpenCV 3 and 4, so they are left alone. The agent lists them so the reviewer knows the search covered every cv2 call and not only findContours.

More OpenCV jobs

More jobs like this: Fix a bug from an alert

Keep reading