The gesture's code with opencv doesn't count the finger

@legacy. Using RPI 3. I modified some extra:

    def calculateFingers(res, drawing):
    #  convexity defect
    cnt = 0 
    hull = cv2.convexHull(res, returnPoints=False)
    if len(hull) > 3:
        defects = cv2.convexityDefects(res, hull)
        if defects is not None:
            #cnt = 0
            for i in range(defects.shape[0]):  # calculate the angle
                s, e, f, d = defects[i][0]
                start = tuple(res[s][0])
                end = tuple(res[e][0])
                far = tuple(res[f][0])
                a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
                b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
                c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
                s = (a+b+c) / 2
                ar = math.sqrt(s * (s-a) * (s-b) * (s-c))
                d=(2*ar) / a
                angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) #* 57  # cosine theorem
                if angle <= math.pi / 2 and d >= math.pi / 6:  # angle less than 90 degree, treat as fingers
                    cnt += 1
                    cv2.circle(drawing, far, 8, [211, 84, 0], -1)
            if cnt > 0:
                return True, cnt #+ 1
            else:
                return True, 0
    return False, 0

Here is output:
original

Here is output. I put white paper behind my hand to get good gesture.
output
U may to have play around with values. I post some more code w/out using backgroundsubstractionMOG and w/out using white object such as walls.