diff --git a/ClenchRex.py b/ClenchRex.py index ee5366f..4f75d63 100644 --- a/ClenchRex.py +++ b/ClenchRex.py @@ -3,42 +3,42 @@ import math import pyautogui +# pass source as 0 (zero) for inbuilt webcam, 1 or -1 for external camera cap = cv2.VideoCapture(0) -while (1): +while (True): try: + # an error comes if it does not find anything in window as it cannot find contour of max area + # therefore this try error statement + # obtain frame and kernel matrix ret, frame = cap.read() frame = cv2.flip(frame, 1) kernel = np.ones((3, 3), np.uint8) - # defining region of interest + # define region of interest roi = frame[100:300, 100:300] - # Making a rectangle around the region of interest + # convert to HSV cv2.rectangle(frame, (100, 100), (300, 300), (0, 255, 0), 0) hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) - # defining skin range for HSV + # define range of skin color in HSV lower_skin = np.array([0, 20, 70], dtype=np.uint8) upper_skin = np.array([20, 255, 255], dtype=np.uint8) - # Green glove skin : Doesn't work - #lower_skin = np.array([66, 75, 82], dtype=np.uint8) - #upper_skin = np.array([96, 75, 82], dtype=np.uint8) - - # extracting skin color image + # extract skin colur image mask = cv2.inRange(hsv, lower_skin, upper_skin) - # Dilating + # extrapolate the hand to fill dark spots within mask = cv2.dilate(mask, kernel, iterations=4) - # blurring the image + # blur the image mask = cv2.GaussianBlur(mask, (5, 5), 100) # find contours - contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + _, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # find contour of max area(hand) cnt = max(contours, key=lambda x: cv2.contourArea(x)) @@ -47,7 +47,7 @@ epsilon = 0.0005 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) - # making convex hull + # make convex hull around hand hull = cv2.convexHull(cnt) # define area of hull and area of hand @@ -61,10 +61,8 @@ hull = cv2.convexHull(approx, returnPoints=False) defects = cv2.convexityDefects(approx, hull) - # l = no. of defects + # finding number of defects due to fingers (= l) l = 0 - - # code for finding no. of defects due to fingers for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] start = tuple(approx[s][0]) @@ -85,7 +83,7 @@ # apply cosine rule here angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) * 57 - # ignore angles > 90 and ignore points very close to convex hull(they generally come due to noise) + # ignore angles > 90 and ignore points very close to convex hull (generally noise induced points) if angle <= 90 and d > 30: l += 1 cv2.circle(roi, far, 3, [255, 0, 0], -1) @@ -93,6 +91,7 @@ # draw lines around hand cv2.line(roi, start, end, [0, 255, 0], 2) + # minimum one defect for hand l += 1 # print corresponding gestures which are in their ranges @@ -103,22 +102,19 @@ else: if arearatio < 12: cv2.putText(frame, 'Running', (0, 50), font, 2, (0, 0, 255), 3, cv2.LINE_AA) - #pyautogui.keyDown('down') - - elif l > 1: cv2.putText(frame, 'Jump', (0, 50), font, 2, (0, 0, 255), 3, cv2.LINE_AA) pyautogui.press('space') - # show the windows cv2.imshow('mask', mask) cv2.imshow('frame', frame) + except: pass - k = cv2.waitKey(5) & 0xFF - if k == 27: + if cv2.waitKey(5) & 0xFF == 27: break cv2.destroyAllWindows() +cap.release() diff --git a/README.md b/README.md index 72a08e2..6f8ad07 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,9 @@ How to play: 3. Put your clenched fist in the Green Box, and start the game. 4. Open your fist to Jump, clench back again to stop Jumping. +Install on Linux: + +`pip3 install opencv-python` References used: https://github.com/Sadaival/Hand-Gestures/blob/master/gesture.py