ValueError: Not enough values to unpack

0 votes

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)


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 =
    # 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
    # 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:
        # 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('thresh', thresh_delta)
    key = cv2.waitKey(1)
    if key == ord('q'):

Error Screenshot:


Jun 23 in Machine Learning by Raph
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

1 answer to this question.

+1 vote

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

(cnts, _) = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
answered Jun 24 by Omkar
• 67,620 points
Thanks, it works for now.

Related Questions In Machine Learning

0 votes
1 answer

Example to run KNN algorithm using python

Have a look at this one: from sklearn.datasets ...READ MORE

answered May 8 in Machine Learning by Harvy
0 votes
1 answer
0 votes
1 answer
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 278 views