Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

ValueError: not enough values to unpack (expected 3, got 2) #339

Closed
ypj95 opened this issue Jan 13, 2019 · 38 comments
Closed

ValueError: not enough values to unpack (expected 3, got 2) #339

ypj95 opened this issue Jan 13, 2019 · 38 comments

Comments

@ypj95
Copy link

ypj95 commented Jan 13, 2019

❓ Questions and Help

So I followed the jupyter notebook tutorial and putted the code sequences into one file .
When I compile with python I get this error result:

Traceback (most recent call last):
File "demo.py", line 42, in
predictions = coco_demo.run_on_opencv_image(image)
File "/home/nero/Documents/workspace/github/maskrcnn-benchmark/demo/predictor.py", line 179, in run_on_opencv_image
result = self.overlay_mask(result, top_predictions)
File "/home/nero/Documents/workspace/github/maskrcnn-benchmark/demo/predictor.py", line 292, in overlay_mask
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
ValueError: not enough values to unpack (expected 3, got 2)

Seems that the last saved value in predictor.py in overlay_mask() can't be found.. not sure what to do.

Running on Ubuntu 18.04
Cuda 10 & 9.1

@kishorepv
Copy link

kishorepv commented Jan 14, 2019

The problem is in this line in maskrcnn-benchmark/demo/predictor.py:

_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # fails with error "not enough values to unpack (expected 3, got 2)"

Change it to:

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

Restart the kernel and run. Your problem should be fixed

@ypj95
Copy link
Author

ypj95 commented Jan 14, 2019

That's what I have tried and it worked too. Just wondering why expecting a 3rd parameter at all.

Cheers for help!

@ypj95 ypj95 closed this as completed Jan 14, 2019
@fmassa
Copy link
Contributor

fmassa commented Jan 14, 2019

Hi,

This is a problem with your OpenCV version. The current implementation works fine for OpenCV 3.4.3. If your OpenCV version is < that, then the fix proposed by @kirkshoop should fix it.

@ypj95
Copy link
Author

ypj95 commented Jan 14, 2019

@fmassa Hello,
okey, seems that I am using OpenCV 4.0.0.21. Either way @kirkshoop solution fixed the problem.

@muzammil360
Copy link

I also got the above error and fix mentioned above works. However, I am using opencv version '4.0.0'

@mythrireddy
Copy link

1

please tell me solution for this problem

@muzammil360
Copy link

@mythrireddy, a simple solution to this problem is to follow the advice of @kishorepv above.

@DeepakSuryaS
Copy link

@kishorepv your solution worked. Thanks man.

@pathfinderconsultancyhr123

@kishorepv reddy saab , problem solved thanks man, can i have your mail id?

hrghauri added a commit to hrghauri/IoT-Hydroponics that referenced this issue May 16, 2019
@Piyush142
Copy link

