Spaces:
Runtime error
Runtime error
Commit
·
b8712f0
1
Parent(s):
cfa05ff
Update annotate_anything.py script
Browse files- annotate_anything.py +16 -14
annotate_anything.py
CHANGED
|
@@ -73,7 +73,7 @@ def process(
|
|
| 73 |
|
| 74 |
# Detect boxes
|
| 75 |
if prompt != "":
|
| 76 |
-
detections,
|
| 77 |
grounding_dino_model,
|
| 78 |
image,
|
| 79 |
caption=prompt,
|
|
@@ -88,8 +88,8 @@ def process(
|
|
| 88 |
# Draw boxes
|
| 89 |
box_annotator = sv.BoxAnnotator()
|
| 90 |
labels = [
|
| 91 |
-
f"{
|
| 92 |
-
for
|
| 93 |
]
|
| 94 |
box_image = box_annotator.annotate(
|
| 95 |
scene=image, detections=detections, labels=labels
|
|
@@ -145,22 +145,24 @@ def process(
|
|
| 145 |
|
| 146 |
# ToDo: Extract metadata
|
| 147 |
if detections:
|
| 148 |
-
|
| 149 |
-
for (xyxy, mask, confidence,
|
| 150 |
-
detections, detections.area, detections.box_area
|
| 151 |
):
|
| 152 |
annotation = {
|
| 153 |
-
"id":
|
| 154 |
"bbox": [int(x) for x in xyxy],
|
| 155 |
"box_area": float(box_area),
|
| 156 |
}
|
| 157 |
-
if
|
| 158 |
-
annotation["
|
| 159 |
-
annotation["label"] =
|
| 160 |
if mask is not None:
|
|
|
|
| 161 |
annotation["area"] = int(area)
|
| 162 |
-
annotation["predicted_iou"] = float(
|
| 163 |
metadata["annotations"].append(annotation)
|
|
|
|
| 164 |
|
| 165 |
if output_dir and save_mask:
|
| 166 |
mask_image_path = os.path.join(
|
|
@@ -169,8 +171,6 @@ def process(
|
|
| 169 |
metadata["assets"]["intermediate_mask"].append(mask_image_path)
|
| 170 |
Image.fromarray(mask * 255).save(mask_image_path)
|
| 171 |
|
| 172 |
-
id += 1
|
| 173 |
-
|
| 174 |
if output_dir:
|
| 175 |
meta_file_path = os.path.join(output_dir, basename + "_meta.json")
|
| 176 |
with open(meta_file_path, "w") as fp:
|
|
@@ -246,7 +246,9 @@ def main(args: argparse.Namespace) -> None:
|
|
| 246 |
cache_dir=weight_dir,
|
| 247 |
)
|
| 248 |
grounding_dino_model = DinoModel(
|
| 249 |
-
model_config_path=dino_config_file,
|
|
|
|
|
|
|
| 250 |
)
|
| 251 |
|
| 252 |
if task in ["auto", "segment"]:
|
|
|
|
| 73 |
|
| 74 |
# Detect boxes
|
| 75 |
if prompt != "":
|
| 76 |
+
detections, phrases, classes = detect(
|
| 77 |
grounding_dino_model,
|
| 78 |
image,
|
| 79 |
caption=prompt,
|
|
|
|
| 88 |
# Draw boxes
|
| 89 |
box_annotator = sv.BoxAnnotator()
|
| 90 |
labels = [
|
| 91 |
+
f"{phrases[i]} {detections.confidence[i]:0.2f}"
|
| 92 |
+
for i in range(len(phrases))
|
| 93 |
]
|
| 94 |
box_image = box_annotator.annotate(
|
| 95 |
scene=image, detections=detections, labels=labels
|
|
|
|
| 145 |
|
| 146 |
# ToDo: Extract metadata
|
| 147 |
if detections:
|
| 148 |
+
i = 0
|
| 149 |
+
for (xyxy, mask, confidence, _, _), area, box_area in zip(
|
| 150 |
+
detections, detections.area, detections.box_area
|
| 151 |
):
|
| 152 |
annotation = {
|
| 153 |
+
"id": i + 1,
|
| 154 |
"bbox": [int(x) for x in xyxy],
|
| 155 |
"box_area": float(box_area),
|
| 156 |
}
|
| 157 |
+
if confidence:
|
| 158 |
+
annotation["confidence"] = float(confidence)
|
| 159 |
+
annotation["label"] = phrases[i]
|
| 160 |
if mask is not None:
|
| 161 |
+
# annotation["segmentation"] = mask_to_polygons(mask)
|
| 162 |
annotation["area"] = int(area)
|
| 163 |
+
annotation["predicted_iou"] = float(scores[i])
|
| 164 |
metadata["annotations"].append(annotation)
|
| 165 |
+
i += 1
|
| 166 |
|
| 167 |
if output_dir and save_mask:
|
| 168 |
mask_image_path = os.path.join(
|
|
|
|
| 171 |
metadata["assets"]["intermediate_mask"].append(mask_image_path)
|
| 172 |
Image.fromarray(mask * 255).save(mask_image_path)
|
| 173 |
|
|
|
|
|
|
|
| 174 |
if output_dir:
|
| 175 |
meta_file_path = os.path.join(output_dir, basename + "_meta.json")
|
| 176 |
with open(meta_file_path, "w") as fp:
|
|
|
|
| 246 |
cache_dir=weight_dir,
|
| 247 |
)
|
| 248 |
grounding_dino_model = DinoModel(
|
| 249 |
+
model_config_path=dino_config_file,
|
| 250 |
+
model_checkpoint_path=dino_checkpoint,
|
| 251 |
+
device=device,
|
| 252 |
)
|
| 253 |
|
| 254 |
if task in ["auto", "segment"]:
|