Hand Detection Using OpenCV in Python With Source Code

The Real-Time Hand Detection OpenCV Python was developed using Python Detection OpenCV, Hand detection is a simple idea, but it requires some advanced tools to do it.

This project is one of the systems that can detect the gesture of hand in a real-time video.

A Hand Detection OpenCV Python is one of the most viable and popular solutions for improving human-computer interaction.

The code consists of a single file and can be executed from the command line or terminal by calling: “py main.py“.

Project Information

Project Name:Hand Detection OpenCV Python With Source Code
Language/s Used:Python (OpenCV)
Python version (Recommended):2.x or 3.x
Type:Deep Learning Project
Real-TIme Hand Detection with Source Code – Project Information

About Hand Detection OpenCV Python

This OpenCV Python Project also includes a downloadable Python Project With Source Code for free, just find the downloadable source code below and click to start downloading.

When running the Python file, you can expect to see the real-time frame from your camera with a bounding rectangular framing in your hand.

By the way, if you are new to Python programming and don’t know how to use the Python IDE, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS that will suit you.

I also have here How to Download and Install the Latest Version of Python on Windows.

To start executing a Real-Time Hand Detection With Source Code, make sure that you have installed Python on your computer.

Hand Detection using OpenCV in Python Tutorial With Source Code

Time needed: 5 minutes

These are the steps on how to run Real-Time Hand Detection OpenCV Python With Source Code

  • Step 1: Download the given source code below.

    First, download the given source code below and unzip the source code.
    Hand Detection download source code

  • Step 2: Import the project to your PyCharm IDE.

    Next, import the source code you’ve downloaded to your PyCharm IDE.
    Hand Detection open project

  • Step 3: Run the project.

    Lastly, run the project with the command “py main.py”
    Hand Detection run project

Installed Libraries

import cv2

Complete Source Code

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_hand.xml')
# To capture video from webcam. 
cap = cv2.VideoCapture(0)
# To use a video file as input 
# cap = cv2.VideoCapture('filename.mp4')
while True:
    # Read the frame
    _, img = cap.read()
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
    # Display
    cv2.imshow('img', img)
    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k==27:
# Release the VideoCapture object


OpenCV Python Hand Detection Output
OpenCV Hand Detection Output

Download the Source Code below


In this tutorial, you will learn about detecting a Hand with the feature mappers known as haar cascades.

Here in the project, we will use the Python language along with the OpenCV library for the algorithm execution and image processing respectively.

The haar cascades we are going to use in the project are pretrained and stored along with the OpenCV library as haarcascade_hand.xml files.

The project develops a basic understanding of the systems such as hand detection and also the haar cascades usage with the OpenCV library.


If you have any questions or suggestions about Real-Time Hand Detection OpenCV Python With Source Code, please feel free to leave a comment below.

Leave a Comment