Skip to content

Ultralytics HUB推理API

在您训练模型后,您可以免费使用共享推理API。如果您是Pro用户,您可以访问专用推理APIUltralytics HUB推理API允许您通过我们的REST API运行推理,而无需在本地安装和设置Ultralytics YOLO环境。

Ultralytics HUB在模型页面内部署标签的截图,箭头指向专用推理API卡片和共享推理API卡片


观看: Ultralytics HUB推理API演示

专用推理API

为了回应高需求和广泛兴趣,我们很高兴推出Ultralytics HUB专用推理API,为我们的Pro用户提供一键部署的专用环境!

Note

我们很高兴在公开测试期间免费提供此功能作为Pro计划的一部分,未来可能会有付费层级。

  • 全球覆盖: 在全球38个地区部署,确保从任何地点都能获得低延迟访问。查看完整的Google Cloud地区列表
  • Google Cloud Run支持: 由Google Cloud Run支持,提供无限可扩展和高可靠性的基础设施。
  • 高速: 根据Ultralytics测试,在附近地区进行YOLOv8n推理时,640分辨率的延迟可低于100毫秒。
  • 增强安全性: 提供强大的安全功能,保护您的数据并确保符合行业标准。了解更多关于Google Cloud安全性的信息

要使用Ultralytics HUB专用推理API,请点击启动端点按钮。接下来,使用下面指南中描述的唯一端点URL。

Ultralytics HUB在模型页面内部署标签的截图,箭头指向专用推理API卡片中的启动端点按钮

Tip

选择延迟最低的地区以获得最佳性能,如文档中所述。

要关闭专用端点,请点击停止端点按钮。

Ultralytics HUB在模型页面内部署标签的截图,箭头指向专用推理API卡片中的停止端点按钮

共享推理API

要使用Ultralytics HUB共享推理API,请按照以下指南操作。

免费用户有以下使用限制:

  • 每小时100次调用
  • 每月1000次调用

Pro用户有以下使用限制:

  • 每小时1000次调用
  • 每月10000次调用

Python

要使用Python访问Ultralytics HUB推理API,请使用以下代码:

import requests

# API URL
url = "https://predict.ultralytics.com"

# Headers, 使用实际的API_KEY
headers = {"x-api-key": "API_KEY"}

# 推理参数 (使用实际的MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# 加载图像并发送请求
with open("path/to/image.jpg", "rb") as image_file:
    files = {"file": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())

Note

MODEL_ID替换为所需的模型ID,API_KEY替换为您的实际API密钥,并将path/to/image.jpg替换为您要运行推理的图像路径。

如果您使用的是我们的专用推理API,请同时替换url

cURL

要使用cURL访问Ultralytics HUB推理API,请使用以下代码:

curl -X POST "https://predict.ultralytics.com" \
  -H "x-api-key: API_KEY" \
  -F "model=https://hub.ultralytics.com/models/MODEL_ID" \
-F "file=@/path/to/image.jpg" \
-F "imgsz=640" \
-F "conf=0.25" \
-F "iou=0.45"

Note

MODEL_ID 替换为所需的模型 ID,API_KEY 替换为您的实际 API 密钥,并将 path/to/image.jpg 替换为您要运行推理的图像路径。

如果您使用的是我们的 专用推理 API,请同时替换 url

参数

请参阅下表以获取完整的可用推理参数列表。

参数 默认值 类型 描述
file file 用于推理的图像或视频文件。
imgsz 640 int 输入图像的大小,有效范围为 32 - 1280 像素。
conf 0.25 float 预测的置信度阈值,有效范围 0.01 - 1.0
iou 0.45 float 交并比 (IoU) 阈值,有效范围 0.0 - 0.95

响应

Ultralytics HUB 推理 API 返回一个 JSON 响应。

分类

分类模型

from ultralytics import YOLO

# 加载模型
model = YOLO("yolov8n-cls.pt")

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

# 以 JSON 格式打印 image.jpg 的结果
print(results[0].to_json())
curl -X POST "https://predict.ultralytics.com" \
    -H "x-api-key: API_KEY" \
    -F "model=https://hub.ultralytics.com/models/MODEL_ID" \
    -F "file=@/path/to/image.jpg" \
    -F "imgsz=640" \
    -F "conf=0.25" \
    -F "iou=0.45"
