Hand tracking and gesture recognition opencv python | Easy and Simple python module to control your computer with hand gestures

Welcome to my blog post, in this post I will share a simple and easy Python module for hand gesture control. Hand gesture or hand tracking is to recognize and detect hand movements in images or video using a computer algorithm. It has a lot of applications for example when we raise our hand to the phone camera it takes a selfie. With hand gestures we can control our computer like we can control the volume of our system, we can change the slides by using our hand finger, and many other things. Basically, there are two main libraries. Most people use opencv and media pipe, but for beginners, it's not easy to use so in this article I will be using cvzone which builds upon media pipe to make the implementation easy and simple. so let's start.





Required libraries:-

  1. Opencv to install open cmd and paste this ( pip install opencv-python )
  2. Mediapipe to install this open cmd and paste this ( pip install mediapipe )
  3. install cvzone ( pip install cvzone )
  4. Also if you install cvzone library so then you don't need to install opencv the 1 one because it includes the cvzone package. 
  5. So I recommend you install only cvzone

Start Programming :

  • First import  libraries and create objects 


        import cv2 will import the opencv library, and to import HandDetector we use HandTrackingModule from cvzone. Once our libraries are imported now we can use the libraries. Libraries are like our schools and universities once we get access now we can get what books we need. so here from cvzone, we need a Hand Detector that will detect the hand in our image or video. 
In HandDetector we have two parameters detectionCon and maxHands, maxHands is for how many hands we have to detect in the image or video so we are setting here 2 because we have two hands. But sometimes we need one hand like when we raise our hand and it takes a picture so we will use one hand. We will be detecting the hand from our computer camera so we need to access the camera so for that we are cv2 library function VideoCapture(0), This function takes only one parameter the camera number if we have two cameras and I want to use 2  one so will pass 1 but now we have only one so we are passing 0. 
 

  • Main Code
Now our camera is ready to open, read the images, and detect the hands in the images. We are using a loop to continuously capture frames (images ) from the camera. 


The read() function will read the images from the camera and will give us two things in return one is success which is not important the second is image ( img ). Great so now we get our image as img, so now our hand detector object will detect hands in this image. so lets our detectorHand call the function findHands( img and pass the parameter img. The magic is done call the cv2 to show your image with the function imshow("Image name", img) that takes your image names and the image you want to show on the screen. Your code is completed show your hand in to the camera and see it will draw points on your hand and detect it.

Important functions to remember :

  • read()
  • findHands(image)
  • imshow("image name", image)


Comments