"Too many values to unpack (expected 2)" in Python opencv2

Cendol404 :

I have a problem where I am getting an error message

success, img = cv2.imread ('stop.jpg', 0) ValueError: too many values to unpack (expected 2)

I am using the following script:

def getContours(img, imgContour):
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]

for cnt in contours:
    area = cv2.contourArea(cnt)
    if area > 1000:
        cv2.drawContours(imgContour, contours, -1, (255, 0, 255), 7)
        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
        # print(len(approx))
        x, y, w, h = cv2.boundingRect(approx)
        cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 5)

        cv2.putText(imgContour, "Point: " + str(len(approx)), (x + w + 20, y + 20), cv2.FONT_HERSHEY_COMPLEX, .7,
                    (0, 255, 0), 2)
        cv2.putText(imgContour, "Area: " + str(int(area)), (x + w + 20, y + 45), cv2.FONT_HERSHEY_COMPLEX, 0.7,
                    (0, 255, 0), 2)


success, img = cv2.imread('stop.jpg', 0)
imgContour = img.copy()

imgBlur = cv2.GaussianBlur(img, (7, 7), 1)
imgGray = cv2.cvtColor(imgBlur, cv2.COLOR_BGR2GRAY)

threshold1 = cv2.getTrackbarPos("Threshold1", "Parameter")
threshold2 = cv2.getTrackbarPos("Threshold2", "Parameter")
imgCanny = cv2.Canny(imgGray, threshold1, threshold2)
kernel = np.ones((5, 5))
imgDil = cv2.dilate(imgCanny, kernel, iterations=1)

getContours(imgDil, imgContour)

How can I fix this?

Nullman :

imread only returns one thing so change your line to

img = cv2.imread('stop.jpg', 0)

You are thinking of reading from a capture device which returns 2 things cv2.VideoCapture.read([image]) → retval, image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174291&siteId=1