跳至内容

Ultralytics HUB 推断应用程序接口

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

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


观看: Ultralytics HUB 推断应用程序接口演练

专用推理应用程序接口

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

备注

我们很高兴能在公开测试期间免费提供该功能,作为专业计划的一部分,未来还可能提供付费级别。

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

要使用Ultralytics HUB 专用推理 API,请单击 " 启动端点"按钮。然后,按照以下指南中的描述使用唯一的端点 URL。

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

提示

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

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

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

共享推理 API

要使用Ultralytics HUB 共享推理应用程序接口,请遵循以下指南。

免费用户的使用限制如下:

  • 100 次/小时
  • 每月 1000 次通话

专业版用户有以下使用限制:

  • 1000 次/小时
  • 每月 10000 次通话

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 您的实际应用程序接口密钥,以及 path/to/image.jpg 的路径。

如果您使用我们的 专用推理应用程序接口更换 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 您的实际应用程序接口密钥,以及 path/to/image.jpg 的路径。

如果您使用我们的 专用推理应用程序接口更换 url 也是如此。

论据

有关可用推理参数的完整列表,请参见下表。

论据 默认值 类型 说明
file file 用于推理的图像或视频文件。
imgsz 640 int 输入图像的大小,有效范围为 32 - 1280 像素。
conf 0.25 float 预测的置信度阈值,有效范围 0.01 - 1.0.
iou 0.45 float 联合交叉点 (IoU) threshold, valid range 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": ...
}

📅 Created 8 months ago ✏️ Updated 13 days ago

评论