├── 1.gif ├── README.md ├── ezgif-4-1135f6c536.gif ├── hand_detection1.py └── hand_detection_tracking.py /1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudonitin/Hand_detection_tracking_opencv-/87ba12c1a1e6eeeee1e7d7cdf118f197b6e30344/1.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Hand_detection_tracking_opencv

2 |

Recognizing gestures of hand and controlling the mouse. Use the hand_detection.py file to detect your hand and hand_detection_tracking.py to control mouse with your palm.

3 | 4 | NOTE: Change the bgr value range according to your skin color, provide a range i.e. skin color appears different when exposed to different amount of light 5 | 6 |

Demonstration show below

7 | 8 | 9 | ### Support Me 10 | If you liked this Repository, then please leave a star on this repository. It motivates me to contribute more in such Open Source projects in the future. 11 | ### Happy Coding =) 12 | -------------------------------------------------------------------------------- /ezgif-4-1135f6c536.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudonitin/Hand_detection_tracking_opencv-/87ba12c1a1e6eeeee1e7d7cdf118f197b6e30344/ezgif-4-1135f6c536.gif -------------------------------------------------------------------------------- /hand_detection1.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from sklearn.metrics import pairwise 4 | 5 | cap = cv2.VideoCapture(0) 6 | kernelOpen = np.ones((5,5))#if jiggers are present other than yellow area 7 | kernelClose = np.ones((20,20)) #if jiggers are present in yellow area 8 | 9 | #HSV color range which should be detected 10 | lb = np.array([20,100,100]) 11 | ub = np.array([120,255,255]) 12 | 13 | while True: 14 | ret, frame = cap.read() 15 | flipped = cv2.flip(frame, 1) 16 | flipped = cv2.resize(flipped,(500,400)) 17 | 18 | #use HSV of yellow to detect only yellow color 19 | imgSeg = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 20 | imgSegFlipped = cv2.flip(imgSeg, 1) 21 | imgSegFlipped = cv2.resize(imgSegFlipped,(500,400)) 22 | 23 | #masking and filtering all shades of yellow 24 | mask = cv2.inRange(imgSegFlipped, lb, ub) 25 | mask = cv2.resize(mask,(500,400)) 26 | 27 | #apply morphology to avoid jiggers 28 | maskOpen = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernelOpen) 29 | maskOpen = cv2.resize(maskOpen,(500,400)) 30 | maskClose = cv2.morphologyEx(maskOpen, cv2.MORPH_CLOSE, kernelClose) 31 | maskClose = cv2.resize(maskClose,(500,400)) 32 | 33 | final = maskClose 34 | _, conts, h = cv2.findContours(maskClose,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 35 | 36 | 37 | if(len(conts)!=0): #draws the contours of that object which has the highest 38 | b = max(conts, key=cv2.contourArea) 39 | west = tuple(b[b[:, :, 0].argmin()][0]) #gives the co-ordinate of the left extreme of contour 40 | east = tuple(b[b[:, :, 0].argmax()][0]) #above for east i.e right 41 | north = tuple(b[b[:, :, 1].argmin()][0]) 42 | south = tuple(b[b[:, :, 1].argmax()][0]) 43 | centre_x = (west[0]+east[0])/2 44 | centre_y = (north[0]+south[0])/2 45 | 46 | cv2.drawContours(flipped, b, -1, (0,255,0), 3) 47 | cv2.circle(flipped, west, 6, (0,0,255), -1) 48 | cv2.circle(flipped, east, 6, (0,0,255), -1) 49 | cv2.circle(flipped, north, 6, (0,0,255), -1) 50 | cv2.circle(flipped, south, 6, (0,0,255), -1) 51 | cv2.circle(flipped, (int(centre_x),int(centre_y)), 6, (255,0,0), -1)#plots centre of the area 52 | 53 | cv2.imshow('video', flipped) 54 | #cv2.imshow('mask', mask) 55 | #cv2.imshow('mask open', maskOpen) 56 | #cv2.imshow('mask close', maskClose) 57 | if cv2.waitKey(1) & 0xFF == ord(' '):#exiting 58 | break 59 | 60 | cap.release() 61 | cv2.destroyAllWindows() 62 | -------------------------------------------------------------------------------- /hand_detection_tracking.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import wx 4 | from pynput.mouse import Button, Controller 5 | 6 | mouse = Controller() 7 | 8 | app = wx.App(False) 9 | (screenx,screeny) = wx.GetDisplaySize() 10 | (capturex,capturey) = (700,500)#captures this size frame 11 | 12 | 13 | cap = cv2.VideoCapture(0) 14 | cap.set(3,capturex) 15 | cap.set(4,capturey) 16 | 17 | kernelOpen = np.ones((5,5))#if noise are present other than yellow area 18 | kernelClose = np.ones((20,20)) #if noise are present in yellow area 19 | 20 | #HSV color range which should be detected 21 | lb = np.array([20,100,100]) 22 | ub = np.array([120,255,255]) 23 | 24 | cd = 0 25 | 26 | while True: 27 | ret, frame = cap.read() 28 | 29 | #use HSV of yellow to detect only yellow color 30 | imgSeg = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 31 | #imgSegFlipped = cv2.resize(imgSegFlipped,(500,400)) 32 | 33 | #masking and filtering all shades of yellow 34 | mask = cv2.inRange(imgSeg, lb, ub) 35 | #mask = cv2.resize(mask,(500,400)) 36 | 37 | #apply morphology to avoid noise 38 | maskOpen = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernelOpen) 39 | #maskOpen = cv2.resize(maskOpen,(500,400)) 40 | maskClose = cv2.morphologyEx(maskOpen, cv2.MORPH_CLOSE, kernelClose) 41 | #maskClose = cv2.resize(maskClose,(500,400)) 42 | 43 | final = maskClose 44 | _, conts, h = cv2.findContours(maskClose,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 45 | 46 | 47 | if(len(conts)!=0): #draws the contours of that object which has the highest 48 | b = max(conts, key=cv2.contourArea) 49 | west = tuple(b[b[:, :, 0].argmin()][0]) #gives the co-ordinate of the left extreme of contour 50 | east = tuple(b[b[:, :, 0].argmax()][0]) #above for east i.e right 51 | north = tuple(b[b[:, :, 1].argmin()][0]) 52 | south = tuple(b[b[:, :, 1].argmax()][0]) 53 | centre_x = (west[0]+east[0])/2 54 | centre_y = (north[0]+south[0])/2 55 | 56 | cv2.drawContours(frame, b, -1, (0,255,0), 3) 57 | cv2.circle(frame, west, 6, (0,0,255), -1) 58 | cv2.circle(frame, east, 6, (0,0,255), -1) 59 | cv2.circle(frame, north, 6, (0,0,255), -1) 60 | cv2.circle(frame, south, 6, (0,0,255), -1) 61 | cv2.circle(frame, (int(centre_x),int(centre_y)), 6, (255,0,0), -1)#plots centre of the area 62 | 63 | bint = int(cv2.contourArea(b)) 64 | 65 | if (bint in range(8000,18000)): #hand is open 66 | mouse.release(Button.left) 67 | cv2.circle(frame, (int(centre_x),int(centre_y)), 6, (255,0,0), -1)#plots centre of the area 68 | mouse.position = (screenx-(centre_x*screenx/capturex),screeny-(centre_y*screeny/capturey)) 69 | 70 | elif(bint in range(2000,7000)): #hand is closed 71 | cv2.circle(frame, (int(centre_x),int(centre_y)), 10, (255,255,255), -1)#plots centre of the area 72 | mouse.position = (screenx-(centre_x*screenx/capturex), screeny-(centre_y*screeny/capturey)) 73 | mouse.press(Button.left) 74 | 75 | 76 | cv2.imshow('video', frame) 77 | if cv2.waitKey(1) & 0xFF == ord(' '):#exiting 78 | break 79 | 80 | cap.release() 81 | cv2.destroyAllWindows() 82 | --------------------------------------------------------------------------------