Datasets:
Download code/probe_detect.py from spatial-evidence-bench/SpatialEvidenceBench: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/datasets/spatial-evidence-bench/SpatialEvidenceBench/resolve/2b2e98a86663b2fd7de0817c2c5db0d4f2e7bc83/code/probe_detect.py
- Command line
-
hf download hf://datasets/spatial-evidence-bench/SpatialEvidenceBench@2b2e98a86663b2fd7de0817c2c5db0d4f2e7bc83/code/probe_detect.py
-
curl -L -o probe_detect.py https://huggingface.co/datasets/spatial-evidence-bench/SpatialEvidenceBench/resolve/2b2e98a86663b2fd7de0817c2c5db0d4f2e7bc83/code/probe_detect.py
1.08 kB
| import sys | |
| import torch | |
| from PIL import Image | |
| from transformers import AutoModelForZeroShotObjectDetection, AutoProcessor | |
| repo = "/repository/models/grounding-dino-tiny" | |
| processor = AutoProcessor.from_pretrained(repo) | |
| model = AutoModelForZeroShotObjectDetection.from_pretrained(repo).to("cuda:0").eval() | |
| image_path, *labels = sys.argv[1:] | |
| image = Image.open(image_path).convert("RGB") | |
| text = " . ".join(labels) + " ." | |
| inputs = processor(images=image, text=text, return_tensors="pt").to("cuda:0") | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| results = processor.post_process_grounded_object_detection( | |
| outputs, inputs["input_ids"], threshold=0.05, text_threshold=0.05, | |
| target_sizes=[image.size[::-1]])[0] | |
| names = results.get("text_labels") or results["labels"] | |
| rows = [] | |
| for box, score, name in zip(results["boxes"], results["scores"], names): | |
| x0, y0, x1, y1 = [float(v) for v in box] | |
| rows.append((float(score), str(name), round((x1 - x0) / image.size[0], 3), round((y1 - y0) / image.size[1], 3))) | |
| rows.sort(reverse=True) | |
| for r in rows[:12]: | |
| print(r) | |