Hand Detection & Gesture Recognition – OpenCV Python

Hand Detection & Gesture Recognition

Hand gesture recognition is one of the most viable and popular solution for improving human computer interaction. In the recent years it has become very popular due to its use in gaming devices like Xbox, PS4, and other devices like laptops, smart phones, etc. Hand Detection & gesture recognition has usage in various applications like medicine, accessibility support etc. In this paper, we would like to propose on how to develop a hand gesture recognition simulation using OpenCV and python 2.7.

Histogram based approach is used to separate out the hand from the background image. Background cancellation techniques are used to produce optimum results. The detector hand is then processed and modelled by finding contours and convex hull to recognize finger and palm positions and dimensions. Finally a gesture object is created from the input which is then used to recognise the count of fingers.


This is a Computer vision package that makes its easy to run Image processing and AI functions. At the core it uses OpenCV and Mediapipe libraries.

You can simply use pip to install the latest version of cvzone.

pip install cvzone

Hand Tracking

Basic Code Example

import cvzone
import cv2

cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
detector = cvzone.HandDetector(detectionCon=0.5, maxHands=1)

while True:
    # Get image frame
    success, img = cap.read()

    # Find the hand and its landmarks
    img = detector.findHands(img)
    lmList, bbox = detector.findPosition(img)
    # Display
    cv2.imshow("Image", img)

Finding How many finger are up

if lmList:
        # Find how many fingers are up
        fingers = detector.fingersUp()
        totalFingers = fingers.count(1)
        cv2.putText(img, f'Fingers:{totalFingers}', (bbox[0] + 200, bbox[1] - 30),
                    cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)

Finding distace between two fingers

if lmList:
        # Find Distance Between Two Fingers
        distance, img, info = detector.findDistance(8, 12, img)
        cv2.putText(img, f'Dist:{int(distance)}', (bbox[0] + 400, bbox[1] - 30),
                    cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)

Find Hand Type – i.e. Left or Right

if lmList:
        # Find Hand Type
        myHandType = detector.handType()
        cv2.putText(img, f'Hand:{myHandType}', (bbox[0], bbox[1] - 30),
                    cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)

Gesture recognition(Hand Detection ) is an active research field in Human-Computer Interaction technology. It has many applications in virtual environment control and sign language translation, robot control, or music creation. In this machine learning project on Hand Detection, we are going to make a real-time Hand detector using the CVZONE module and OpenCV Python.

Prerequisites for this project:

  1. Python โ€“ 3.x (we used Python 3.8.8 in this project)
  2. OpenCV โ€“ 4.5 Run โ€œpip install opencv-pythonโ€ to install OpenCV.
  3. cvzone-1.5.2 Run “pip install cvzone” to install cvzone

To exit the app press q. Happy Coding !!!


About Diwas

๐Ÿš€ I'm Diwas Pandey, a Computer Engineer with an unyielding passion for Artificial Intelligence, currently pursuing a Master's in Computer Science at Washington State University, USA. As a dedicated blogger at AIHUBPROJECTS.COM, I share insights into the cutting-edge developments in AI, and as a Freelancer, I leverage my technical expertise to craft innovative solutions. Join me in bridging the gap between technology and healthcare as we shape a brighter future together! ๐ŸŒ๐Ÿค–๐Ÿ”ฌ

View all posts by Diwas →

Leave a Reply

Your email address will not be published. Required fields are marked *