A tennis match visual recognition project based on YOLO that can detect tennis court lines, players, and tennis balls.
- Tennis Court Line Detection - Uses Hough line transform to detect court boundaries
- Player Detection - Uses YOLO to detect player positions
- Tennis Ball Detection - Uses YOLO + color detection to identify tennis ball positions
使用 venv(Python 3.3+):
# 创建虚拟环境
python3 -m venv venv
# 激活虚拟环境
# macOS/Linux:
source venv/bin/activate
# Windows:
# venv\Scripts\activate使用 conda(如果安装了Anaconda/Miniconda):
# 创建虚拟环境
conda create -n tennis-recognition python=3.10
# 激活虚拟环境
conda activate tennis-recognitionpip install -r requirements.txtTIP: It is recommended to use a virtual environment to avoid polluting the global Python environment. If you must install globally, simply run
pip install -r requirements.txt.
将你的视频或图像文件放入 data/videos/ 目录
处理视频:
python main.py data/videos/tennis_match.mp4处理图像:
python main.py data/videos/tennis_frame.jpg指定输出路径:
python main.py data/videos/tennis_match.mp4 output.mp4Tennis-Recognization/
├── requirements.txt # 依赖包
├── README.md # 项目说明
├── config.py # 配置文件
├── main.py # 主程序入口
├── venv/ # 虚拟环境(可选,不提交到git)
├── models/
│ ├── __init__.py
│ ├── court_detector.py # 边线检测模块
│ ├── player_detector.py # 球员检测模块
│ └── ball_detector.py # 网球检测模块
├── utils/
│ ├── __init__.py
│ └── visualization.py # 可视化工具
└── data/
├── videos/ # 输入视频/图像
└── output/ # 输出结果
在 config.py 中可以调整以下参数:
- YOLO模型: 可选择
yolov11n.pt(最快),yolov11s.pt,yolov11m.pt,yolov11l.pt,yolov11x.pt(最精确) - 置信度阈值:
CONFIDENCE_THRESHOLD(默认0.25) - 边线检测参数: Canny和Hough变换的参数
- YOLOv11 - 目标检测(Ultralytics)
- OpenCV - 图像处理和线检测
- PyTorch - 深度学习框架
- NumPy - 数值计算
from models.player_detector import PlayerDetector
from models.ball_detector import BallDetector
from models.court_detector import CourtDetector
import cv2
# 初始化检测器
player_detector = PlayerDetector()
ball_detector = BallDetector()
court_detector = CourtDetector()
# 读取图像
frame = cv2.imread("tennis_frame.jpg")
# 检测
players = player_detector.detect(frame)
balls = ball_detector.detect(frame)
lines = court_detector.detect_lines(frame)- 使用GPU: 如果有NVIDIA GPU,安装CUDA版本的PyTorch可以大幅提升速度
- 模型选择: 使用
yolov11n.pt可以获得最快的速度,yolov11x.pt可以获得最高的精度 - 视频分辨率: 对于实时处理,可以降低输入视频分辨率
退出虚拟环境:
deactivate删除虚拟环境(如果需要重新创建):
# venv方式
rm -rf venv
# conda方式
conda deactivate
conda env remove -n tennis-recognitionMIT License
Welcome to submit Issues and Pull Requests!