跳至内容

Ultralytics HUB 推断应用程序接口

Ultralytics HUB Inference API 允许您通过我们的 REST API 运行推理,而无需在本地安装和设置Ultralytics YOLO 环境。

Ultralytics 模型页面内部署选项卡的 HUB 截图,箭头指向Ultralytics Inference API 卡


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

Python

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

import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

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

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())

备注

更换 MODEL_ID 输入所需的型号 ID、 API_KEY 您的实际应用程序接口密钥,以及 path/to/image.jpg 的路径。

cURL

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

curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"

备注

更换 MODEL_ID 输入所需的型号 ID、 API_KEY 您的实际应用程序接口密钥,以及 path/to/image.jpg 的路径。

论据

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

论据 默认值 类型 说明
image image 用于推理的图像文件。
url str 图像的 URL(如果未传递文件)。
size 640 int 输入图像的大小,有效范围为 32 - 1280 像素。
confidence 0.25 float 预测的置信度阈值,有效范围 0.01 - 1.0.
iou 0.45 float 超过并集 (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].tojson())
curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

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

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92
    }
  ]
}

检测

检测模型

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].tojson())
curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

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

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92,
      width: 0.4893378019332886,
      height: 0.7437513470649719,
      xcenter: 0.4434437155723572,
      ycenter: 0.5198975801467896
    }
  ]
}

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://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

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

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92,
      obb: [
        0.669310450553894,
        0.6247171759605408,
        0.9847468137741089,
        ...
      ]
    }
  ]
}

细分

细分模型

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://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

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

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92,
      segment: [0.44140625, 0.15625, 0.439453125, ...]
    }
  ]
}

姿势

姿势模型

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://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

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

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92,
      keypoints: [
        0.5290805697441101,
        0.20698919892311096,
        1.0,
        0.5263055562973022,
        0.19584226608276367,
        1.0,
        0.5094948410987854,
        0.19120082259178162,
        1.0,
        ...
      ]
    }
  ]
}


创建于 2024-01-23,更新于 2024-06-22
作者:glenn-jocher(9),sergiuwaxmann(2),RizwanMunawar(1),priytosh-tripathi(1)

评论