ret,contours, hierarchy = cv2.findContours(bordered, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

Change it to
contours, hierarchy = cv2.findContours(bordered, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

@AnnAfloatX
Copy link

How do I get the modified img in this case then? Does it mean that the source img is modified?
(using verions 4.1.0 btw)

@muzammil360
Copy link

Does it mean that the source img is modified?

@AnnMLin, since you are using 4.1.0, the source image is not modified in this case. Take a look at documentation here.

image

How do I get the modified img in this case then?

What do you mean by modified image? If you want to modify your input image (in whatever way you choose), you can read each counter from contours and apply it as your wish.

I hope this helps.

@snehitvaddi
Copy link

@AnnMLin @barrelsrider
Since you are using opencv > 4, just use
contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
instead of
image,contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
Though without using image value it worked fine. It is drawing contours just with values of contours and hierarchy returned. It doesn't require any image or ret value to be declared.

@Rahesh31
Copy link

The problem is in this line in maskrcnn-benchmark/demo/predictor.py:
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # fails with error "not enough values to unpack (expected 3, got 2)"
Change it to:
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Restart the kernel and run. Your problem should be fixed

I have this problem what should I do now Im new to this
Capture

@muzammil360
Copy link

@rahesh, you need to remove the "underscore" from relevent file. The solution has been explained in detail above.

@snehitvaddi
Copy link

@Rahesh31 bro, refer above discussions you will have clear idea.

@Falcatrua
Copy link

from opencv 4.0 on,

contours, find_h = cv2.findContours(im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contour_img = np.zeros(im.shape)
cv2.drawContours(contour_img, contours, -1, tuple([255]*im.shape[-1]), 1)

contour_img will have only the contours drawn in white

@Rahesh31
Copy link

Rahesh31 commented Sep 5, 2019 via email

@jholar
Copy link

jholar commented Sep 25, 2019

error

ValueError: too many values to unpack (expected 2)

Please help me to solve this error
Thanks in advance
i am using opencv 3.4.5. version

@botcs
Copy link
Contributor

botcs commented Sep 25, 2019

just modify L74 to contours, hierarchy, _, but really, I'm curious where did you get the idea that a windows OCR script error should be issued here? :D

@jholar
Copy link

jholar commented Sep 25, 2019

Hey sorry for posting on wrong post but really thanks for replying the issue i am getting is solved now!!!!!!

@itsayushsingh
Copy link

I had the same issue. Working fine with the above fix. Thanks :D

@mamarasulovs
Copy link

The problem is in this line in maskrcnn-benchmark/demo/predictor.py:

_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # fails with error "not enough values to unpack (expected 3, got 2)"

Change it to:

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

Restart the kernel and run. Your problem should be fixed

thanks dear. its working

@tnavadiya
Copy link

Only this given photo works. If I tried some other photos which have garden in background then it doesn't worked.

@tonnidewan
Copy link

Tq so much 😊i got it

On Thu 5 Sep, 2019, 6:17 PM Falcatrua, @.***> wrote: from opencv 4.0 on, contours, find_h = cv2.findContours(im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) contour_img = np.zeros(im.shape) cv2.drawContours(contour_img, contours, -1, tuple([255]*im.shape[-1]), 1) contour_img will have only the contours drawn in white — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#339?email_source=notifications&email_token=AM7K3BH5CI67ZN3YB5NGRIDQID5WZA5CNFSM4GPURHUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5666AQ#issuecomment-528346882>, or mute the thread https://github.com/notifications/unsubscribe-auth/AM7K3BDTCMQDQPOFDJJFIV3QIur5WZANCNFSM4GPURHUA .

Scalar value for argument 'color' is longer than 4 . this error is showed when used your code

@tejaswinivjadhav
Copy link

@fmassa it fixed the problem .thank you

@sowbhagya09
Copy link

i changed it to " contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)"
then it is getting like this :

in
63 closing = cv2.cvtColor(closing,cv2.COLOR_BGR2GRAY)
64
---> 65 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # find contours with simple approximation cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE
66
67 closing = cv2.cvtColor(closing,cv2.COLOR_GRAY2RGB)

error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgproc\src\contours.cpp:197: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'

@towhidrahman
Copy link

Uploading Capture.JPG…

@towhidrahman
Copy link

help me anyone

@cu2uyash
Copy link

Hi,

This is a problem with your OpenCV version. The current implementation works fine for OpenCV 3.4.3. If your OpenCV version is < that, then the fix proposed by @kirkshoop should fix it.

@fmassa that works thank you

Lyears pushed a commit to Lyears/maskrcnn-benchmark that referenced this issue Jun 28, 2020
Krupal09 added a commit to Krupal09/MaskRCNN-Keypoint-Demo that referenced this issue Jul 15, 2020
@av04121997
Copy link

---> 34 image, contours, hierarchy= cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE )
35
36

ValueError: not enough values to unpack (expected 3, got 2)

@nithinreddyy
Copy link

just modify L74 to contours, hierarchy, _, but really, I'm curious where did you get the idea that a windows OCR script error should be issued here? :D

It's not working for me can you please look into it.
opencverror

@nithinreddyy
Copy link

Hey sorry for posting on the
opencverror
wrong post but really thanks for replying the issue i am getting is solved now!!!!!!

How did you get that? I'm getting the same error again

@Tam678
Copy link

Tam678 commented Oct 23, 2020

The solution above fixed my problem,thanks a lot!

@shubhamnagalwade
Copy link

@kishorepv Your solution work for me. Thank you.

@nesrine20mezni
Copy link

image
please i need help ??

@Leon-Brant
Copy link

你好,

这是您的 OpenCV 版本的问题。当前的实现适用于 OpenCV 3.4.3。如果您的 OpenCV 版本是 < 那,那么@kirkshoop提出的修复应该修复它。
Thank you for sharing this information. It is more important to know the cause of the error than to fix it

@zhhvjhtgrfedfgrhjuurgtrf

import cv2
import numpy as np
import math

def distance(x1, y1, x2, y2):
"""
Calculate distance between two points
"""
dist = math.sqrt(math.fabs(x2-x1)**2 + math.fabs(y2-y1)**2)
return dist

def find_color1(frame):
"""
Filter "frame" for HSV bounds for color1 (inplace, modifies frame) & return coordinates of the object with that color
"""
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hsv_lowerbound = np.array([102, 152, 0]) #replace THIS LINE w/ your hsv lowerb
hsv_upperbound = np.array([118, 255, 255])#replace THIS LINE w/ your hsv upperb
mask = cv2.inRange(hsv_frame, hsv_lowerbound, hsv_upperbound)
res = cv2.bitwise_and(frame, frame, mask=mask) #filter inplace
, cnts, = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if len(cnts) > 0:
maxcontour = max(cnts, key=cv2.contourArea)

    #Find center of the contour 
    M = cv2.moments(maxcontour)
    if M['m00'] > 0 and cv2.contourArea(maxcontour) > 1000:
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])
        return (cx, cy), True
    else:
        return (700, 700), False #faraway point
