Views
No views yet

1import requests
2
3import torch
4from PIL import Image
5from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection
6
7model_id = "IDEA-Research/grounding-dino-base"
8device = "cuda" if torch.cuda.is_available() else "cpu"
9
10processor = AutoProcessor.from_pretrained(model_id)
11model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device)
12
13image_url = "http://images.cocodataset.org/val2017/000000039769.jpg"
14image = Image.open(requests.get(image_url, stream=True).raw)
15# Check for cats and remote controls
16# VERY important: text queries need to be lowercased + end with a dot
17text = "a cat. a remote control."
18
19inputs = processor(images=image, text=text, return_tensors="pt").to(device)
20with torch.no_grad():
21 outputs = model(**inputs)
22
23results = processor.post_process_grounded_object_detection(
24 outputs,
25 inputs.input_ids,
26 box_threshold=0.4,
27 text_threshold=0.3,
28 target_sizes=[image.size[::-1]]
29)1@misc{liu2023grounding,
2 title={Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection},
3 author={Shilong Liu and Zhaoyang Zeng and Tianhe Ren and Feng Li and Hao Zhang and Jie Yang and Chunyuan Li and Jianwei Yang and Hang Su and Jun Zhu and Lei Zhang},
4 year={2023},
5 eprint={2303.05499},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV}
8}