import requests

# API URL
url = "https://predict.ultralytics.com"

# 请求头,使用实际的 API_KEY
headers = {"x-api-key": "API_KEY"}

# 推理参数(使用实际的 MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# 加载图像并发送请求
with open("path/to/image.jpg", "rb") as image_file:
    files = {"file": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  "images": [
    {
      "results": [
        {
          "class": 0,
          "name": "person",
          "confidence": 0.92
        }
      ],
      "shape": [
        750,
        600
      ],
      "speed": {
        "inference": 200.8,
        "postprocess": 0.8,
        "preprocess": 2.8
      }
    }
  ],
  "metadata": ...
}

检测

检测模型

from ultralytics import YOLO

# 加载模型
model = YOLO("yolov8n.pt")

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

# 以 JSON 格式打印 image.jpg 的结果
print(results[0].to_json())
curl -X POST "https://predict.ultralytics.com" \
    -H "x-api-key: API_KEY" \
    -F "model=https://hub.ultralytics.com/models/MODEL_ID" \
    -F "file=@/path/to/image.jpg" \
    -F "imgsz=640" \
    -F "conf=0.25" \
    -F "iou=0.45"
import requests

# API URL
url = "https://predict.ultralytics.com"

# 请求头,使用实际的 API_KEY
headers = {"x-api-key": "API_KEY"}

# 推理参数(使用实际的 MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# 加载图像并发送请求
with open("path/to/image.jpg", "rb") as image_file:
    files = {"file": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())

```json { "images": [ { "results": [ { "class": 0, "name": "person", "confidence": 0.92,

```json { "images": [ { "results": [ { "class": 0, "name": "person", "confidence": 0.92, "box": { "x1": 118, "x2": 416, "y1": 112, "y2": 660 } } ], "shape": [ 750, 600 ], "speed": { "inference": 200.8, "postprocess": 0.8, "preprocess": 2.8 } } ], "metadata": ... }

### OBB

!!! example "OBB 模型"

    === "`ultralytics`"

        ```python
        from ultralytics import YOLO

        # 加载模型
        model = YOLO("yolov8n-obb.pt")

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

        # 以 JSON 格式打印 image.jpg 的结果
        print(results[0].tojson())
        ```

    === "cURL"

        ```bash
        curl -X POST "https://predict.ultralytics.com" \
            -H "x-api-key: API_KEY" \
            -F "model=https://hub.ultralytics.com/models/MODEL_ID" \
            -F "file=@/path/to/image.jpg" \
            -F "imgsz=640" \
            -F "conf=0.25" \
            -F "iou=0.45"
        ```

    === "Python"

        ```python
        import requests

        # API URL
        url = "https://predict.ultralytics.com"

        # 请求头,使用实际的 API_KEY
        headers = {"x-api-key": "API_KEY"}

        # 推理参数(使用实际的 MODEL_ID)
        data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

        # 加载图像并发送请求
        with open("path/to/image.jpg", "rb") as image_file:
            files = {"file": image_file}
            response = requests.post(url, headers=headers, files=files, data=data)

        print(response.json())
        ```

    === "响应"

        ```json
        {
          "images": [
            {
              "results": [
                {
                  "class": 0,
                  "name": "person",
                  "confidence": 0.92,
                  "box": {
                    "x1": 374.85565,
                    "x2": 392.31824,
                    "x3": 412.81805,
                    "x4": 395.35547,
                    "y1": 264.40704,
                    "y2": 267.45728,
                    "y3": 150.0966,
                    "y4": 147.04634
                  }
                }
              ],
              "shape": [
                750,
                600
              ],
              "speed": {
                "inference": 200.8,
                "postprocess": 0.8,
                "preprocess": 2.8
              }
            }
          ],
          "metadata": ...
        }
        ```

### 分割

