File size: 980 Bytes
8938d1b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from autodistill_gpt_4v import GPT4V
from autodistill.detection import CaptionOntology
from autodistill_grounded_sam import GroundedSAM
import supervision as sv
from autodistill.core.custom_detection_model import CustomDetectionModel
import cv2
classes = ["mercedes", "toyota"]
SAMGPT = CustomDetectionModel(
detection_model=GroundedSAM(
CaptionOntology({"car": "car"})
),
classification_model=GPT4V(
CaptionOntology({k: k for k in classes})
)
)
IMAGE = "mercedes.jpeg"
results = SAMGPT.predict(IMAGE)
image = cv2.imread(IMAGE)
annotator = sv.MaskAnnotator()
label_annotator = sv.LabelAnnotator()
labels = [
f"{classes[class_id]} {confidence:0.2f}"
for _, _, confidence, class_id, _ in results
]
annotated_frame = annotator.annotate(
scene=image.copy(), detections=results
)
annotated_frame = label_annotator.annotate(
scene=annotated_frame, labels=labels, detections=results
)
sv.plot_image(annotated_frame, size=(8, 8)) |