else:
    return (700, 700), False #faraway point

def find_color2(frame):
"""
Filter "frame" for HSV bounds for color1 (inplace, modifies frame) & return coordinates of the object with that color
"""
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hsv_lowerbound = np.array([40, 83, 0])#replace THIS LINE w/ your hsv lowerb
hsv_upperbound = np.array([101, 255, 255])#replace THIS LINE w/ your hsv upperb
mask = cv2.inRange(hsv_frame, hsv_lowerbound, hsv_upperbound)
res = cv2.bitwise_and(frame, frame, mask=mask)
,cnts, = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if len(cnts) > 0:
maxcontour = max(cnts, key=cv2.contourArea)

    #Find center of the contour 
    M = cv2.moments(maxcontour)
    if M['m00'] > 0 and cv2.contourArea(maxcontour) > 2000:
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])
        return (cx, cy), True #True
    else:
        return (700, 700), True #faraway point
else:
    return (700, 700), True #faraway point

cap = cv2.VideoCapture(0)

while(1):
_, orig_frame = cap.read()

if orig_frame is None:
    break


#we'll be inplace modifying frames, so save a copy
copy_frame = orig_frame.copy() 
(color1_x, color1_y), found_color1 = find_color1(copy_frame)
(color2_x, color2_y), found_color2 = find_color2(copy_frame)

#draw circles around these objects
cv2.circle(copy_frame, (color1_x, color1_y), 20, (255, 0, 0), -1)
cv2.circle(copy_frame, (color2_x, color2_y), 20, (0, 128, 255), -1)

if found_color1 and found_color2:
    #trig stuff to get the line
    hypotenuse = distance(color1_x, color1_x, color2_x, color2_y)
    horizontal = distance(color1_x, color1_y, color2_x, color1_y)
    vertical = distance(color2_x, color2_y, color2_x, color1_y)
    angle = np.arcsin(vertical/hypotenuse)*180.0/math.pi

    #draw all 3 lines
    cv2.line(copy_frame, (color1_x, color1_y), (color2_x, color2_y), (0, 0, 255), 2)
    cv2.line(copy_frame, (color1_x, color1_y), (color2_x, color1_y), (0, 0, 255), 2)
    cv2.line(copy_frame, (color2_x, color2_y), (color2_x, color1_y), (0, 0, 255), 2)

    #put angle text (allow for calculations upto 180 degrees)
    angle_text = ""
    if color2_y < color1_y and color2_x > color1_x:
        angle_text = str(int(angle))
    elif color2_y < color1_y and color2_x < color1_x:
        angle_text = str(int(180 - angle))
    elif color2_y > color1_y and color2_x < color1_x:
        angle_text = str(int(180 + angle))
    elif color2_y > color1_y and color2_x > color1_x:
        angle_text = str(int(360 - angle))
    
    #CHANGE FONT HERE
    cv2.putText(copy_frame, angle_text, (color1_x-30, color1_y), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 128, 229), 2)

cv2.imshow('AngleCalc', copy_frame)
cv2.waitKey(5) 

cap.release()
cv2.destroyAllWindows()

error

Traceback (most recent call last):
File "C:/Users/Besitzer/Real-Time-and-Static-Angles-Calculation-main/Real-Time-and-Static-Angles-Calculation-main/Angle_Findings Taha/color_based.py", line 72, in
(color1_x, color1_y), found_color1 = find_color1(copy_frame)
File "C:/Users/Besitzer/Real-Time-and-Static-Angles-Calculation-main/Real-Time-and-Static-Angles-Calculation-main/Angle_Findings Taha/color_based.py", line 21, in find_color1
, cnts, = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)
[ WARN:1@4.056] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

Process finished with exit code 1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests