logo
0
0
WeChat Login
Chief<520@zut.edu.cn>
添加项目README文件,介绍摄像头贪吃蛇游戏的功能、安装和使用说明

Camera Snake Game (MediaPipe + OpenCV + Python)

一个使用 MediaPipe + OpenCV + Python 制作的摄像头贪吃蛇小游戏。通过手指在摄像头前的移动来控制贪吃蛇的头部,摄像头采集到的画面直接作为游戏画布,带来沉浸式的交互体验。

特性 Features

  • 使用摄像头画面作为游戏背景/画布(Camera feed as game canvas)
  • 基于手部关键点的手指控制(Finger control via hand landmarks)
  • 轻量、纯本地运行(Lightweight, runs locally)
  • 可作为计算机视觉与人机交互课程/项目示例(Great for CV/HCI demos)

环境要求 Requirements

  • Python 3.8+(推荐 3.9 或更高)
  • 操作系统:Windows(已验证),理论上支持 macOS/Linux
  • 依赖库 Dependencies:
    • opencv-python
    • mediapipe
    • numpy
  • 可用摄像头(Webcam)

安装 Installation

  1. 克隆或下载项目源码(Clone/download the repo)
  2. 建议使用虚拟环境(Recommended to use a virtual environment)
    • 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)
    • 如果存在 requirements.txt:
      • pip install -r requirements.txt
    • 或手动安装(Or install manually):
      • pip install opencv-python mediapipe numpy

运行 Run

  • 确保摄像头可用并已允许权限(Ensure camera access is allowed)
  • 在项目根目录执行(Run in project root):
    • python main.py
  • 第一次运行可能需要较好光照与简单背景以提升手部识别稳定性(Good lighting and a clean background help recognition)

控制方式 Controls

  • 将食指(或指定的手指)对准摄像头并移动,即可控制蛇头移动方向(Move your index finger to control the snake head)
  • 保持手部在画面内且清晰可见(Keep your hand in the frame and clearly visible)
  • 根据实现,可能包含:速度随移动幅度、碰撞检测、得分规则等(Depending on implementation: speed vs. motion amplitude, collision, scoring, etc.)

推荐设置 Tips

  • 光线充足且背景尽量简洁(Good lighting and simple background)
  • 避免强反光或遮挡(Avoid glare and occlusions)
  • 如识别抖动,可在代码中增加平滑/滤波处理(If jitter occurs, add smoothing/filtering in code)

项目结构(示例) Project Structure (example)

  • main.py:入口脚本,摄像头采集、MediaPipe 检测、游戏循环(entry, camera capture, landmarks, game loop)
  • game/:游戏逻辑(蛇身体、碰撞、得分)(game logic: snake, collision, scoring)
  • utils/:工具方法(坐标转换、平滑)(utilities: coordinate mapping, smoothing)
  • assets/:素材资源(可选)(assets, optional)

具体文件可能与示例不同,请以实际代码为准。

常见问题 FAQ

  • 摄像头打不开(Camera fails to open)
    • 检查设备是否被占用;确认索引 cv2.VideoCapture(0) 是否正确(尝试 1、2)
    • 在系统隐私设置里允许应用访问摄像头
  • 手部识别不稳定(Unstable hand detection)
    • 提升光照、减少背景干扰,手靠近镜头;适当调整 MediaPipe 参数
  • 运行报错缺少依赖(Missing dependency errors)
    • 使用 pip install opencv-python mediapipe numpy 安装依赖
  • 帧率过低(Low FPS)
    • 降低图像分辨率;优化绘制与处理逻辑;关闭不必要的可视化

安全与隐私 Privacy & Safety

  • 图像只在本地处理,无外部上传(Images are processed locally; nothing is uploaded)
  • 请在合规场景使用并告知被拍摄者(Use responsibly and inform subjects when applicable)

许可 License

  • 未设置明确许可证(License not specified)。如需开源分发,建议添加常见许可证(MIT/Apache-2.0 等)。

贡献 Contributing

  • 欢迎提交 Issue/PR 来改进手势识别稳定性、游戏机制、UI 效果等(Issues/PRs are welcome for stabilization, game mechanics, UI improvements)

致谢 Acknowledgements