DEV Community

Cover image for Hand Gesture
Sahil Tripathi
Sahil Tripathi

Posted on • Edited on

Hand Gesture

Hello Guys,😊 This is my First Blog Hope you will like it

So I have tried to make this Hand Gesture using Computer Vision-
Alt Text

Before Proceeding to this Project please refer to this important Documentation

1-Eroding and Dilation
2-Morphological transformation
3-Convex Hull
4-Contours

Algorithms -

1- Importing necessary libraries cv2, NumPy and math

2-Now we apply gaussian blur to smoothing the image (it removes noise from image) and convert it into BGR ->HSV Image(Huge saturation value)

3-Now we use the morphological technique so that our image our feed won't lose the information.

5- We use dilate and erode morphological (as they are used to filter out the background noise) and apply the threshold to filtered images.

6- Now we will try to find the contours (as it is used for colour and shape analysis)we will find the maximum area of contour by using the function "max", "cv2.contourArea

7- Afterwards we use convex hull well its do the same thing in two different formats.

8- Now we will detect the fingertips by count_defect its not an inbuilt function but what is do is when we will apply the hand it will detect our hand

9- At last it will display the number like if count_defects ==0 it shows 1 and so on...

Code for this Project

# Importing Modules

import numpy as np
import cv2
import time

print("HELLO FRANDSSSSSSSSSSSS ENJOY HARRY POTTER CLOAK ")

# Capturing Webcam Feed
#cap = cv2.VideoCapture('http://192.168.43.200:8080/video?640x480')
cap = cv2.VideoCapture(0)

time.sleep(3)  #providing 3 second of time to adjust

count = 0

background = 0  # capturing back image when we use cloak

# Capturing Static Background Frame

for i in range(60):  # provided 60 iteration to capturing the background
    ret, background = cap.read()

# Flip the Image

background = np.flip(background, axis=1)

while (cap.isOpened()): # till running this will executing

    ret, img = cap.read()  # capturing our image to perform operation

    if not ret:
        break

    count += 1

    img = np.flip(img, axis=1)

    # Converting from BGR to HSV
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #img=source input BGR=red,blue,green

    lower_red = np.array([0, 120, 70]) #using lower red color to determine cloak
    upper_red = np.array([10, 255, 255])
    mask1 = cv2.inRange(hsv, lower_red, upper_red) #for 170 to 180

    lower_red = np.array([170, 120, 70])
    upper_red = np.array([180, 255, 255])
    mask2 = cv2.inRange(hsv, lower_red, upper_red)

    mask1 = mask1 + mask2  #overloading as what shade of color it will be it will segemnted(170-180) degree

    # morphology is used to remove the noise or distortions

    # mask1 is input image morph is operation we are doing np.ons willl create matrix 3,3

    mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8), iterations=2) # 2 reduce noise 

    # using dilate to thickness and smoothing the images 
    mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8), iterations=1)



    mask2 = cv2.bitwise_not(mask1) # except the cloak

    # it will give result when the background will be there
    res1 = cv2.bitwise_and(background, background, mask=mask1) # differentiate clock color wrt background

    # when i will be in image

    res2 = cv2.bitwise_and(img, img, mask=mask2) # substuting the cloak part
    final_output = cv2.addWeighted(res1, 1, res2, 1, 0) #addition of res1 and res2 1 and 0 is a equation
    # aplha time source image 0 is gaama 
    # adding linearly image 

    cv2.imshow('HOLA HUNTER', final_output)
    k = cv2.waitKey(10)
    if k == 27: # using esacpe key  
        break

cap.release()
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

Suggestions are welcome 😊

🤝🏻 Reach me out!

Instagram : @dynamic__geek

Top comments (0)