Skip to content

mohitr2111/3dCubeControl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ–๏ธ 3dCubeControl โ€” Hand Gesture 3D Control

Control a 3D Cube in Real-Time using only your Hand

Python OpenCV MediaPipe PyGame PyOpenGL License


๐Ÿ“ธ Preview

Live Demo Screenshot

3dCubeControl in Action

Left: 3D cube with neon cyberpunk theme rendered in OpenGL | Right: Real-time hand tracking with MediaPipe โ€” 21 landmark detection, neon cyan skeleton, pink joint dots


๐ŸŽฌ Live Demo Video

https://github.com/mohitr2111/3dCubeControl/raw/main/ScreenShot/2.mp4

Full demo โ€” rotating, zooming and controlling the 3D cube using bare hand gestures at 32 FPS


๐ŸŒŸ What is 3dCubeControl?

3dCubeControl is a real-time interactive application that lets you manipulate a 3D cube using nothing but your hand. No controller, no mouse, no touch โ€” just your hand in front of a webcam.

Built on top of Google's MediaPipe Tasks API for hand landmark detection and OpenGL for 3D rendering, the system detects 21 precise hand landmarks per frame and translates your hand movements into smooth 3D transformations โ€” rotation on X and Y axes and zoom โ€” all at real-time speed.

The UI features a dark neon / cyberpunk theme โ€” deep navy background, vivid neon-colored cube faces, bright white edges, and a neon skeleton hand overlay.


โœจ Features

Feature Description
๐Ÿ–๏ธ 21-Point Hand Tracking Full hand skeleton with MediaPipe Tasks API
๐ŸŽฒ Real-Time 3D Rendering OpenGL-powered cube with Phong lighting
๐ŸŒ€ Multi-Axis Rotation Rotate on X and Y axes simultaneously
๐Ÿ” Pinch-to-Zoom Smooth zoom with pinch gesture
๐Ÿ’ค Idle Auto-Rotation Cube slowly rotates when no hand detected
๐ŸŽจ Neon Cyberpunk Theme Dark navy bg, vivid neon cube faces, white edges
๐Ÿ“Š Live HUD Overlay Real-time FPS, Rotation X/Y, Zoom on screen
โšก Smooth Gesture Filtering Moving average + exponential smoothing
๐Ÿ”„ Auto Model Download MediaPipe model auto-downloads on first run
โŒจ๏ธ Keyboard Shortcuts Reset, Quit via keyboard

๐ŸŽฎ Controls

โœ‹ Hand Gestures

Gesture Action
๐Ÿ–๏ธ Open hand + move Left / Right Rotate cube on Y-axis
๐Ÿ–๏ธ Open hand + move Up / Down Rotate cube on X-axis
๐Ÿค Pinch (Thumb + Index closer) Zoom In
๐Ÿค Pinch (Thumb + Index apart) Zoom Out
โœŠ Close hand / Remove hand Pause โ€” cube enters idle rotation

โŒจ๏ธ Keyboard

Key Action
R Reset cube to default position
Q or ESC Quit application

๐Ÿ—๏ธ Project Structure

3dCubeControl/
โ”‚
โ”œโ”€โ”€ main.py                  # Main application (all logic)
โ”œโ”€โ”€ requirements.txt         # Python dependencies
โ”œโ”€โ”€ README.md                # This file
โ”œโ”€โ”€ LICENSE                  # MIT License
โ”œโ”€โ”€ .gitignore               # Git ignore rules
โ”‚
โ”œโ”€โ”€ ScreenShot/
โ”‚   โ”œโ”€โ”€ 1.png                # Live demo screenshot
โ”‚   โ””โ”€โ”€ 2.mp4                # Live demo screen recording
โ”‚
โ””โ”€โ”€ hand_landmarker.task     # MediaPipe model (auto-downloaded, gitignored)

โš™๏ธ How It Works

Webcam Frame
     โ”‚
     โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   OpenCV Capture    โ”‚  640ร—480, flipped (mirror)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  MediaPipe Tasks    โ”‚  Detects 21 hand landmarks per frame
โ”‚  HandLandmarker     โ”‚  VIDEO mode @ ~30fps
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  GestureController  โ”‚  Palm center โ†’ rotation delta
โ”‚                     โ”‚  Pinch distance โ†’ zoom delta
โ”‚                     โ”‚  Exponential smoothing applied
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   OpenGL Renderer   โ”‚  800ร—600 window
โ”‚   (Pygame + OpenGL) โ”‚  Phong lighting (2 lights)
โ”‚                     โ”‚  6-face colored cube + grid
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽจ UI Theme โ€” Dark Neon / Cyberpunk

Element Color / Style
๐ŸŒŒ Background Deep dark navy (0.04, 0.04, 0.10)
๐ŸŸฅ Cube Front Neon Red (1.0, 0.08, 0.08)
๐ŸŸฉ Cube Back Neon Green (0.0, 1.0, 0.3)
๐ŸŸฆ Cube Top Electric Blue (0.0, 0.5, 1.0)
๐ŸŸจ Cube Bottom Neon Yellow (1.0, 0.95, 0.0)
๐ŸŸช Cube Right Neon Purple (0.8, 0.0, 1.0)
๐Ÿฉต Cube Left Neon Cyan (0.0, 1.0, 0.9)
โฌœ Cube Edges Bright White โ€” 2.5px
๐Ÿ”ท Grid Dark Neon Blue
๐Ÿซ€ Hand Skeleton Neon Cyan lines
๐Ÿ’œ Hand Joints Neon Pink filled dots

