解决报错ValueError: not enough values to unpack (expected 3, got 2)

  • Post author:
  • Post category:其他


def getKeypoints(probMap, threshold=0.1):
    mapSmooth = cv2.GaussianBlur(probMap,(3,3),0,0)
    mapMask = np.uint8(mapSmooth>threshold)
    keypoints = []
    #find the blobs
    _, contours, _ = cv2.findContours(mapMask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    #for each blob find the maxima

最近使用openpose进行多人关键点识别时,出现如下错误

File "d:/openpose/poses.py", line 30, in getKeypoints
    _, contours, _ = cv2.findContours(mapMask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)

报错原因是函数定义的返回值数量和调用函数时实际赋予的返回值数量不一致,实际是因为opencv版本不一致引起的,只需要删除一个参数就可以完美解决


解决方法:删除第一个参数

OpenCV旧版中,返回三个参数

_, contours, _ = cv2.findContours(mapMask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

OpenCV新版,只需要返回两个参数,所以把上面代码中第一个参数删除就可以解决

contours, _ = cv2.findContours(mapMask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)



版权声明:本文为weixin_47214888原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。