跳转至内容

Ultralytics HUBInference API

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

Ultralytics HUB 模型页面内部署选项卡的截图,其中一个箭头指向专用Inference API 卡,另一个箭头指向共享Inference API 卡


观看: Ultralytics HUBInference API 演练

专用Inference API

为了满足用户的高需求和广泛兴趣,我们非常高兴地推出了Ultralytics HUB专用Inference API),在专用环境中为专业版用户提供单击部署功能!

注意

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

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

要使用Ultralytics HUB专用Inference API,请单击 "启动端点"按钮。然后,按照以下指南中的说明使用独特的端点 URL。

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

提示

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

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

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

共享Inference API

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

Ultralytics HUB共享Inference API 的使用限制如下:

  • 每小时100次呼叫

Python

要使用Python 访问Ultralytics HUB Inference 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 以及您想要运行推理的图像路径。

如果您正在使用我们的 专用Inference API,替换 url 也一样。

cURL

要使用 cURL 访问Ultralytics HUB Inference 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 以及您想要运行推理的图像路径。

如果您正在使用我们的 专用Inference API,替换 url 也一样。

参数

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

参数默认值类型描述
filefile用于推理的图像或视频文件。
imgsz640int输入图像的尺寸,有效范围是 32 - 1280 像素。
conf0.25float预测的置信度阈值,有效范围 0.01 - 1.0.
iou0.45float交并比 (IoU)阈值,有效范围 0.0 - 0.95.

响应

Ultralytics HUB Inference 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 年前创建 ✏️ 8 个月前更新
glenn-jochersergiuwaxmannMatthewNoycejk4eUltralyticsAssistantRizwanMunawarpriytosh-tripathi

评论