[CF]构建项目基础目录

This commit is contained in:
edzhao 2025-06-04 10:15:00 +08:00
parent 7eee69dc41
commit f32a53508b
15 changed files with 310 additions and 1 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
venv

8
.idea/.gitignore generated vendored Normal file
View File

@ -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

9
.idea/le-yolo.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/le-yolo.iml" filepath="$PROJECT_DIR$/.idea/le-yolo.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

78
README.md Normal file
View File

@ -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 # 项目说明
```

View File

@ -1 +0,0 @@
1111

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
ultralytics
opencv-python
torch
torchvision
Pillow

BIN
res/test.mp4 Normal file

Binary file not shown.

62
run_app.bat Normal file
View File

@ -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

50
run_app.sh Executable file
View File

@ -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/ 目录中"

79
setup_and_run.py Normal file
View File

@ -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()

3
src/app.py Normal file
View File

@ -0,0 +1,3 @@
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
results = model.predict("../res/test.mp4", show=True, save=True)

Binary file not shown.

BIN
src/yolov8n.pt Normal file

Binary file not shown.