Ask Your Question
0

I am learning Open CV using Python, and after doing some tutorials I found one exercise of hand gesture recognition but it gave me the next error :

asked 2017-07-27 22:19:36 -0600

NAOucr87 gravatar image

updated 2017-07-29 02:21:04 -0600

berak gravatar image
Traceback (most recent call last):
  File "F:\David\Varios\OPENCV\hand gestures.py", line 11, in <module>
    contours, hierarchy = cv2.findContours(th,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack
>>>

Here is the code:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (3,3),0)
    ret, th = cv2.threshold(blur,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    contours, hierarchy = cv2.findContours(th,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    areas = [cv2.contourArea(temp) for temp in contours]
    max_index = np.argmax(areas)
    largest_contour = contours[max_index]
    approx = cv2.approxPolyDP(largest_contour,0.01*cv2.arcLength(largest_contour,True),True)
    hull = cv2.convexHull(approx,returnPoints = True)
    cv2.putText(frame,'Number of Fingers ' + str (len(hull)-2),(10,30),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(255,0,0))
    cv2.drawContours(frame,[hull],0,(0,0,255),1)
    for i in range ( len( hull) ):
        [x,y] = hull[i] [0].flatten()
        cv2.circle(frame,(int(x), int(y)),2,(0,255,0),-1)
        cv2.circle(frame,(int(x), int(y)),5,(255,255,0),1)
        cv2.circle(frame,(int(x), int(y)),8,(255,0,0),1)
        print "Number of Fingers " + str ( (len(hull)-2) )
        cv2.imshow('Gestures',frame)

        if cv2.waitKey(5) == 27:
            break
    cv2.destroyAllWindows()
    cap.release()

The version of Open CV is 3.2 and python 2.7

Any help is really appreciated!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-07-29 02:29:02 -0600

berak gravatar image

updated 2017-07-29 03:36:06 -0600

the api has changed a bit with opencv3, it should be:

img, contours, hierarchy = cv2.findContours(th,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

(in other words, it returns more arguments, than you have variables there)

there is not much documentation, unfortunately, but still, in cases like this you should try the builtin help function:

>>> help(cv2.findContours)
Help on built-in function findContours:

findContours(...)
    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy
edit flag offensive delete link more

Comments

@NAOucr87 it might be a nice thing of you to do to contact whoever made the tutorial you followed and ask him/her to adapt it or add it as a change to their book online platform.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-07-29 07:57:38 -0600 )edit
0

answered 2017-07-28 10:26:45 -0600

Lukle gravatar image

Change the code from:

(contours, heirachy) = cv2.findcontours(etc)

to

contours, heirachy = cv2.findcontours(etc)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-07-27 22:16:33 -0600

Seen: 499 times

Last updated: Jul 29 '17