diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a2fb00d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +venv diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/le-yolo.iml b/.idea/le-yolo.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/le-yolo.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..969cc26 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c107c19 --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +# YOLO 项目 + +这是一个使用 YOLO (You Only Look Once) 模型进行视频目标检测的项目。 + +## 快速开始 + +### 方法1: 使用Bash脚本 (macOS/Linux推荐) + +```bash +./run_app.sh +``` + +### 方法2: 使用批处理脚本 (Windows推荐) + +```cmd +run_app.bat +``` + +### 方法3: 使用Python脚本 (跨平台) + +```bash +python3 setup_and_run.py +``` + +### 方法4: 手动设置 + +#### macOS/Linux: +```bash +# 1. 创建虚拟环境 +python3 -m venv venv + +# 2. 激活虚拟环境 +source venv/bin/activate + +# 3. 安装依赖 +pip install -r requirements.txt + +# 4. 运行应用 +cd src +python app.py +``` + +#### Windows: +```cmd +REM 1. 创建虚拟环境 +python -m venv venv + +REM 2. 激活虚拟环境 +venv\Scripts\activate.bat + +REM 3. 安装依赖 +pip install -r requirements.txt + +REM 4. 运行应用 +cd src +python app.py +``` + +## 注意事项 + +- 确保在 `res/` 目录中有名为 `test.mp4` 的视频文件 +- 首次运行时会自动下载 YOLOv8n 模型文件 +- 检测结果会保存在 `runs/detect/` 目录中 + +## 项目结构 + +``` +le-yolo/ +├── src/ +│ └── app.py # 主应用文件 +├── res/ +│ └── test.mp4 # 测试视频文件 +├── requirements.txt # Python依赖 +├── run_app.sh # Bash启动脚本 (macOS/Linux) +├── run_app.bat # 批处理启动脚本 (Windows) +├── setup_and_run.py # Python启动脚本 (跨平台) +└── README.md # 项目说明 +``` diff --git a/app.go.txt b/app.go.txt deleted file mode 100644 index d1d06ad..0000000 --- a/app.go.txt +++ /dev/null @@ -1 +0,0 @@ -1111 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a25e8fb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +ultralytics +opencv-python +torch +torchvision +Pillow \ No newline at end of file diff --git a/res/test.mp4 b/res/test.mp4 new file mode 100644 index 0000000..094f8d8 Binary files /dev/null and b/res/test.mp4 differ diff --git a/run_app.bat b/run_app.bat new file mode 100644 index 0000000..c4554a5 --- /dev/null +++ b/run_app.bat @@ -0,0 +1,62 @@ +@echo off +chcp 65001 >nul +:: 创建虚拟环境,并且运行app.py的脚本 (Windows版本) + +echo 开始设置YOLO项目环境... + +:: 检查Python是否安装 +python --version >nul 2>&1 +if %errorlevel% neq 0 ( + echo 错误: Python 未安装,请先安装Python + pause + exit /b 1 +) + +:: 创建虚拟环境 +echo 创建虚拟环境... +python -m venv venv +if %errorlevel% neq 0 ( + echo 创建虚拟环境失败 + pause + exit /b 1 +) + +:: 激活虚拟环境 +echo 激活虚拟环境... +call venv\Scripts\activate.bat + +:: 升级pip +echo 升级pip... +python -m pip install --upgrade pip + +:: 安装依赖 +echo 安装项目依赖... +pip install -r requirements.txt +if %errorlevel% neq 0 ( + echo 安装依赖失败 + pause + exit /b 1 +) + +:: 检查测试视频文件是否存在 +if not exist "res\test.mp4" ( + echo ❌ 错误: res\test.mp4 文件不存在! + echo. + echo 请按以下步骤操作: + echo 1. 在 res\ 目录中放置一个视频文件 + echo 2. 将视频文件重命名为 test.mp4 + echo 3. 重新运行此脚本 + echo. + echo 支持的视频格式: .mp4, .avi, .mov, .mkv 等 + echo 脚本已停止运行。 + pause + exit /b 1 +) + +:: 运行应用 +echo 运行应用... +cd src +python app.py + +echo ✅ 应用运行完成!检测结果已保存在 runs\detect\ 目录中 +pause \ No newline at end of file diff --git a/run_app.sh b/run_app.sh new file mode 100755 index 0000000..b3780c3 --- /dev/null +++ b/run_app.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +# 创建虚拟环境,并且运行app.py的脚本 + +echo "开始设置YOLO项目环境..." + +# 检查Python3是否安装 +if ! command -v python3 &> /dev/null; then + echo "错误: Python3 未安装,请先安装Python3" + exit 1 +fi + +# 创建虚拟环境 +echo "创建虚拟环境..." +python3 -m venv venv + +# 激活虚拟环境 +echo "激活虚拟环境..." +source venv/bin/activate + +# 升级pip +echo "升级pip..." +pip install --upgrade pip + +# 安装依赖 +echo "安装项目依赖..." +pip install -r requirements.txt + +# 检查测试视频文件是否存在 +if [ ! -f "res/test.mp4" ]; then + echo "❌ 错误: res/test.mp4 文件不存在!" + echo "" + echo "请按以下步骤操作:" + echo "1. 在 res/ 目录中放置一个视频文件" + echo "2. 将视频文件重命名为 test.mp4" + echo "3. 重新运行此脚本" + echo "" + echo "支持的视频格式: .mp4, .avi, .mov, .mkv 等" + echo "脚本已停止运行。" + exit 1 +fi + +echo "✅ 找到测试视频文件: res/test.mp4" + +# 运行应用 +echo "运行应用..." +cd src +python app.py + +echo "✅ 应用运行完成!检测结果已保存在 runs/detect/ 目录中" \ No newline at end of file diff --git a/setup_and_run.py b/setup_and_run.py new file mode 100644 index 0000000..8de0287 --- /dev/null +++ b/setup_and_run.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import sys +import subprocess +import shutil + +def run_command(command, shell=False): + """运行命令并返回结果""" + try: + result = subprocess.run(command, shell=shell, check=True, + capture_output=True, text=True) + return result.returncode, result.stdout, result.stderr + except subprocess.CalledProcessError as e: + return e.returncode, e.stdout, e.stderr + +def main(): + print("开始设置YOLO项目环境...") + + # 检查Python3是否安装 + if not shutil.which("python3"): + print("错误: Python3 未安装,请先安装Python3") + sys.exit(1) + + # 创建虚拟环境 + print("创建虚拟环境...") + returncode, stdout, stderr = run_command([sys.executable, "-m", "venv", "venv"]) + if returncode != 0: + print(f"创建虚拟环境失败: {stderr}") + sys.exit(1) + + # 确定虚拟环境中的Python解释器路径 + if os.name == 'nt': # Windows + venv_python = os.path.join("venv", "Scripts", "python.exe") + venv_pip = os.path.join("venv", "Scripts", "pip.exe") + else: # macOS/Linux + venv_python = os.path.join("venv", "bin", "python") + venv_pip = os.path.join("venv", "bin", "pip") + + # 升级pip + print("升级pip...") + returncode, stdout, stderr = run_command([venv_pip, "install", "--upgrade", "pip"]) + if returncode != 0: + print(f"升级pip失败: {stderr}") + + # 安装依赖 + print("安装项目依赖...") + returncode, stdout, stderr = run_command([venv_pip, "install", "-r", "requirements.txt"]) + if returncode != 0: + print(f"安装依赖失败: {stderr}") + sys.exit(1) + + # 检查测试视频文件是否存在 + if not os.path.exists("res/test.mp4"): + print("❌ 错误: res/test.mp4 文件不存在!") + print() + print("请按以下步骤操作:") + print("1. 在 res/ 目录中放置一个视频文件") + print("2. 将视频文件重命名为 test.mp4") + print("3. 重新运行此脚本") + print() + print("支持的视频格式: .mp4, .avi, .mov, .mkv 等") + print("脚本已停止运行。") + sys.exit(1) + + # 运行应用 + print("运行应用...") + os.chdir("src") + returncode, stdout, stderr = run_command([os.path.join("..", venv_python), "app.py"]) + + if returncode == 0: + print("✅ 应用运行完成!检测结果已保存在 runs/detect/ 目录中") + else: + print(f"应用运行失败: {stderr}") + sys.exit(1) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..16403ea --- /dev/null +++ b/src/app.py @@ -0,0 +1,3 @@ +from ultralytics import YOLO +model = YOLO("yolov8n.pt") +results = model.predict("../res/test.mp4", show=True, save=True) diff --git a/src/runs/detect/predict/test.mp4 b/src/runs/detect/predict/test.mp4 new file mode 100644 index 0000000..d35d3bd Binary files /dev/null and b/src/runs/detect/predict/test.mp4 differ diff --git a/src/yolov8n.pt b/src/yolov8n.pt new file mode 100644 index 0000000..0db4ca4 Binary files /dev/null and b/src/yolov8n.pt differ