Skip to main content

pygame_gesture_kit

Easy camera based hand tracking and gesture recognition for pygame-ce

Take a look at the PyGame-ComputerVision-Racer 🚗 game for an example project.

How it works

pygame_gesture_kit uses MediaPipe and OpenCV to capture a video feed from a camera and detect hands in these images.

The camera frame is mapped to the pygame window and tracking coordinates for all hand landmarks can be acquired by calling the function get_hands() from within the game loop. It also provides information on detected gesture. Capturing from a camera from within the game loop would limit the frame rate of your game to that of the camera, which might cause the framerate to drop to 30 fps or even lower. To prevent this, pygame_gesture_kit executes video capturing and processing in a separate thread.

The gesture recognition uses a customized model, trained from the HaGRID dataset. It provides the following labels:

  • none if no gesture was detected,
  • fist for a closed hand,
  • one finger: one
  • two fingers: two_up, two_up_inverted, peace, peace_inverted
  • three fingers: three, three2
  • four fingers: four
  • five fingers: palm, stop, stop_inverted
  • thumbs up/down: like, dislike

How to use it

Important note: MediaPipe is currently only available for Python 3.12 and older so you can't use pygame_gesture_kit with Python 3.13/3.14 at this time.

A comprehensive example is provided in the example directory. Basically, you need to import and use two classes in your code: the Camera and GestureRecognizer.

from pygame_gesture_kit import GestureRecognizer, Camera
import pygame_gesture_kit.hand_visualizer

The Camera class wraps an OpenCV capture device and uses the same index numbers to specify devices. Unfortunately, OpenCV does not provide an interface to enumerate cameras, so you will have to guess, which is the right index for you. It will usually be 0, but on macOS that index may also be the continuity camera. Try 1 and higher numbers if you don't get the camera you want.

You can start capturing and recognizing hands easily like this:

capture_device = Camera()
try:
    capture_device.open_camera(0)
except Exception as e:
    print(e)
    exit(1)
gesture_recognizer = GestureRecognizer(capture_device, max_hands=2)
gesture_recognizer.start()

The number of visible hands can be determined by the gesture_recognizer.get_visible_hands() function.

The following code snippet illustrates how to process data from the get_hands() function. It uses the hand_visualizer to draw the detected landmarks on a surface and adds labels for the detected gestures.

i_hand = 0
for hand in gesture_recognizer.get_hands():
    hand_visualizer.draw_bones(screen, hand, joint_label_font=small_font)
    i_hand += 1
    text = font.render(f'Hand {i_hand}', 1, pygame.Color('black'), pygame.Color('white'))
    x, y = hand.landmarks[0]
    screen.blit(text, (x, y))
    text = font.render(f'Gesture: {hand.gesture}', 1, pygame.Color('black'), pygame.Color('white'))
    screen.blit(text, (x, y + 24))

Installing

You can install pygame_gesture_kit using the PyPI package manager:

python -m pip install pygame-gesture-kit

alternatively, install it directly from this github repo:

python -m pip install git+https://github.com/FreeBugs/pygame_gesture_kit.git

If you are using PyCharm, you can install the package from the Package Manager by selecting Add Package -> From Version Control.

Contributing

  1. (Optional) Fork the repo
  2. Clone the repo
git clone https://github.com/FreeBugs/pygame_gesture_kit.git
  1. Install dependencies
cd pygame_gesture_kit
python3 -m pip install .
  1. Run the example
python3 src/pygame_gesture_kit/example/main.py
  1. Feel free to open a pull request if you add something or make pygame_gesture_kit better.

Release files for pygame-gesture-kit 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygame-gesture-kit 0.0.3
File Size Uploaded
pygame_gesture_kit-0.0.3.tar.gz 12.8 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pygame-gesture-kit 0.0.3
File Interpreter ABI Platform
pygame_gesture_kit-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 25.7 MB

Release files / pygame_gesture_kit-0.0.3.tar.gz

Download URL pygame_gesture_kit-0.0.3.tar.gz
Size 12.8 MB
Tags Source
SHA-256 checksum
How to use checksums
dc2384d3e2bf2e8fb13a0b32c3559938937eeb5ee370b5554e830e63f79b63cb
BLAKE2b-256 checksum
How to use checksums
f8465b861a44fa50e4aa3eb4d151292c89d0c3fea26a95361a3a04b7cb3d0a1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / pygame_gesture_kit-0.0.3-py3-none-any.whl

Download URL pygame_gesture_kit-0.0.3-py3-none-any.whl
Size 12.9 MB
Tags Python 3
SHA-256 checksum
How to use checksums
ecfd32238c6b40d6c011b4a10b37794bbee7c7652bf94a46c96a212beb52c6bc
BLAKE2b-256 checksum
How to use checksums
06aae455380a85feea8a39c7cbb37637c000f68d3a0787556366df1800618a09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page