Supervision: Toolkit Computer Vision dari Roboflow
Dalam proyek computer vision, setelah model mendeteksi objek, Anda masih perlu melakukan banyak hal: menggambar bounding box, memfilter deteksi, menghitung objek, dan melacak pergerakan. Semua ini membutuhkan kode yang cukup banyak dan repetitif jika ditulis dari nol.
Supervision dari Roboflow hadir sebagai toolkit Python yang menyederhanakan semua pekerjaan pasca-deteksi ini. Dengan API yang konsisten, Supervision bekerja dengan berbagai model deteksi seperti YOLO, Detectron2, dan SAM, memungkinkan Anda fokus pada logika aplikasi bukan boilerplate code.Apa Itu Supervision?
Supervision adalah library Python open-source dari Roboflow untuk memudahkan pekerjaan computer vision. Fitur utamanya meliputi:
- Detections Object: Representasi universal untuk hasil deteksi dari berbagai model
- Annotators: Berbagai tipe annotator visual (bounding box, mask, label, trace, halo)
- Filtering: Filter deteksi berdasarkan confidence, kelas, zona, dan kriteria lainnya
- Polygon Zones: Definisi zona poligon untuk counting dan analisis area
- Line Counters: Penghitung objek yang melewati garis virtual
- Object Tracking: Integrasi ByteTrack untuk pelacakan objek lintas frame
- Video Processing: Utilitas untuk memproses video frame-by-frame
- FPS Monitor: Monitoring performa real-time
Instalasi
Instalasi Dasar
pip install supervision
Instalasi dengan Dependensi Model
# Dengan YOLO (Ultralytics)
pip install supervision ultralytics
Dengan Detectron2
pip install supervision detectron2
Dengan SAM (Segment Anything)
pip install supervision segment-anything
Verifikasi Instalasi
import supervision as sv
print(f"Supervision version: {sv.version}")
Detections Object
sv.Detections adalah objek inti di Supervision yang merepresentasikan hasil deteksi secara universal.
Membuat Detections dari Berbagai Model
import supervision as sv
import numpy as np
Dari Ultralytics YOLO
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
results = model("image.jpg")
detections = sv.Detections.fromultralytics(results[0])
print(f"Jumlah deteksi: {len(detections)}")
print(f"Bounding boxes: {detections.xyxy}")
print(f"Confidence: {detections.confidence}")
print(f"Class IDs: {detections.classid}")
Dari Detectron2
from detectron2.engine import DefaultPredictor
from detectron2.config import getcfg
cfg = getcfg()
cfg.mergefromfile("config.yaml")
predictor = DefaultPredictor(cfg)
outputs = predictor(image)
detections = sv.Detections.fromdetectron2(outputs)
Dari SAM (Segment Anything)
from segmentanything import sammodelregistry, SamAutomaticMaskGenerator
sam = sammodelregistryvith.pth"">"vith"
maskgenerator = SamAutomaticMaskGenerator(sam)
masks = maskgenerator.generate(image)
detections = sv.Detections.fromsam(masks)
Membuat Detections Manual
# Buat detections secara manual
detections = sv.Detections(
xyxy=np.array([
[100, 200, 300, 400],
[150, 250, 350, 450]
]),
confidence=np.array([0.95, 0.87]),
classid=np.array([0, 1])
)
Annotators: Visualisasi Deteksi
Supervision menyediakan berbagai annotator untuk memvisualisasikan hasil deteksi.
Bounding Box Annotator
import supervision as sv
import cv2
image = cv2.imread("image.jpg")
Annotator bounding box
bboxannotator = sv.BoxAnnotator(
thickness=2,
color=sv.ColorPalette.fromhex(["#FF0000", "#00FF00", "#0000FF"])
)
annotatedimage = bboxannotator.annotate(
scene=image.copy(),
detections=detections
)
cv2.imwrite("annotatedbbox.jpg", annotatedimage)