Skip to content

Commit

Permalink
Merge pull request #1 from ildarkhasanshin/master
Browse files Browse the repository at this point in the history
* edited comments
  • Loading branch information
Kshitij05 authored Nov 18, 2020
2 parents 00692d8 + 5466d79 commit fb891b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
42 changes: 19 additions & 23 deletions ClenchRex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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])
Expand All @@ -85,14 +83,15 @@
# 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)

# 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
Expand All @@ -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()
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fb891b6

Please sign in to comment.