跳转至内容

Ultralytics HUB 推理 API

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

Ultralytics HUB 截图,显示模型页面中的“部署”选项卡,箭头指向专用 Inference API 卡片和一个共享 Inference API 卡片


观看: Ultralytics HUB 推理 API 演练

专用推理 API

为了响应市场的强烈需求和广泛关注,我们非常激动地推出 Ultralytics HUB 专用推理 API,为我们的 Pro 用户提供一键式专用环境部署!

注意

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

  • 全球覆盖: 部署在全球 38 个区域,确保从任何位置进行低延迟访问。查看 Google Cloud 区域的完整列表
  • Google Cloud Run 支持: 由 Google Cloud Run 提供支持,提供无限可扩展且高度可靠的基础设施。
  • 高速度: 根据 Ultralytics 测试,对于来自附近区域的 640 分辨率的 YOLOv8n 推理,可能实现低于 100 毫秒的延迟。
  • 增强的安全性: 提供强大的安全功能来保护您的数据,并确保符合行业标准。详细了解 Google Cloud 安全性

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

Ultralytics HUB 截图,显示模型页面中的“部署”选项卡,箭头指向专用 Inference API 卡片中的“启动端点”按钮

提示

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

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

Ultralytics HUB 截图,显示模型页面中的“部署”选项卡,箭头指向专用 Inference API 卡片中的“停止端点”按钮

共享推理 API

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

Ultralytics HUB共享推理API具有以下使用限制:

  • 每小时100次呼叫

Python

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

import requests

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

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (use actual MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# Load image and send request
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())

注意

替换 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"

注意

替换 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

# Load model
model = YOLO("yolov8n-cls.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
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"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (use actual MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# Load image and send request
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

# Load model
model = YOLO("yolov8n.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
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"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (use actual MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# Load image and send request
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
          }
        }
      ],
      "shape": [
        750,
        600
      ],
      "speed": {
        "inference": 200.8,
        "postprocess": 0.8,
        "preprocess": 2.8
      }
    }
  ],
  "metadata": ...
}

OBB

OBB 模型

from ultralytics import YOLO

# Load model
model = YOLO("yolov8n-obb.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
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"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (use actual MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# Load image and send request
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": 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": ...
}

分割

分割模型

from ultralytics import YOLO

# Load model
model = YOLO("yolov8n-seg.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
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"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (use actual MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# Load image and send request
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
          },
          "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": ...
}

姿势估计

姿势估计模型

from ultralytics import YOLO

# Load model
model = YOLO("yolov8n-pose.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
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"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (use actual MODEL_ID)
data = {"model": "https://hub.ultralytics.com/models/MODEL_ID", "imgsz": 640, "conf": 0.25, "iou": 0.45}

# Load image and send request
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": ...
}


📅 创建于 1 年前 ✏️ 更新于 5 个月前

评论