1
votes

I am trying to get through an OpenCV tutorial and I am using the provided source code. I run into this error:

File "C:\xxx\xxxxxxx\Desktop\basic-motion-detection\motion_detector.py", line 61, in cv2.CHAIN_APPROX_SIMPLE) ValueError: too many values to unpack.

Here is the code:

# on thresholded image
thresh = cv2.dilate(thresh, None, iterations=3)
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)`
1
Looks like you're using OpenCV 3.x (next time, please specify the version), yet writing code intended for 2.x. Some of the API has changed. If in doubt, you can always use help. - Dan Mašek
Also see stackoverflow.com/questions/20851365/…, where the user had the opposite problem. - Warren Weckesser

1 Answers

3
votes

The problem is that you are using cv2 version 3, and not version 2, the code is for version 2. To Solve your problem only change this line

(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)

for this:

(_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)