1

I was taking source code from pyimagesearch.com to make a mobile document scanner and tried to test out the code. The edge detection part works but whenever I arrive at the part where it tries to find contours of an image, the program outputs an error saying that there are too many values to unpack, despite the programming working on the author's side.

What's the problem and how do I fix it?

Blog Post about the source code: http://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/?__vid=c35c22a06af30132982122000b2a88d7

Youtube video about the program: https://www.youtube.com/watch?v=yRer1GC2298

Terminal Command in Ubuntu

python scan.py --image images/page.jpg 

Result:

STEP 1: Edge Detection
Traceback (most recent call last):
  File "scan.py", line 40, in <module>
    (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack

Code:

# USAGE
# python scan.py --image images/page.jpg 

# import the necessary packages
from pyimagesearch.transform import four_point_transform
from pyimagesearch import imutils
from skimage.filter import threshold_adaptive
import numpy as np
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
        help = "Path to the image to be scanned")
args = vars(ap.parse_args())

# load the image and compute the ratio of the old height
# to the new height, clone it, and resize it
image = cv2.imread(args["image"])
ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)

# convert the image to grayscale, blur it, and find edges
# in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 75, 200)

# show the original image and the edge detected image
print "STEP 1: Edge Detection"
cv2.imshow("Image", image)
cv2.imshow("Edged", edged)
cv2.waitKey(0)
cv2.destroyAllWindows()

# find the contours in the edged image, kee
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, ping only the
# largest ones, and initialize the screen contourcv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

# loop over the contours
for c in cnts:
        # approximate the contour
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)

        # if our approximated contour has four points, then we
        # can assume that we have found our screen
        if len(approx) == 4:
                screenCnt = approx
                break

# show the contour (outline) of the piece of paper
print "STEP 2: Find contours of paper"
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Outline", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# apply the four point transform to obtain a top-down
# view of the original image
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)

# convert the warped image to grayscale, then threshold it
# to give it that 'black and white' paper effect
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
warped = threshold_adaptive(warped, 250, offset = 10)
warped = warped.astype("uint8") * 255

# show the original and scanned images
print "STEP 3: Apply perspective transform"
cv2.imshow("Original", imutils.resize(orig, height = 650))
cv2.imshow("Scanned", imutils.resize(warped, height = 650))
cv2.waitKey(0)
18
  • what will this line return? image = imutils.resize(image, height = 500)? will it change how image will be? Because omitting this line, the code runs fine in my laptop
    – Anzel
    Jan 2, 2015 at 18:55
  • It resizes the image to a height of 500. When I omitted it, the un-resized images popped out as extremely large images (it covered the screen). Surprisingly, even when I do that, I still get the same error: File "scan.py", line 40, in <module> (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) ValueError: too many values to unpack Jan 2, 2015 at 19:40
  • Are you saying that with that code, it did not work on your end either, but once you removed it, it suddenly worked? Jan 2, 2015 at 19:41
  • I haven't got pyimagesearch installed to start with. So I can only remove those lines, and when I run the code there's no exception raised. Can you post a full stack trace?
    – Anzel
    Jan 2, 2015 at 19:43
  • This was the entire error output for running the script. What happens is that it shows me two images that popped up (one image and one image with edge detection) and print out "Step 1: Edge Detection". Then after you close the two images to move on, the script continues and gives me an error involving too many values to unpack. STEP 1: Edge Detection Traceback (most recent call last): File "scan.py", line 40, in <module> (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) ValueError: too many values to unpack Jan 2, 2015 at 20:13

3 Answers 3

6

This is the answer at least works for me. The function return 3 values so:

_,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
1
(_,cnts,hierarchy) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
0

In OpenCV 3.0.0 (beta) they have added a return value. This works:

derp,contours,hierarchy = cv2.findContours(dilation.copy(),cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE) 

I have no idea what derp is and it can be ignored.

1
  • Please format your answer to improve its readability.
    – ryanyuyu
    Mar 19, 2015 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.