Spaces:
Runtime error
Runtime error
Commit
·
cfa05ff
1
Parent(s):
f1213ed
Using phrases for plotting
Browse files
app.py
CHANGED
|
@@ -15,9 +15,9 @@ from segment_anything import SamPredictor
|
|
| 15 |
from supervision.detection.utils import mask_to_polygons
|
| 16 |
from supervision.detection.utils import xywh_to_xyxy
|
| 17 |
|
| 18 |
-
if os.environ.get(
|
| 19 |
-
result = subprocess.run([
|
| 20 |
-
print(f
|
| 21 |
|
| 22 |
sys.path.append("tag2text")
|
| 23 |
sys.path.append("GroundingDINO")
|
|
@@ -80,7 +80,9 @@ sam_predictor = SamPredictor(sam)
|
|
| 80 |
sam_automask_generator = SamAutomaticMaskGenerator(sam)
|
| 81 |
|
| 82 |
grounding_dino_model = DinoModel(
|
| 83 |
-
model_config_path=dino_config_file,
|
|
|
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
|
|
@@ -104,7 +106,7 @@ def process(image_path, task, prompt, box_threshold, text_threshold, iou_thresho
|
|
| 104 |
metadata["image"]["height"] = h
|
| 105 |
|
| 106 |
# Generate tags
|
| 107 |
-
if task in ["auto", "
|
| 108 |
tags, caption = generate_tags(tag2text_model, image_pil, "None", device)
|
| 109 |
prompt = " . ".join(tags)
|
| 110 |
print(f"Caption: {caption}")
|
|
@@ -129,12 +131,13 @@ def process(image_path, task, prompt, box_threshold, text_threshold, iou_thresho
|
|
| 129 |
iou_threshold=iou_threshold,
|
| 130 |
post_process=True,
|
| 131 |
)
|
|
|
|
| 132 |
|
| 133 |
# Draw boxes
|
| 134 |
box_annotator = sv.BoxAnnotator()
|
| 135 |
labels = [
|
| 136 |
-
f"{
|
| 137 |
-
for
|
| 138 |
]
|
| 139 |
image = box_annotator.annotate(
|
| 140 |
scene=image, detections=detections, labels=labels
|
|
@@ -179,24 +182,24 @@ def process(image_path, task, prompt, box_threshold, text_threshold, iou_thresho
|
|
| 179 |
|
| 180 |
# ToDo: Extract metadata
|
| 181 |
if detections:
|
| 182 |
-
|
| 183 |
-
for (xyxy, mask, confidence,
|
| 184 |
detections, detections.area, detections.box_area
|
| 185 |
):
|
| 186 |
annotation = {
|
| 187 |
-
"id":
|
| 188 |
"bbox": [int(x) for x in xyxy],
|
| 189 |
"box_area": float(box_area),
|
| 190 |
}
|
| 191 |
-
if
|
| 192 |
-
annotation["
|
| 193 |
-
annotation["label"] =
|
| 194 |
if mask is not None:
|
| 195 |
# annotation["segmentation"] = mask_to_polygons(mask)
|
| 196 |
annotation["area"] = int(area)
|
| 197 |
-
annotation["predicted_iou"] = float(scores[
|
| 198 |
metadata["annotations"].append(annotation)
|
| 199 |
-
|
| 200 |
|
| 201 |
meta_file = tempfile.NamedTemporaryFile(delete=False, suffix=".json")
|
| 202 |
meta_file_path = meta_file.name
|
|
|
|
| 15 |
from supervision.detection.utils import mask_to_polygons
|
| 16 |
from supervision.detection.utils import xywh_to_xyxy
|
| 17 |
|
| 18 |
+
if os.environ.get("IS_MY_DEBUG") is None:
|
| 19 |
+
result = subprocess.run(["pip", "install", "-e", "GroundingDINO"], check=True)
|
| 20 |
+
print(f"pip install GroundingDINO = {result}")
|
| 21 |
|
| 22 |
sys.path.append("tag2text")
|
| 23 |
sys.path.append("GroundingDINO")
|
|
|
|
| 80 |
sam_automask_generator = SamAutomaticMaskGenerator(sam)
|
| 81 |
|
| 82 |
grounding_dino_model = DinoModel(
|
| 83 |
+
model_config_path=dino_config_file,
|
| 84 |
+
model_checkpoint_path=dino_checkpoint,
|
| 85 |
+
device=device,
|
| 86 |
)
|
| 87 |
|
| 88 |
|
|
|
|
| 106 |
metadata["image"]["height"] = h
|
| 107 |
|
| 108 |
# Generate tags
|
| 109 |
+
if task in ["auto", "detect"] and prompt == "":
|
| 110 |
tags, caption = generate_tags(tag2text_model, image_pil, "None", device)
|
| 111 |
prompt = " . ".join(tags)
|
| 112 |
print(f"Caption: {caption}")
|
|
|
|
| 131 |
iou_threshold=iou_threshold,
|
| 132 |
post_process=True,
|
| 133 |
)
|
| 134 |
+
print(phrases)
|
| 135 |
|
| 136 |
# Draw boxes
|
| 137 |
box_annotator = sv.BoxAnnotator()
|
| 138 |
labels = [
|
| 139 |
+
f"{phrases[i]} {detections.confidence[i]:0.2f}"
|
| 140 |
+
for i in range(len(phrases))
|
| 141 |
]
|
| 142 |
image = box_annotator.annotate(
|
| 143 |
scene=image, detections=detections, labels=labels
|
|
|
|
| 182 |
|
| 183 |
# ToDo: Extract metadata
|
| 184 |
if detections:
|
| 185 |
+
i = 0
|
| 186 |
+
for (xyxy, mask, confidence, _, _), area, box_area in zip(
|
| 187 |
detections, detections.area, detections.box_area
|
| 188 |
):
|
| 189 |
annotation = {
|
| 190 |
+
"id": i + 1,
|
| 191 |
"bbox": [int(x) for x in xyxy],
|
| 192 |
"box_area": float(box_area),
|
| 193 |
}
|
| 194 |
+
if confidence:
|
| 195 |
+
annotation["confidence"] = float(confidence)
|
| 196 |
+
annotation["label"] = phrases[i]
|
| 197 |
if mask is not None:
|
| 198 |
# annotation["segmentation"] = mask_to_polygons(mask)
|
| 199 |
annotation["area"] = int(area)
|
| 200 |
+
annotation["predicted_iou"] = float(scores[i])
|
| 201 |
metadata["annotations"].append(annotation)
|
| 202 |
+
i += 1
|
| 203 |
|
| 204 |
meta_file = tempfile.NamedTemporaryFile(delete=False, suffix=".json")
|
| 205 |
meta_file_path = meta_file.name
|