Ask Your Question
0

Hon can I fixed this "ValueError: too many values to unpack (expected 2)"

asked 2019-08-29 04:03:38 -0600

updated 2019-08-29 05:30:25 -0600

LBerger gravatar image

This error shows when I run the code on Anaconda(jupyter notebook), but it's work fine in pycharm. The full error message is " ValueError Traceback (most recent call last) <ipython-input-3-473e4a69ef15> in <module>

     30     dilated = cv2.dilate(thresh, None, iterations=3)
     31     contours, _ = cv2.findContours(dilated, cv2.RETR_TREE,
---> 32                                    cv2.CHAIN_APPROX_SIMPLE)
     33 
     34     k = cv2.drawContours(frame1, contours, -1, (0, 255, 0), 2)

ValueError: too many values to unpack (expected 2)"

And the code is :

cap = cv2.VideoCapture(0)
frame_width = int( cap.get(cv2.CAP_PROP_FRAME_WIDTH))

frame_height =int( cap.get( cv2.CAP_PROP_FRAME_HEIGHT))

fourcc = cv2.VideoWriter_fourcc('X','V','I','D')

out = cv2.VideoWriter("output.avi", fourcc, 5.0, (1280,720))


ret, frame1 = cap.read()
ret, frame2 = cap.read()

camera_height = 500
raw_frames_type_1 = []


while cap.isOpened():
    _, frame = cap.read()

    aspect = frame.shape[1] / float(frame.shape[0])
    res = int(aspect * camera_height)  # landscape orientation - wide image
    frame = cv2.resize(frame, (res, camera_height))
    cv2.rectangle(frame, (300, 75), (650, 425), (0, 255, 0), 2)


    cv2.imshow("Capturing frames", frame)


    diff = cv2.absdiff(frame1, frame2)
    gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5), 0)
    _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
    dilated = cv2.dilate(thresh, None, iterations=3)
    contours, _ = cv2.findContours(dilated, cv2.RETR_TREE,
                                   cv2.CHAIN_APPROX_SIMPLE)

    k = cv2.drawContours(frame1, contours, -1, (0, 255, 0), 2)


    if k.all() == True:
        raw_frames_type_1.append(frame)
        print("hi")

    else:
        print ("bye")

    image = cv2.resize(frame1, (1280,720))
    out.write(image)
    cv2.rectangle(frame1, (300, 75), (650, 425), (0, 0, 255), 2)
    cv2.imshow("feed", frame1)
    frame1 = frame2
    ret, frame2 = cap.read()

    key = cv2.waitKey(90)

    if key & 0xFF == ord("q"):
        break

cv2.destroyAllWindows()
cap.release()
out.release()

save_width = 300
save_height = 300

for i in range(1, 2):
    name = 'F:/test/images_type_{}'.format(i)
    os.makedirs(name, exist_ok=True)
for i, frame in enumerate(raw_frames_type_1):
    roi = frame[75+2:425-2, 300+2:650-2]
    roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
    roi = cv2.resize(roi, (save_width, save_height))
    cv2.imwrite('F:/test/images_type_1/{}.png'.format(i),
                cv2.cvtColor(roi,cv2.COLOR_BGR2RGB))
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2019-08-29 05:37:18 -0600

LBerger gravatar image

What's your opencv version?

Use the doc and read it carrefully.

findContours in opencv 4.1.1-dev

findContours in opencv 3.4.7-dev

All docs are here and you can change version id doc page header

image description

edit flag offensive delete link more

Comments

My OpenCv version is 3.4.2

mohammad al-amin gravatar imagemohammad al-amin ( 2019-08-31 01:19:48 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-08-29 04:03:38 -0600

Seen: 1,658 times

Last updated: Aug 29 '19