YOLO 推論API
YOLO Inference API では、RESTful API を介してYOLOv8 のオブジェクト検出機能にアクセスできます。これにより、YOLOv8 環境をローカルにインストールしてセットアップしなくても、画像上でオブジェクト検出を実行できるようになります。
学習済みモデルプレビュータブの推論APIセクションのスクリーンショット。
見るんだ: Ultralytics HUB推論APIウォークスルー
API URL
API URLは、YOLO Inference APIにアクセスするためのアドレスである。この場合、ベースURLは
使用例Python
Python を使って、指定したモデルとAPIキーでYOLO Inference APIにアクセスするには、以下のコードを使うことができる:
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での使用例
クライアントURL(cURL)を使用して、YOLO Inference APIを使用することができます。 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 に、指定された MODEL_ID
URLと API_KEY
リクエスト headers
で指定された画像ファイルとともに @path/to/image.jpg
.
を渡す例である。 size
, confidence
そして iou
引数は 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
, confidence
そして iou
これは画像サイズ640で、信頼度とIoUのしきい値を0.25と0.45にして推論を実行するようAPIに指示する。
これは、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
Python これらのメソッドはすべて、同じJSONレスポンス・フォーマットを生成する。これらのメソッドはすべて、同じJSONレスポンス・フォーマットを生成する。
モデルのJSONレスポンスを検出する
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
Python これらのメソッドはすべて、同じJSONレスポンス・フォーマットを生成する。これらのメソッドはすべて、同じJSONレスポンス・フォーマットを生成する。
セグメントモデルJSONレスポンス
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
x
そして y
長さはオブジェクトによって異なる。大きなオブジェクトや複雑なオブジェクトでは、セグメント点の数が多くなることがあります。
{
"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
Python これらのメソッドはすべて、同じJSONレスポンス・フォーマットを生成する。これらのメソッドはすべて、同じJSONレスポンス・フォーマットを生成する。
ポーズモデルJSONレスポンス
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
...
]
}
}
]
}