Error "ValueError: too many values to unpack (expected 4)" on official structured edge detection and edgeboxes example

Viewed 159

I need help with OpenCV implementation of StructuredEdgeDetection based on the random forest based approach. I'm using official demo and getting error on line 22 ValueError: too many values to unpack.

Here is python script:

import cv2 as cv
import numpy as np
import sys

if __name__ == '__main__':
    model = './model.yml'
    im = cv.imread('./L8C5M.png')

    edge_detection = cv.ximgproc.createStructuredEdgeDetection(model)
    rgb_im = cv.cvtColor(im, cv.COLOR_BGR2RGB)
    edges = edge_detection.detectEdges(np.float32(rgb_im) / 255.0)

    orimap = edge_detection.computeOrientation(edges)
    edges = edge_detection.edgesNms(edges, orimap)

    edge_boxes = cv.ximgproc.createEdgeBoxes()
    edge_boxes.setMaxBoxes(30)
    boxes = edge_boxes.getBoundingBoxes(edges, orimap)

    print(boxes)
    for b in boxes:
        x, y, w, h = b
        cv.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 1, cv.LINE_AA)

    cv.imshow("edges", edges)
    cv.imshow("edgeboxes", im)
    cv.waitKey(0)
    cv.destroyAllWindows()

I'm expecting result like this solution

2 Answers

The error occurs in line x, y, w, h = b within the for loop.

The variable boxes is a tuple comprising of two numpy arrays

According to the code you want to draw bounding boxes found by the edge detection algorithm. In order to that, you need to iterate through the first array of variable boxes, i.e loop through boxes[0]

Change line number 21 to for b in boxes[0]: and proceed as usual.

Sample output:

enter image description here

The method returns (boxes, scores), not just the boxes. That seems to have been changed between 2017 and 2019.

You should use this code:

(boxes, scores) = edge_boxes.getBoundingBoxes(edges, orimap)

Then the loop will work.

You encountered this issue because you're using an outdated link to that example. The link you have shows a version from 2017. That old code would work with a 2017/2018 release of OpenCV.

The current version of the example, from 2019, corresponds to the current API. The current example shows the proper usage for the current API.

Here is the change: https://github.com/opencv/opencv_contrib/commit/6ae9809b2e464b72810298b10c6cf4935886a0f1

Related