ValueError Not enough values to unpack

+1 vote

System information (version)

  • OpenCV => :Latest version
  • Operating System / Platform => : Windows 10 64 Bit
  • Compiler => : Jupyter Notebook (Anaconda)

Detailed description

ValueError: not enough values to unpack (expected 3, got 2)
When trying to run the code below (Motion Detector using Python)

Program

import cv2
import time

first_frame = None
# Create a VideoCApture object to record video using web Cam
video  = cv2.VideoCapture(0)

while True:
    check, frame = video.read()
    
    # Convert the frame to gray scale
    gray= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    # Convert the gray scale image to GaussianBlur
    gray = cv2.GaussianBlur(gray, (21,21),0)
    
    # To store first image/frame of video
    if first_frame is None:
        first_frame = gray
        continue
        
    # Calculate the difference between the first frame and other frames
    delta_frame = cv2.absdiff(first_frame, gray)
    
    # Provides a threshold value, such that it will convert 
    # the difference value with less than 30 to black.
    # If the difference is greater than 30 it will convert 
    # those pixels to white
    thresh_delta = cv2.threshold(delta_frame, 30,255, cv2.THRESH_BINARY)[1]
    thresh_delta = cv2.dilate(thresh_delta , None , iterations = 0)
    
    # Define the contour area.
    # Basically, add the borders.
    (_,cnts,_)= cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    # Remove noises and shadows. Basically it will keep only that 
    # part white, which has area greater than 1000 pixels.
    
    for contour in cnts:
        if cv2.contourArea(contour) <1000:
            continue
        
        # Creates a rectangular box around the object in the frame
        (x,y,w,h) = cv2.boundingRect(contour)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)
        
    cv2.imshow('frame', frame)
    cv2.imshow('capturing', gray)
    cv2.imshow('delta',delta_frame)
    cv2.imshow('thresh', thresh_delta)
    
    key = cv2.waitKey(1)
    if key == ord('q'):
        break
video.release()
cv2.destoyAllWindows

Error Screenshot:

Discover the Power of AI, Enroll now in the midjourney course today!image

Jun 24, 2019 in Machine Learning by Raph
18,686 views
Thanks ,it works
I Have the same problem

(new, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)

pls reply what should I do now

I encounter this problem also can you help me to find out what should i do? 

2 answers to this question.

+3 votes

Make the following changes in your script, it will work:

(cnts, _) = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)


Ready to dive into the world of machine learning? Supercharge your career and join our comprehensive Machine Learning Course today!

answered Jun 24, 2019 by Omkar
• 69,220 points
Thanks, it works for now.
Thanks it work for me..
Hello @Tushar and @asitha. I hope you guys are doing good. Please upvote the answer if it has helped you. Also, register ar Edureka community and earn credits for every contribution. A contribution could be anything from asking a question to answering or even upvoting/downvoting.

Thank you!
Thank you so much, it worked!! :)

It isn't working, giving the same error.

Can you share your screenshot once again? It is not visible properly.
It actually worked for me too. Thanks
+1 vote

Instead of this,

(_,cnts,_)= cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
try the below

contours, hierarchy = cv2.findContours(thresh_delta.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

answered Jan 29, 2020 by Shachi

Related Questions In Machine Learning

+1 vote
1 answer

ValueError: could not convert string to float in Machine learning.

Hi@akhtar, You are trying to use constant variable ...READ MORE

answered Apr 14, 2020 in Machine Learning by MD
• 95,460 points
26,921 views
0 votes
1 answer

How to compare expected and predicted values?

The predict() function returns a plain numpy ...READ MORE

answered Jul 14, 2019 in Machine Learning by Tina
13,214 views
+1 vote
1 answer

not able to see all columns and rows of a pandas DataFrame?

Hi@akhtar, Your data set contains lots of rows ...READ MORE

answered Apr 8, 2020 in Machine Learning by MD
• 95,460 points
5,461 views
0 votes
0 answers

Not able to import images.

It show my syntax is incorrect.Please help ...READ MORE

May 15, 2020 in Machine Learning by sangeethstanly
• 120 points
801 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,403 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP