Grounding DINO
Run the Grounding DINO text-prompted object detection API on a Dedicated Deployment or self-hosted Inference
Grounding DINO is an open-vocabulary object detector. You pass an image and a list of text classes, and the model returns bounding boxes for matching regions without any training.
Grounding DINO API
Get your API Key
Create a Roboflow account, find your key on the Roboflow API settings page and make it available to your shell:
export ROBOFLOW_API_KEY="your-key-here"Run the model
Set URL to your Dedicated Deployment URL or a local Inference server.
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",
headers={"Authorization": f"Bearer {os.environ['ROBOFLOW_API_KEY']}"},
json={
"image": {"type": "base64", "value": image_base64},
"text": ["dog", "person", "backpack"],
},
)
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)
Grounding DINO inference speed
Latency measured with Roboflow Inference on 1x NVIDIA L4, batch size 1, mean after warmup.
grounding-dino
165.4
Measured with two text prompts.
Run Grounding DINO with self-hosted Inference
You can also load Grounding DINO directly into your own Python process with the inference package, skipping the HTTP hop entirely.
Run the model
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": ["apple"],
# Optional thresholds, both default to 0.5
"box_threshold": 0.5,
"text_threshold": 0.5,
}
)
print(results)Replace apple with the objects you want to detect, and tune box_threshold and text_threshold for your use case. See the Grounding DINO README for an explanation of the thresholds.
Last updated
Was this helpful?