Grounding DINO
テキストプロンプト付き物体検出に Grounding DINO を、Dedicated Deployment またはセルフホスト Inference で使用します
コード例
3
モデルを実行する
import base64
import os
import cv2
import numpy as np
import requests
import supervision as sv
URL = "https://your-deployment.roboflow.cloud"
image = sv.load_image_from_url("https://media.roboflow.com/notebooks/examples/dog.jpeg")
_, buffer = cv2.imencode(".jpg", image)
image_base64 = base64.b64encode(buffer).decode("utf-8")
response = requests.post(
f"{URL}/grounding_dino/infer",
json={
"api_key": os.environ["ROBOFLOW_API_KEY"],
"image": {"type": "base64", "value": image_base64},
"text": ["犬", "人", "リュックサック"],
},
)
preds = response.json()["predictions"]
xyxys = [
[p["x"] - p["width"] / 2, p["y"] - p["height"] / 2,
p["x"] + p["width"] / 2, p["y"] + p["height"] / 2]
for p in preds
]
detections = sv.Detections(
xyxy=np.array(xyxys, dtype=float),
class_id=np.array([p.get("class_id", 0) for p in preds]),
confidence=np.array([p["confidence"] for p in preds], dtype=float),
data={"class_name": np.array([p["class"] for p in preds])},
)
labels = [f"{p['class']} {p['confidence']:.2f}" for p in preds]
annotated = sv.BoxAnnotator().annotate(image.copy(), detections)
annotated = sv.LabelAnnotator().annotate(annotated, detections, labels=labels)
cv2.imwrite("dog_annotated.png", annotated)
推論速度
モデル
レイテンシ(ms)
Inference(セルフホスト)で使用
2
モデルを実行する
from inference.models.grounding_dino import GroundingDINO
model = GroundingDINO(api_key="YOUR_API_KEY")
results = model.infer(
{
"image": {
"type": "url",
"value": "https://media.roboflow.com/fruit.png",
},
"text": ["りんご"],
# 省略可能な閾値。どちらもデフォルトは 0.5 です
"box_threshold": 0.5,
"text_threshold": 0.5,
}
)
print(results)最終更新
役に立ちましたか?