!!! example "分割模型"

    === "`ultralytics`"

        ```python
        from ultralytics import YOLO

        # 加载模型
        model = YOLO("yolov8n-seg.pt")

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

        # 以 JSON 格式打印 image.jpg 的结果
        print(results[0].tojson())
        ```

    === "cURL"

        ```bash
        curl -X POST "https://predict.ultralytics.com" \
            -H "x-api-key: API_KEY" \
            -F "model=https://hub.ultralytics.com/models/MODEL_ID" \
            -F "file=@/path/to/image.jpg" \
            -F "imgsz=640" \
            -F "conf=0.25" \
            -F "iou=0.45"
        ```

    === "Python"

        ```python
        import requests

        # API URL
        url = "https://predict.ultralytics.com"

        # 请求头,使用实际的 API_KEY
        headers = {"x-api-key": "API_KEY"}

        # 推理参数(使用实际的 MODEL_ID)
        data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

        # 加载图像并发送请求
        with open("path/to/image.jpg", "rb") as image_file:
            files = {"file": image_file}
            response = requests.post(url, headers=headers, files=files, data=data)

        print(response.json())
        ```

    === "响应"

        ```json
        {
          "images": [
            {
              "results": [
                {
                  "class": 0,
                  "name": "person",
                  "confidence": 0.92,
                  "box": {
                    "x1": 118,
                    "x2": 416,
                    "y1": 112,
                    "y2": 660
                  },
                  "segments": {
                    "x": [
                      266.015625,
                      266.015625,
                      258.984375,
                      ...
                    ],
                    "y": [
                      110.15625,
                      113.67188262939453,
                      120.70311737060547,
                      ...
                    ]
                  }
                }
              ],
              "shape": [
                750,
                600
              ],
              "speed": {
                "inference": 200.8,
                "postprocess": 0.8,
                "preprocess": 2.8
              }
            }
          ],
          "metadata": ...
        }
        ```
```json
{
  "images": [
    {
      "results": [
        {
          "class": 0,
          "name": "person",
          "confidence": 0.92,
          "box": {
            "x1": 118,
            "x2": 416,
            "y1": 112,
            "y2": 660
          },
          "keypoints": {
            "visible": [
              0.9909399747848511,
              0.8162999749183655,
              0.9872099757194519,
              ...
            ],
            "x": [
              316.3871765136719,
              315.9374694824219,
              304.878173828125,
              ...
            ],
            "y": [
              156.4207763671875,
              148.05775451660156,
              144.93240356445312,
              ...
            ]
          }
        }
      ],
      "shape": [
        750,
        600
      ],
      "speed": {
        "inference": 200.8,
        "postprocess": 0.8,
        "preprocess": 2.8
      }
    }
  ],
  "metadata": ...
}

姿态

姿态模型

from ultralytics import YOLO

# 加载模型
model = YOLO("yolov8n-pose.pt")

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

# 以JSON格式打印image.jpg的结果
print(results[0].tojson())
curl -X POST "https://predict.ultralytics.com" \
    -H "x-api-key: API_KEY" \
    -F "model=https://hub.ultralytics.com/models/MODEL_ID" \
    -F "file=@/path/to/image.jpg" \
    -F "imgsz=640" \
    -F "conf=0.25" \
    -F "iou=0.45"
import requests

# API URL
url = "https://predict.ultralytics.com"

# 请求头,使用实际的API_KEY
headers = {"x-api-key": "API_KEY"}

# 推理参数(使用实际的MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# 加载图像并发送请求
with open("path/to/image.jpg", "rb") as image_file:
    files = {"file": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  "images": [
    {
      "results": [
        {
          "class": 0,
          "name": "person",
          "confidence": 0.92,
          "box": {
            "x1": 118,
            "x2": 416,
            "y1": 112,
            "y2": 660
          },
          "keypoints": {
            "visible": [
              0.9909399747848511,
              0.8162999749183655,
              0.9872099757194519,
              ...
            ],
            "x": [
              316.3871765136719,
              315.9374694824219,
              304.878173828125,
              ...
            ],
            "y": [
              156.4207763671875,
              148.05775451660156,
              144.93240356445312,
              ...
            ]
          }
        }
      ],
      "shape": [
        750,
        600
      ],
      "speed": {
        "inference": 200.8,
        "postprocess": 0.8,
        "preprocess": 2.8
      }
    }
  ],
  "metadata": ...
}

📅 Created 8 months ago ✏️ Updated 20 days ago

Comments