问题
When I run this:
contours,_,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
I get this error:
ValueError: not enough values to unpack (expected 3, got 2)
I have also tried:
_, contours,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
and:
contours,hierarchy,_ = cv2.findContours(thresh.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
and got this error:
Traceback (most recent call last):
File "C:/Users/HASHMATI/AppData/Local/Programs/Python/Python37-32/new2hand.py", line 152, in <module>
gestureRecognition(frame,roi_rect,frame[y:y+h, x:x+w])
File "C:/Users/HASHMATI/AppData/Local/Programs/Python/Python37-32/new2hand.py", line 99, in gestureRecognition
detectGesture(src,roi_rect,img_dilated)
File "C:/Users/HASHMATI/AppData/Local/Programs/Python/Python37-32/new2hand.py", line 21, in detectGesture
contours,_,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
ValueError: not enough values to unpack (expected 3, got 2)
回答1:
This is a difference between OpenCV 3.x and 4.x. In 3.x there were three return values, in 4.x there are only two. As the others mentioned, you only catch contours
and hierarchy
:
contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
Alternatively, you can downgrade your OpenCV version, if what you actually want is to use 3.x.
回答2:
cv2.findContours()
return two values and cannot be unpacked to contours, hierarchy and _
It should be like this:
contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
or more:
a, b, *others = [1, 2, 3, 4]
来源:https://stackoverflow.com/questions/55411176/handgesture-detection