์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

YOLO ์ถ”๋ก  API

YOLO ์ถ”๋ก  API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด RESTful API๋ฅผ ํ†ตํ•ด YOLOv8 ๊ฐ์ฒด ๊ฐ์ง€ ๊ธฐ๋Šฅ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด YOLOv8 ํ™˜๊ฒฝ์„ ๋กœ์ปฌ์— ์„ค์น˜ ๋ฐ ์„ค์ •ํ•  ํ•„์š” ์—†์ด ์ด๋ฏธ์ง€์—์„œ ๊ฐ์ฒด ๊ฐ์ง€๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ถ”๋ก  API ์Šคํฌ๋ฆฐ์ƒท ํ•™์Šต๋œ ๋ชจ๋ธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ํƒญ์˜ ์ถ”๋ก  API ์„น์…˜ ์Šคํฌ๋ฆฐ์ƒท.

API URL

API URL์€ YOLO ์ถ”๋ก  API์— ์•ก์„ธ์Šคํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ฃผ์†Œ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ธฐ๋ณธ URL์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

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

์‚ฌ์šฉ ์˜ˆ Python

Python ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •๋œ ๋ชจ๋ธ ๋ฐ API ํ‚ค๋กœ YOLO ์ถ”๋ก  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 ์ถ”๋ก  API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. curl ๋ช…๋ น์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. Replace 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 ์ถ”๋ก  API์— POST ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค. MODEL_ID ๋ฅผ URL์— ์ถ”๊ฐ€ํ•˜๊ณ  API_KEY ์š”์ฒญ์—์„œ headers์— ์ง€์ •๋œ ์ด๋ฏธ์ง€ ํŒŒ์ผ๊ณผ ํ•จ๊ป˜ @path/to/image.jpg.

๋‹ค์Œ์€ size, confidence๋ฐ iou ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ API 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, confidence๋ฐ iou๋ฅผ ์„ค์ •ํ•˜๋ฉด API๊ฐ€ ์ด๋ฏธ์ง€ ํฌ๊ธฐ 640์—์„œ ์‹ ๋ขฐ๋„ 0.25 ๋ฐ 0.45์˜ IoU ์ž„๊ณ„๊ฐ’์œผ๋กœ ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ 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 ์ถ”๋ก  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 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๋Š” ๋กœ์ปฌ ์ถ”๋ก , 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 ํ‚คํฌ์ธํŠธ ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ์—๋Š” 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-02-13
์ž‘์„ฑ์ž: glenn-jocher (3), priytosh-tripathi (1)

๋Œ“๊ธ€