0

I tried running the code below and I came across a ValueError. My expected output was that the code would open up four camera windows that are all different. Can anyone please help me?

This is my code down below.

import cv2

firstframe = None

video = cv2.VideoCapture(0)

while True:
    check, frame = video.read()
    status = 0
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    if firstframe is None:
       firstframe = gray
       continue

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

    for contour in cnts:
        if cv2.contourArea(contour) < 10000:
            continue
        status = 1
        (x, y, w, h) = cv2.boundingRect(contour)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

    cv2.imshow("Gray Frame", gray)
    cv2.imshow("Delta Frame", delta_frame)
    cv2.imshow("Threshold Frame", thresh_frame)
    cv2.imshow("Normal Frame", frame)

    key = cv2.waitKey(1)

    if key == ord('q'):
        break
    print(status)
    
video.release()
cv2.destroyAllWindows

The error I got was this:

 cnts = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
EpicNinja
  • 15
  • 5
  • Just to add to the posted answer, if you want the second element, even if the return of the function is more than 2 elements, you could use ```(_,cnts,*_) = ...```. – sj95126 Sep 18 '21 at 15:05
  • Does this answer your question? [Want to find contours -> ValueError: not enough values to unpack (expected 3, got 2), this appears](https://stackoverflow.com/questions/54164630/want-to-find-contours-valueerror-not-enough-values-to-unpack-expected-3-go) – Christoph Rackwitz Jul 15 '22 at 11:37

1 Answers1

0

cv2.findContours returns 2 things (rather than 3). Those two are contours,hierarchy.

Change from (_,cnts,_) = cv2.findContours... to contours,hierarchy = cv2.findContours...

saedx1
  • 984
  • 6
  • 20
  • question should be flagged as a **duplicate** of https://stackoverflow.com/questions/54164630/want-to-find-contours-valueerror-not-enough-values-to-unpack-expected-3-go – Christoph Rackwitz Jul 15 '22 at 11:38