logo
0
0
WeChat Login
Chief<520@zut.edu.cn>
添加手势识别模块的兼容性支持,当MediaPipe不可用时自动回退到OpenCV方案

Camera Snake Game (MediaPipe + OpenCV + Python)

A small snake game built with MediaPipe + OpenCV + Python. Control the snake head using your finger in front of the camera. The live camera feed serves as the game canvas for an immersive CV/HCI experience.


Features

  • Camera feed as the game background/canvas
  • Finger control via hand landmarks (MediaPipe Hands)
  • Lightweight, runs fully offline
  • Suitable for computer vision & HCI demos/assignments

Requirements

  • Python 3.8+ (recommended 3.9 or newer)
  • OS: Windows (verified). macOS/Linux should also work in principle
  • Dependencies:
    • opencv-python
    • mediapipe
    • numpy
  • A working webcam

Installation

  1. Clone or download this repository
  2. Create and activate a virtual environment (recommended)
    • Windows PowerShell:
      • python -m venv .venv
      • ./.venv/Scripts/Activate.ps1
    • macOS/Linux (bash/zsh):
      • python3 -m venv .venv
      • source .venv/bin/activate
  3. Install dependencies
    • If requirements.txt exists:
      • pip install -r requirements.txt
    • Or install manually:
      • pip install opencv-python mediapipe numpy

Run

  • Ensure your webcam is accessible and permission is granted
  • From the project root, run:
    • python main.py
  • For best results on first run, use good lighting and a simple background

Controls

  • Move your index finger (or the designated finger) in front of the camera to steer the snake head
  • Keep your hand clearly visible within the frame
  • Depending on implementation, mechanics may include: speed vs. motion amplitude, collision detection, scoring rules, etc.

Tips

  • Prefer bright, uniform lighting and a simple background
  • Avoid strong reflections and occlusions
  • If detection jitters, consider adding smoothing/filtering in code

Project Structure (example)

  • main.py: Entry point. Camera capture, MediaPipe detection, game loop
  • game/: Game logic (snake body, collision, scoring)
  • utils/: Utilities (coordinate mapping, smoothing)
  • assets/: Optional resources

Actual files may differ; please refer to the code.

FAQ

  • Webcam cannot open
    • Check if another app is using the camera; verify capture index cv2.VideoCapture(0) (try 1, 2)
    • Allow camera access in system privacy settings
  • Unstable hand detection
    • Improve lighting, simplify background, move hand closer; adjust MediaPipe parameters if needed
  • Missing dependency errors
    • Install with pip install opencv-python mediapipe numpy
  • Low FPS
    • Lower the frame resolution; optimize drawing/processing; disable unnecessary visualizations

Privacy & Safety

  • Image processing is local only; nothing is uploaded externally
  • Use responsibly and inform subjects when applicable

License

  • No explicit license set. If you plan to open-source, consider adding a common license (MIT/Apache-2.0, etc.)

Contributing

  • Issues/PRs are welcome to improve gesture stability, game mechanics, UI/UX, and performance

Acknowledgements

About

CV摄像头贪吃蛇游戏

Language
Python100%