Change language

Writing a palm recognition system in Python OpenCV

| | | |

Hello all, today I show you how to write a palm recognition system in Python + OpenCV (26 lines of code). This tutorial requires minimal knowledge of OpenCV.

What we get in the output

Roughly this:

Useful Links

Before we start

You will need:

  • Python 3;
  • OpenCV;
  • mediapipe.

Installation is done through the package manager:

pip install opencv-python
pip install mediapipe

Writing the code

I decided to use mediapipe because it has an AI already trained to recognize the palm.

First you need to understand that the palm is made up of joints, which can be expressed as dots:

Next, we write the code itself:

import cv2
import mediapipe as mp

cap = cv2.VideoCapture(0) #Camera
hands = mp.solutions.hands.Hands(max_num_hands=1) #AI object for palm definition
draw = mp.solutions.drawing_utils #For drawing the palm

while True:
    #Closing the window
    if cv2.waitKey(1) & 0xFF == 27:
        break

    success, image = cap.read() #read image from camera
    image = cv2.flip(image, -1) #Flip the image for the correct image
    imageRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #Convert to rgb
    results = hands.process(imageRGB) #work on mediapipe

    if results.multi_hand_landmarks:
        for handLms in results.multi_hand_landmarks:
            for id, lm in enumerate(handLms.landmark):
                h, w, c = image.shape
                cx, cy = int(lm.x * w), int(lm.y * h)

            draw.draw_landmarks(image, handLms, mp.solutions.hands.HAND_CONNECTIONS)

    cv2.imshow("Hand", image) #Display the image

The solution to the error: ImportError: DLL load failed can be found here.

There are comments in the code, but I suggest breaking it down in detail:

OpenCV and mediapipe imports:

import cv2
import mediapipe as mp

Necessary objects: camera (cap), neuronics to determine the hands (hands) with arguments max_num_hands - the maximum number of hands - and our "draw".

cap = cv2.VideoCapture(0)
hands = mp.solutions.hands.Hands(max_num_hands=1)
draw = mp.solutions.drawing_utils

Infinite loop:

while True:

Exit from loop if key number 27 is pressed (Esc):

if cv2.waitKey(1) & 0xFF == 27:
    break

We read the image from the camera, reflect the image vertically and horizontally, convert it to RGB. And, most interestingly, we give our picture to the palm definition.

success, image = cap.read()
image = cv2.flip(image, -1)
imageRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(imageRGB)

The first if-block checks if palms were found at all (any object that is not empty is true). Then we use the for loop to "loop" through the object with the set of these points. The second for - go through the points themselves from the set

draw.draw_landmarks is a handy utility that draws a palm on an image, with the following arguments: image, set of points, and what to draw (in our case, a hand).

if results.multi_hand_landmarks:
    for handLms in results.multi_hand_landmarks:
        for id, lm in enumerate(handLms.landmark):
            h, w, c = image.shape
            cx, cy = int(lm.x * w), int(lm.y * h)

        draw.draw_landmarks(image, handLms, mp.solutions.hands.HAND_CONNECTIONS)

Display the picture.

cv2.imshow("Hand", image)

Bottom line

We wrote palm recognition in Python in 26 lines of code, cool isn't it?

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically