ocr-text-extraction
ocr-text-extraction copied to clipboard
ValueError: too many values to unpack in cv2.findContours
For whatever reason, cv2.findContours
on line 211 returns three arguments instead of the expected 2. From playing with it, I found that by eliminating the first value would solve the problem.
I am not knowledgeable about cv2 so if this is something obvious someone please say so. (MacOS, cv2 '3.2.0').
Here is the fix that worked for me.
result = cv2.findContours(edges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, hierarchy = result if len(result) == 2 else result[1:3]
Adding a third value in front of the other two also solved the problem for me.
For example:
_, contours, hierarchy = cv2.findContours(edges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
Apparently findContours got changed some time ago: https://stackoverflow.com/questions/25504964/opencv-python-valueerror-too-many-values-to-unpack
Yes above solution worked for me too.
conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) ValueError: too many values to unpack (expected 2)
sir please tell me where I'm wrong
Just initialize with one more variable in front of other two on L.H.S.For example:- _,conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
I've used _(Underscore) here.You can give any name or simply use it as underscore only.
Is it safe to say that find Contours needs three variables to store the returned value?
Ofcourse it is.Don't worry, it is safe to say this.
(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Not able to understand. Please help me. giving error too many values to unpack.
Use like this:- (_, cnts, h)= cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Because you cannot unpack 3 values from the tuple and place them in a tuple of two,which you are doing.
What is the third value, though? According to the docs, only two should be returned. https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours
Edit: I am using opencv3, cannot find the docs for findContours
in 3.0 though, only 2.4.
EASY FIX ;)
( _ , cnts , _ ) = cv2.findContours(threshFrame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) ValueError: not enough values to unpack (expected 3, got 2)
try this
( cnts , _ )
python is right. you cannot unpack 3 values from the turple and place them in a turple of two ###
happy coding ;)
@bharath5673 this works ,thank u
I'm getting the same error unable to resolve it.
ValueError: not enough values to unpack (expected 3, got 2)