Exception has occurred: ValueError too many values to unpack (expected 3) when attempting to unpack an array

I’m trying to speed up keypoint detection by pre-generating the keypoints of the objects I want to detect before passing them into FLANN for detection.

I’m getting the keypoints and descriptors but when I try and select them with keypoint_needle = needle_kp1_desc[0] and descriptors_needle = needle_kp1_desc[1] I’m expecting keypoint_needle to be a KeyPoint value like: <KeyPoint 000001EEFF0CB5A0> and descriptors_needle to be an array while I’m actually getting both as KeyPoint values.

I thought [0] would select array column/list (unsure what it’s called when multiple lists are appended to the same array) item 0 and [1] would select column/list item 1 as in the below image.

But it looks like I’m getting object 1 and 2 from 0. How do I reference object 1 if not with [1]

enter image description here

def loadImages(directory):
    image_list = []
    for i in directory:
        img = cv.imread(i, cv.IMREAD_UNCHANGED)
        image_list.append(img)
    return image_list

def preProcessNeedle(image_list):
    needle_kp1_desc = []
    for i in image_list:
        orb = cv.ORB_create(edgeThreshold=0, patchSize=32)
        keypoints_needle, descriptors_needle = orb.detectAndCompute(i, None)
        needle_kp1_desc.append((keypoints_needle, descriptors_needle))
    return needle_kp1_desc

def kpDetection(needle_kp1_desc):
    # Object Detection
    for i in needle_kp1_desc:
        keypoint_needle = needle_kp1_desc[0]
        descriptors_needle = needle_kp1_desc[1]
        

>Solution :

Looks like you’re iterating over elements in your needle_kp1_desc, but you never do anything with i. Try:

def kpDetection(needle_kp1_desc):
    # Object Detection
    for i in needle_kp1_desc:
        keypoint_needle = i[0]
        descriptors_needle = i[1]

Note, you should also find a more descriptive name for i.

Leave a Reply