跳至内容

YOLO 推理应用程序接口

YOLO Inference API 允许您通过 RESTful API 访问YOLOv8 对象检测功能。这样,您就可以在图像上运行对象检测,而无需在本地安装和设置YOLOv8 环境。

推理应用程序接口截图 训练模型预览选项卡中推理 API 部分的截图。



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

应用程序接口 URL

API URL 是用于访问YOLO Inference API 的地址。在本例中,基本 URL 为

https://api.ultralytics.com/v1/predict

使用示例Python

要使用指定的模型和 API 密钥访问YOLO Inference API(使用Python ),可以使用以下代码:

import requests

# API URL, use actual MODEL_ID
url = f"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())

在本例中,替换 API_KEY 使用您的实际 API 密钥、 MODEL_ID 使用所需的型号 ID,以及 path/to/image.jpg 输入要分析的图像的路径。

使用 cURL 的示例

YOLO 您可以通过使用 curl 命令。替换 API_KEY 使用您的实际 API 密钥、 MODEL_ID 使用所需的型号 ID,以及 image.jpg 输入要分析的图像的路径:

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"

传递参数

该命令会向YOLO Inference API 发送 POST 请求,其中包含指定的 MODEL_ID 中的 API_KEY 在请求中 headers指定的图像文件。 @path/to/image.jpg.

下面是通过 size, confidenceiou 参数,通过应用程序接口 URL 使用 requests Python 中的图书馆:

import requests

# API URL, use actual MODEL_ID
url = f"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())

在这个例子中, data 字典中包含查询参数 size, confidenceiou,告诉应用程序接口在图像大小为 640、置信度和 IoU 阈值为 0.25 和 0.45 时运行推理。

这将在 POST 请求中连同文件一起发送查询参数。有关可用推理参数的完整列表,请参见下表。

推理论证 默认值 类型 说明
size 640 int 有效范围为 32 - 1280 像素
confidence 0.25 float 有效范围为 0.01 - 1.0
iou 0.45 float 有效范围为 0.0 - 0.95
url '' str 如果传递的不是图像文件,可选图像 URL
normalize False bool

返回 JSON 格式

YOLO Inference API 会返回一个包含检测结果的 JSON 列表。JSON 列表的格式与 results[0].tojson() 指挥。

JSON 列表包含有关检测到的对象、其坐标、类别和置信度分数的信息。

检测模型格式

YOLO 检测模型,如 yolov8n.pt可以通过本地推理、cURL 推理和Python 推理返回 JSON 响应。所有这些方法都会产生相同的 JSON 响应格式。

检测模型 JSON 响应

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 = f"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": [
    {
      "name": "person",
      "class": 0,
      "confidence": 0.8359682559967041,
      "box": {
        "x1": 0.08974208831787109,
        "y1": 0.27418340047200523,
        "x2": 0.8706787109375,
        "y2": 0.9887352837456598
      }
    },
    {
      "name": "person",
      "class": 0,
      "confidence": 0.8189555406570435,
      "box": {
        "x1": 0.5847355842590332,
        "y1": 0.05813225640190972,
        "x2": 0.8930277824401855,
        "y2": 0.9903111775716146
      }
    },
    {
      "name": "tie",
      "class": 27,
      "confidence": 0.2909725308418274,
      "box": {
        "x1": 0.3433395862579346,
        "y1": 0.6070465511745877,
        "x2": 0.40964522361755373,
        "y2": 0.9849439832899306
      }
    }
  ]
}

分段模型格式

YOLO 分割模型,如 yolov8n-seg.pt可以通过本地推理、cURL 推理和Python 推理返回 JSON 响应。所有这些方法都会产生相同的 JSON 响应格式。

分段模型 JSON 响应

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 = f"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())

备注 segments xy 不同物体的长度可能不同。较大或较复杂的物体可能有更多的分段点。

{
  "success": True,
  "message": "Inference complete.",
  "data": [
    {
      "name": "person",
      "class": 0,
      "confidence": 0.856913149356842,
      "box": {
        "x1": 0.1064866065979004,
        "y1": 0.2798851860894097,
        "x2": 0.8738358497619629,
        "y2": 0.9894873725043403
      },
      "segments": {
        "x": [
          0.421875,
          0.4203124940395355,
          0.41718751192092896
          ...
        ],
        "y": [
          0.2888889014720917,
          0.2916666567325592,
          0.2916666567325592
          ...
        ]
      }
    },
    {
      "name": "person",
      "class": 0,
      "confidence": 0.8512625694274902,
      "box": {
        "x1": 0.5757311820983887,
        "y1": 0.053943040635850696,
        "x2": 0.8960096359252929,
        "y2": 0.985154045952691
      },
      "segments": {
        "x": [
          0.7515624761581421,
          0.75,
          0.7437499761581421
          ...
        ],
        "y": [
          0.0555555559694767,
          0.05833333358168602,
          0.05833333358168602
          ...
        ]
      }
    },
    {
      "name": "tie",
      "class": 27,
      "confidence": 0.6485961675643921,
      "box": {
        "x1": 0.33911995887756347,
        "y1": 0.6057066175672743,
        "x2": 0.4081430912017822,
        "y2": 0.9916408962673611
      },
      "segments": {
        "x": [
          0.37187498807907104,
          0.37031251192092896,
          0.3687500059604645
          ...
        ],
        "y": [
          0.6111111044883728,
          0.6138888597488403,
          0.6138888597488403
          ...
        ]
      }
    }
  ]
}

姿势模型格式

YOLO 姿势模型,如 yolov8n-pose.pt可以通过本地推理、cURL 推理和Python 推理返回 JSON 响应。所有这些方法都会产生相同的 JSON 响应格式。

姿势模型 JSON 响应

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 = f"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())

注意 COCO-keypoints 预训练模型将有 17 个人类关键点。的 visible 部分表示关键点是可见的还是被遮挡的。被遮挡的关键点可能在图像之外,也可能不可见,例如人的眼睛朝向远离摄像机的方向。

{
  "success": True,
  "message": "Inference complete.",
  "data": [
    {
      "name": "person",
      "class": 0,
      "confidence": 0.8439509868621826,
      "box": {
        "x1": 0.1125,
        "y1": 0.28194444444444444,
        "x2": 0.7953125,
        "y2": 0.9902777777777778
      },
      "keypoints": {
        "x": [
          0.5058594942092896,
          0.5103894472122192,
          0.4920862317085266
          ...
        ],
        "y": [
          0.48964157700538635,
          0.4643048942089081,
          0.4465252459049225
          ...
        ],
        "visible": [
          0.8726999163627625,
          0.653947651386261,
          0.9130823612213135
          ...
        ]
      }
    },
    {
      "name": "person",
      "class": 0,
      "confidence": 0.7474289536476135,
      "box": {
        "x1": 0.58125,
        "y1": 0.0625,
        "x2": 0.8859375,
        "y2": 0.9888888888888889
      },
      "keypoints": {
        "x": [
          0.778544008731842,
          0.7976160049438477,
          0.7530890107154846
          ...
        ],
        "y": [
          0.27595141530036926,
          0.2378823608160019,
          0.23644638061523438
          ...
        ],
        "visible": [
          0.8900790810585022,
          0.789978563785553,
          0.8974530100822449
          ...
        ]
      }
    }
  ]
}



创建于 2024-01-23,更新于 2024-03-06
作者:RizwanMunawar(1)、glenn-jocher(3)、preytosh-tripathi(1)

评论