YOLO ์ถ๋ก API
YOLO ์ถ๋ก API๋ฅผ ์ฌ์ฉํ๋ฉด RESTful API๋ฅผ ํตํด YOLOv8 ๊ฐ์ฒด ๊ฐ์ง ๊ธฐ๋ฅ์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด YOLOv8 ํ๊ฒฝ์ ๋ก์ปฌ์ ์ค์น ๋ฐ ์ค์ ํ ํ์ ์์ด ์ด๋ฏธ์ง์์ ๊ฐ์ฒด ๊ฐ์ง๋ฅผ ์คํํ ์ ์์ต๋๋ค.
ํ์ต๋ ๋ชจ๋ธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ํญ์ ์ถ๋ก API ์น์ ์คํฌ๋ฆฐ์ท.
Watch: Ultralytics HUB ์ถ๋ก API ์ฐ์ต
API URL
API URL์ YOLO ์ถ๋ก API์ ์ก์ธ์คํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ฃผ์์ ๋๋ค. ์ด ๊ฒฝ์ฐ ๊ธฐ๋ณธ URL์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
์ฌ์ฉ ์ 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
๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ํฌ๊ธฐ 640์์ ์ ๋ขฐ๋ 0.25 ๋ฐ 0.45์ IoU ์๊ณ๊ฐ์ผ๋ก ์ถ๋ก ์ ์คํํ๋๋ก 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 ์ถ๋ก API๋ ํ์ง ๊ฒฐ๊ณผ๊ฐ ํฌํจ๋ JSON ๋ชฉ๋ก์ ๋ฐํํฉ๋๋ค. JSON ๋ชฉ๋ก์ ํ์์ ๋ก์ปฌ์์ ์์ฑ๋ ๊ฒ๊ณผ ๋์ผํฉ๋๋ค. results[0].tojson()
๋ช
๋ น์ ์ฌ์ฉํฉ๋๋ค.
JSON ๋ชฉ๋ก์๋ ๊ฐ์ง๋ ๊ฐ์ฒด, ์ขํ, ํด๋์ค ๋ฐ ์ ๋ขฐ๋ ์ ์์ ๋ํ ์ ๋ณด๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
๋ชจ๋ธ ํ์ ๊ฐ์ง
YOLO ํ์ง ๋ชจ๋ธ๊ณผ ๊ฐ์ yolov8n.pt
๋ ๋ก์ปฌ ์ถ๋ก , cURL ์ถ๋ก ๋ฐ 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
๋ ๋ก์ปฌ ์ถ๋ก , cURL ์ถ๋ก ๋ฐ 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
๋ ๋ก์ปฌ ์ถ๋ก , cURL ์ถ๋ก ๋ฐ 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-ํคํฌ์ธํธ ์ฌ์ ํ์ต ๋ชจ๋ธ์๋ 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
์์ฑ์: ๋ฆฌ์ฆ์ ๋ฌด๋์๋ฅด (1), ๊ธ๋ ์กฐ์ฒ (3), ํ๋ฆฌํ ์ ํธ๋ฆฌํํฐ (1)