-4

I have a problem with python program( line 22 -(_cnts_) ). I use win11, PyCharm. I am getting error ValueError: not enough values to unpack (expected 3, got 2).

import cv2, time
import pandas

first_frame = None
video = cv2.VideoCapture(0)

while True:

    check, frame = video.read()
    print(frame)

    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray,(21,21),0)

    if first_frame is None:
        first_frame = gray
        continue

    delta_frame = cv2.absdiff(first_frame,gray)
    thresh_delta = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
    thresh_delta = cv2.dilate(thresh_delta, None, iterations=0)
    (_,cnts,_) = cv2.findContours(thresh_delta.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

    for contour in cnts:
        if cv2.contourArea(contour) < 1000:
            continue
        (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)

    cv2.imshow('Capturing #by ensar',gray)

    key = cv2.waitKey(1)

    if key == ord('q'):
        break

video.release()
cv2.destroyAllWindows()

problems start when I am trying to add this line

(_,cnts,_) = cv2.findContours(thresh_delta.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Ensar Yesir
  • 1
  • 1
  • 2
  • there are 3 items in the tuple `( _ , cnts , _ )` so you are expecting 3 items. presumably the function `cv2.findContours` only returns 2. So this is your problem. – D.L Mar 25 '22 at 17:37
  • change the code to `cnts = cv2.findContours(thresh_delta.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)` and it proceeds... – D.L Mar 25 '22 at 17:41
  • 1
    bad idea. if you do what you suggest, `cnts` is now a 2-tuple or 3-tuple, and it's NOT a list of contours, but a list of contours, a hierarchy, and whatever else – Christoph Rackwitz Mar 25 '22 at 17:46
  • 1
    @EnsarYesir please take the [tour] and review [ask]. you should search for the error before asking. – Christoph Rackwitz Mar 25 '22 at 17:46

2 Answers2

-1

findContours method returns two values: contours and hierarchy but you're expecting three: _,cnts,_. Change to this:

cnts, _ = cv2.findContours(thresh_delta.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
Nilton Moura
  • 313
  • 2
  • 8
-2

I modify the error line to this:

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

The return value of the above is (seen in the debugger):

cnts = ((), None)

So this will fix the problem, but the code will have another problem later on since it is using this value in a loop and its contents are an empty tuple and a None.

D.L
  • 4,339
  • 5
  • 22
  • 45
  • 4
    please don't. this is not a generic python question. this is a question specific to OpenCV. this question is a duplicate and the top result for this error has plenty of well reasoned answers. – Christoph Rackwitz Mar 25 '22 at 17:48
  • 1
    This is dangerous advice, please do not follow it. – stateMachine Mar 26 '22 at 02:02
  • @stateMachine, for my understanding why is this bad advice please ? this is what i observer when running the code... i observe that the change proposed would not get the code to run to completion on its own. – D.L Mar 26 '22 at 08:04
  • 2
    the goal isn't to shut up the exceptions. the goal is to make the code work. your answer doesn't do that. you don't understand how to use this OpenCV API. the other answer makes the code work. that answerer understands the API. it's that simple. you should familiarize yourself with a library (or at least that one API call) when you try to give advice on it. – Christoph Rackwitz Mar 26 '22 at 08:20
  • so you are saying that the answer should have referred to: https://docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0 which shows the two return values `contours`, `hierarchy` specifically and omitted the observation that they were `()`, `None` ? – D.L Mar 26 '22 at 08:34
  • Ideally no one should have answered, because the question is easily identified as duplicate. Add a comment with a link. – Timus Mar 26 '22 at 09:42