> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/ji-suan-ji-shi-jue/yolov8-detection.md).

# YOLOv8 检测

在 Clore.ai 上使用 YOLOv8 和 YOLOv11 进行实时目标检测

使用 Ultralytics YOLOv8 和 YOLOv11 运行实时目标检测。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行 [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

{% hint style="info" %}
**更新：YOLOv11（2025）——快 22%**

YOLOv11 现已可通过同样的 `ultralytics` 包获取。它提供 **快 22% 的推理速度** 并且比 YOLOv8 具有更高的 mAP，同时保持相同的简洁 API。新功能包括旋转边界框（OBB）检测。通过运行以下命令升级： `pip install -U ultralytics`.
{% endhint %}

## 在 CLORE.AI 上租用

1. 访问 [CLORE.AI 市场](https://clore.ai/marketplace)
2. 按 GPU 类型、显存和价格筛选
3. 选择 **按需** （固定费率）或 **竞价** （出价）
4. 配置你的订单：
   * 选择 Docker 镜像
   * 设置端口（SSH 用 TCP，Web 界面用 HTTP）
   * 如有需要，添加环境变量
   * 输入启动命令
5. 选择支付方式： **CLORE**, **BTC**，或 **USDT/USDC**
6. 创建订单并等待部署

### 访问你的服务器

* 在 **我的订单**
* 中查找连接详情
* Web 界面：使用 HTTP 端口 URL `ssh -p <port> root@<proxy-address>`

## 什么是 YOLOv8？

YOLOv8 是一个高性能 YOLO 模型，提供：

* 目标检测
* 实例分割
* 姿态估计
* 图像分类
* 目标跟踪

## 什么是 YOLOv11？

YOLOv11（2025）是最新一代，新增：

* **快 22% 的推理速度** 相较于 YOLOv8
* 在所有模型尺寸上都有更高的 mAP
* **旋转边界框（OBB）** 检测——新任务
* 改进的架构（C3k2 模块、SPPF、C2PSA）
* 相同的 `ultralytics` 包，可直接替换

### 支持的任务（YOLOv11）

| 任务         | 后缀      | 描述                 |
| ---------- | ------- | ------------------ |
| `detect`   | *（无）*   | 带边界框的目标检测          |
| `segment`  | `-seg`  | 带掩码的实例分割           |
| `classify` | `-cls`  | 图像分类               |
| `pose`     | `-pose` | 人体姿态估计             |
| `obb`      | `-obb`  | **新增** 旋转边界框（旋转检测） |

## 模型大小

### YOLOv8 模型

| 模型      | 大小     | mAP  | 速度（RTX 3090） |
| ------- | ------ | ---- | ------------ |
| YOLOv8n | 320 万  | 37.3 | 约 1 毫秒       |
| YOLOv8s | 1120 万 | 44.9 | 约 2 毫秒       |
| YOLOv8m | 2590 万 | 50.2 | 约 4 毫秒       |
| YOLOv8l | 4370 万 | 52.9 | 约 6 毫秒       |
| YOLOv8x | 6820 万 | 53.9 | 约 8 毫秒       |

### YOLOv11 模型

| 模型      | 大小     | mAP  | 速度（RTX 3090） |
| ------- | ------ | ---- | ------------ |
| yolo11n | 260 万  | 39.5 | 约 0.8 毫秒     |
| yolo11s | 940 万  | 47.0 | 约 1.5 毫秒     |
| yolo11m | 2010 万 | 51.5 | 约 3.2 毫秒     |
| yolo11l | 2530 万 | 53.4 | 约 4.7 毫秒     |
| yolo11x | 5690 万 | 54.7 | 约 6.5 毫秒     |

### YOLOv8 与 YOLOv11 对比

| 指标                   | YOLOv8x | yolo11x  | 提升           |
| -------------------- | ------- | -------- | ------------ |
| 参数                   | 6820 万  | 5690 万   | **减少 17%**   |
| mAP50-95（COCO）       | 53.9    | 54.7     | **+0.8 mAP** |
| 推理（RTX 3090）         | 约 8 毫秒  | 约 6.5 毫秒 | **快 22%**    |
| FPS（RTX 3090，640 像素） | \~150   | \~183    | **快 22%**    |
| OBB 任务               | ❌       | ✅        | **v11 新增**   |

## 快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-runtime
```

**端口：**

```
22/tcp
7860/http
```

**命令（YOLOv11）：**

```bash
pip install ultralytics gradio && \
python -c "
import gradio as gr
from ultralytics import YOLO
from PIL import Image

model = YOLO('yolo11m.pt')

def detect(image):
    results = model(image)
    return Image.fromarray(results[0].plot())

demo = gr.Interface(fn=detect, inputs=gr.Image(type='pil'), outputs=gr.Image(), title='YOLOv11 Detection')
demo.launch(server_name='0.0.0.0', server_port=7860)
"
```

## 访问你的服务

部署完成后，找到你的 `http_pub` URL 于 **我的订单**:

1. 前往 **我的订单** 页面
2. 点击你的订单
3. 找到 `http_pub` URL（例如， `abc123.clorecloud.net`)

使用 `https://YOUR_HTTP_PUB_URL` 替代 `localhost` 用于下面的示例中。

## 安装

```bash
pip install ultralytics
```

YOLOv8 和 YOLOv11 使用相同的包。升级即可获取 YOLOv11：

```bash
pip install -U ultralytics
```

## YOLOv11 目标检测

### 使用 yolo11m 的基础检测

```python
from ultralytics import YOLO
from PIL import Image

# 加载 YOLOv11 中等模型
model = YOLO('yolo11m.pt')

# 运行推理
results = model('image.jpg')

# 显示结果
results[0].show()

# 保存结果
results[0].save('output.jpg')
```

### 获取检测结果

```python
results = model('image.jpg')

for result in results:
    boxes = result.boxes
    for box in boxes:
        # 坐标
        x1, y1, x2, y2 = box.xyxy[0].tolist()

        # 置信度
        conf = box.conf[0].item()

        # 类别
        cls = int(box.cls[0].item())
        name = model.names[cls]

        print(f"{name}: {conf:.2f} 位于 ({x1:.0f}, {y1:.0f}, {x2:.0f}, {y2:.0f})")
```

### 批量处理

```python
from ultralytics import YOLO
import os

model = YOLO('yolo11m.pt')

input_dir = './images'
output_dir = './detected'
os.makedirs(output_dir, exist_ok=True)

# 处理所有图像
results = model(input_dir, save=True, project=output_dir)
```

## YOLOv11 任务

### 实例分割

```python
from ultralytics import YOLO

# 加载 YOLOv11 分割模型
model = YOLO('yolo11m-seg.pt')

results = model('image.jpg')

for result in results:
    masks = result.masks  # 分割掩码
    if masks is not None:
        for mask in masks.data:
            # mask 是一个二值张量
            pass
```

### 姿态估计

```python
from ultralytics import YOLO

# 加载 YOLOv11 姿态模型
model = YOLO('yolo11m-pose.pt')

results = model('image.jpg')

for result in results:
    keypoints = result.keypoints
    if keypoints is not None:
        for kp in keypoints.data:
            # 17 个关键点：鼻子、眼睛、耳朵、肩膀、肘部、手腕、髋部、膝盖、脚踝
            pass
```

### 分类

```python
from ultralytics import YOLO

# 加载 YOLOv11 分类模型
model = YOLO('yolo11m-cls.pt')

results = model('image.jpg')
for result in results:
    # Top-1 类别和置信度
    top1 = result.probs.top1
    top1conf = result.probs.top1conf.item()
    print(f"类别：{result.names[top1]} ({top1conf:.2f})")
```

### 旋转边界框（OBB）——YOLOv11 新增

OBB 可检测任意旋转角度的目标——非常适合航空/卫星图像、文档扫描和文本检测。

```python
from ultralytics import YOLO

# 加载 YOLOv11 OBB 模型
model = YOLO('yolo11m-obb.pt')

results = model('aerial_image.jpg')

for result in results:
    obb = result.obb
    if obb is not None:
        for box in obb:
            # 旋转框：x_center, y_center, width, height, angle
            xywhr = box.xywhr[0].tolist()
            conf = box.conf[0].item()
            cls = int(box.cls[0].item())
            print(f"{result.names[cls]}: {conf:.2f} 旋转框：{xywhr}")
```

## 视频处理

### 处理视频

```python
from ultralytics import YOLO

model = YOLO('yolo11m.pt')

# 处理视频
results = model('video.mp4', save=True)
```

### 实时摄像头

```python
from ultralytics import YOLO
import cv2

model = YOLO('yolo11n.pt')  # 实时使用 nano 模型

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    results = model(frame)
    annotated = results[0].plot()

    cv2.imshow('YOLOv11', annotated)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
```

### 保存处理后的视频

```python
from ultralytics import YOLO
import cv2

model = YOLO('yolo11m.pt')

cap = cv2.VideoCapture('input.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    results = model(frame)
    annotated = results[0].plot()
    out.write(annotated)

cap.release()
out.release()
```

## 目标跟踪

```python
from ultralytics import YOLO

model = YOLO('yolo11m.pt')

# 在视频中跟踪目标
results = model.track('video.mp4', save=True, tracker='bytetrack.yaml')

# 获取跟踪 ID
for result in results:
    boxes = result.boxes
    if boxes.id is not None:
        track_ids = boxes.id.tolist()
```

## 自定义训练

### 准备数据集

```yaml

# dataset.yaml
path: /workspace/dataset
train: images/train
val: images/val

names:
  0: 猫
  1: 狗
  2: 鸟
```

### 训练 YOLOv11

```python
from ultralytics import YOLO

# 加载预训练 YOLOv11 模型
model = YOLO('yolo11n.pt')

# 训练
results = model.train(
    data='dataset.yaml',
    epochs=100,
    imgsz=640,
    batch=16,
    device=0
)
```

### 训练参数

```python
model.train(
    data='dataset.yaml',
    epochs=100,
    imgsz=640,
    batch=16,
    device=0,
    workers=8,
    patience=50,         # 早停
    save=True,
    save_period=10,      # 每 N 个 epoch 保存一次
    cache=True,          # 缓存图像
    amp=True,            # 混合精度
    lr0=0.01,
    lrf=0.01,
    momentum=0.937,
    weight_decay=0.0005,
    warmup_epochs=3.0,
    box=7.5,
    cls=0.5,
    dfl=1.5,
    augment=True,
    hsv_h=0.015,
    hsv_s=0.7,
    hsv_v=0.4,
    flipud=0.0,
    fliplr=0.5,
    mosaic=1.0,
    mixup=0.0,
)
```

## 导出模型

```python
from ultralytics import YOLO

model = YOLO('yolo11m.pt')

# 导出为不同格式
model.export(format='onnx')           # ONNX
model.export(format='tensorrt')       # TensorRT
model.export(format='openvino')       # OpenVINO
model.export(format='coreml')         # CoreML
model.export(format='tflite')         # TensorFlow Lite
```

## API 服务器

```python
from fastapi import FastAPI, UploadFile
from fastapi.responses import JSONResponse
from ultralytics import YOLO
from PIL import Image
import io

app = FastAPI()
model = YOLO('yolo11m.pt')

@app.post("/detect")
async def detect(file: UploadFile):
    contents = await file.read()
    image = Image.open(io.BytesIO(contents))

    results = model(image)

    detections = []
    for box in results[0].boxes:
        detections.append({
            "class": model.names[int(box.cls[0])],
            "confidence": float(box.conf[0]),
            "bbox": box.xyxy[0].tolist()
        })

    return JSONResponse(content={"detections": detections})

@app.post("/segment")
async def segment(file: UploadFile):
    contents = await file.read()
    image = Image.open(io.BytesIO(contents))

    model_seg = YOLO('yolo11m-seg.pt')
    results = model_seg(image)

    # 返回带注释的图像
    annotated = results[0].plot()
    # ... 转换并返回

# 运行：uvicorn server:app --host 0.0.0.0 --port 8000
```

## 性能优化

### TensorRT 导出

```python
model = YOLO('yolo11m.pt')
model.export(format='tensorrt', half=True)

# 使用导出的模型
model_trt = YOLO('yolo11m.engine')
results = model_trt('image.jpg')
```

### 批量推理

```python

# 一次处理多张图像
images = ['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg']
results = model(images, batch=4)
```

## 性能基准

### YOLOv11 FPS（640px 输入）

| 模型      | GPU      | FPS    |
| ------- | -------- | ------ |
| yolo11n | RTX 3090 | \~1100 |
| yolo11s | RTX 3090 | \~730  |
| yolo11m | RTX 3090 | \~370  |
| yolo11x | RTX 3090 | \~183  |
| yolo11x | RTX 4090 | \~305  |

### YOLOv8 FPS（640px 输入）— 前一代

| 模型      | GPU      | FPS   |
| ------- | -------- | ----- |
| YOLOv8n | RTX 3090 | \~900 |
| YOLOv8s | RTX 3090 | \~600 |
| YOLOv8m | RTX 3090 | \~300 |
| YOLOv8x | RTX 3090 | \~150 |
| YOLOv8x | RTX 4090 | \~250 |

## 故障排查

### 内存不足

```python

# 使用更小的模型
model = YOLO('yolo11n.pt')

# 或减小图像尺寸
results = model('image.jpg', imgsz=320)
```

### 处理缓慢

* 使用 TensorRT 导出
* 使用更小的模型（yolo11n 或 yolo11s）
* 减小图像尺寸

### 准确率低

* 使用更大的模型（用 yolo11x 代替 yolo11n）
* 使用自定义数据训练
* 增大图像尺寸

## 成本估算

CLORE.AI 市场的典型价格（截至 2025 年）：

| GPU       | 按小时费率   | 按天费率    | 4 小时会话  |
| --------- | ------- | ------- | ------- |
| RTX 3060  | \~$0.03 | \~$0.70 | \~$0.12 |
| RTX 3090  | \~$0.06 | \~$1.50 | \~$0.25 |
| RTX 4090  | \~$0.10 | \~$2.30 | \~$0.40 |
| A100 40GB | \~$0.17 | \~$4.00 | \~$0.70 |
| A100 80GB | \~$0.25 | \~$6.00 | \~$1.00 |

*价格因提供商和需求而异。请查看* [*CLORE.AI 市场*](https://clore.ai/marketplace) *以获取当前价格。*

**节省开支：**

* 使用 **竞价** 可中断工作市场——大约三分之一的服务器将现货价格定在低于按需价格的位置（中位数约优惠 13%），其余则与之持平
* 使用 **CLORE** 代币
* 比较不同提供商的价格

## 下一步

* [Segment Anything](/guides/guides_v2-zh/tu-xiang-chu-li/segment-anything.md) - 高级分割
* Detectron2 - 更多检测选项
* [Real-ESRGAN](/guides/guides_v2-zh/tu-xiang-chu-li/real-esrgan-upscaling.md) - 增强检测到的对象


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/ji-suan-ji-shi-jue/yolov8-detection.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