๐Ÿ”ง Technical Architecture

Classes Overview

Config             โ†’  All tunable constants (resolution, sensitivity, zoom limits)
HandTracker        โ†’  MediaPipe Tasks API wrapper โ€” detects landmarks, draws skeleton
GestureController  โ†’  Converts landmark positions to rotation/zoom deltas with smoothing
Renderer3D         โ†’  OpenGL cube + grid drawing, Pygame window management
App                โ†’  Main loop โ€” ties everything together, HUD overlay

Key Config Parameters

class Config:
    WEBCAM_WIDTH  = 640           # Webcam resolution
    WEBCAM_HEIGHT = 480
    RENDER_WIDTH  = 800           # 3D window size
    RENDER_HEIGHT = 600
    ROTATION_SENSITIVITY = 0.3    # Higher = faster rotation
    ZOOM_SENSITIVITY     = 0.015  # Higher = faster zoom
    smoothing_factor_size = 0.4   # Lower = more responsive, higher = smoother
    ZOOM_MIN = -15.0              # Max zoom out
    ZOOM_MAX = -2.0               # Max zoom in
    ZOOM_DEFAULT = -6.0           # Starting zoom level

Smoothing Algorithm

The gesture controller uses a two-stage smoothing pipeline:

  1. Exponential Moving Average โ€” blends current delta with previous smooth value
smooth_dx = alpha * delta[0] + (1 - alpha) * smooth_dx
  1. Moving Average Window โ€” averages last 5 frames for stability
history_x = deque(maxlen=5)
rotation_y += mean(history_x) * ROTATION_SENSITIVITY

๐Ÿš€ Setup & Installation

Prerequisites

  • Python 3.10+
  • Webcam (built-in or external)
  • OS: Windows / macOS / Linux

Step 1 โ€” Clone the Repository

git clone https://github.com/mohitr2111/3dCubeControl.git
cd 3dCubeControl

Step 2 โ€” Create Virtual Environment

# Create
python3 -m venv venv

# Activate (macOS/Linux)
source venv/bin/activate

# Activate (Windows)
venv\Scripts\activate

Step 3 โ€” Install Dependencies

pip install -r requirements.txt

Step 4 โ€” Run

python3 main.py

On first run, the MediaPipe hand landmarker model (~7.5 MB) downloads automatically.

[INFO] Starting 3D Cube Control... Press 'q' to quit.
Downloading hand landmark model...
Model downloaded!
Webcam opened successfully!
Hand tracker ready!

๐Ÿ“ฆ Dependencies

opencv-python       # Webcam capture + image processing
mediapipe           # Hand landmark detection (Tasks API)
numpy               # Math operations for 3D transformations
pygame              # Window + event management
PyOpenGL            # 3D graphics rendering
PyOpenGL_accelerate # OpenGL speed boost (optional)

๐Ÿ› ๏ธ Customization

Change Rotation / Zoom Speed

# In Config class
ROTATION_SENSITIVITY = 0.3    # Increase for faster rotation
ZOOM_SENSITIVITY = 0.015      # Increase for faster zoom
smoothing_factor_size = 0.4   # Lower = more snappy, higher = smoother

Change Cube Colors

# In draw_cube() โ€” faces list
# Format: (normal, [R, G, B], vertices)
# R, G, B values are 0.0 to 1.0
([0, 0, 1],  [1.0, 0.08, 0.08], [...]),  # Front โ€” change color here
([0, 0, -1], [0.0, 1.0,  0.3 ], [...]),  # Back

Change Detection Confidence

# In HandTracker.__init__()
options = vision.HandLandmarkerOptions(
    min_hand_detection_confidence=0.5,  # 0.0 to 1.0
    min_hand_presence_confidence=0.5,
    min_tracking_confidence=0.5,
)

๐Ÿ›Ÿ Troubleshooting

Camera Not Opening

  • Ensure webcam is not used by another app
  • macOS: System Preferences โ†’ Security & Privacy โ†’ Camera โ†’ enable Terminal/Python
  • Windows: Settings โ†’ Privacy โ†’ Camera โ†’ enable

Hand Not Detected

  • Ensure good lighting โ€” avoid very dark rooms
  • Keep hand clearly visible, centered in frame
  • Try reducing min_hand_detection_confidence to 0.3

Low FPS / Lag

  • Close other applications
  • Reduce WEBCAM_WIDTH/HEIGHT in Config
  • Ensure GPU drivers are updated for OpenGL

OpenGL Errors

  • Linux: sudo apt-get install python3-opengl
  • Windows: Update GPU drivers

Module Not Found

  • Ensure virtual environment is activated
  • Re-run: pip install -r requirements.txt

๐Ÿ“Š Performance

Metric Value
Typical FPS 30โ€“60 FPS
Hand Landmarks 21 points per frame
Smoothing Window 5 frames moving average
Webcam Resolution 640 ร— 480
3D Window 800 ร— 600
Model Size ~7.5 MB

๐Ÿ”ฎ Future Enhancements

  • Two-hand support โ€” separate control per hand
  • Multiple 3D shapes (sphere, pyramid, custom .obj models)
  • Gesture recording and playback
  • Color theme switcher via keyboard
  • VR/AR mode integration
  • Multi-object scene with hand collision

๐Ÿ“œ License

This project is licensed under the MIT License โ€” see the LICENSE file for details.


๐Ÿ™ Acknowledgements


Control reality with your hands. ๐Ÿ–๏ธโœจ

Made with โค๏ธ by mohitr2111

About

Manipulate a 3D cube using nothing but your hand.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages