Supervision Integration with Other frameworks. #233
hardikdava
started this conversation in
General
Replies: 2 comments 2 replies
-
As for |
Beta Was this translation helpful? Give feedback.
2 replies
-
Supervision Framework Integration GuideOverviewThis guide demonstrates how to integrate Supervision with various popular frameworks and tools. We'll cover integration patterns, best practices, and common pitfalls to avoid. 1. FastAPI Integrationfrom fastapi import FastAPI
from supervision.tools.detectors import Detector
from supervision.draw.annotators import BoundingBoxAnnotator
app = FastAPI()
detector = Detector(model="yolov8n.pt")
annotator = BoundingBoxAnnotator()
@app.post("/detect")
async def detect_objects(image: UploadFile):
# Process image with Supervision
image_array = await image.read()
detections = detector.detect(image_array)
# Annotate results
annotated_frame = annotator.annotate(
scene=image_array.copy(),
detections=detections
)
return {
"detections": detections.to_dict(),
"annotated_image": annotated_frame.tolist()
} 2. PyTorch Integrationimport torch
import supervision as sv
from supervision.detection.tools import TorchDetector
class CustomModel(torch.nn.Module):
def __init__(self):
super().__init__()
# Your model architecture here
def forward(self, x):
# Your forward pass here
return x
# Create Supervision detector wrapper
model = CustomModel()
detector = TorchDetector(
model=model,
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
)
# Use with Supervision tools
annotator = sv.BoundingBoxAnnotator()
tracker = sv.ByteTracker()
def process_frame(frame):
detections = detector.detect(frame)
tracked_detections = tracker.track(detections)
annotated_frame = annotator.annotate(
scene=frame.copy(),
detections=tracked_detections
)
return annotated_frame 3. OpenCV Integrationimport cv2
import supervision as sv
# Initialize Supervision components
detector = sv.Detector(model="yolov8n.pt")
box_annotator = sv.BoundingBoxAnnotator()
label_annotator = sv.LabelAnnotator()
def process_video(video_path):
cap = cv2.VideoCapture(video_path)
while True:
ret, frame = cap.read()
if not ret:
break
# Detect objects
detections = detector.detect(frame)
# Annotate frame
annotated_frame = box_annotator.annotate(
scene=frame.copy(),
detections=detections
)
annotated_frame = label_annotator.annotate(
scene=annotated_frame,
detections=detections
)
cv2.imshow("Processed", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows() 4. TensorFlow Integrationimport tensorflow as tf
import supervision as sv
from supervision.detection.tools import TensorFlowDetector
# Create TensorFlow model wrapper
class TFDetector(TensorFlowDetector):
def __init__(self, model_path):
self.model = tf.saved_model.load(model_path)
def detect(self, image):
# Preprocess image
input_tensor = tf.convert_to_tensor(image)
input_tensor = tf.expand_dims(input_tensor, 0)
# Run inference
detections = self.model(input_tensor)
# Convert to Supervision format
return self._convert_to_supervision(detections)
def _convert_to_supervision(self, detections):
# Convert TensorFlow detection format to Supervision format
return sv.Detections(
xyxy=detections['detection_boxes'],
confidence=detections['detection_scores'],
class_id=detections['detection_classes']
)
# Usage
detector = TFDetector("path/to/model")
annotator = sv.BoundingBoxAnnotator()
def process_image(image):
detections = detector.detect(image)
annotated_image = annotator.annotate(
scene=image.copy(),
detections=detections
)
return annotated_image 5. MLflow Integrationimport mlflow
import supervision as sv
class SupervisionMLflowTracker:
def __init__(self, experiment_name):
mlflow.set_experiment(experiment_name)
self.detector = sv.Detector(model="yolov8n.pt")
def track_detections(self, image, log_artifacts=True):
with mlflow.start_run():
# Log parameters
mlflow.log_param("model_type", "yolov8n")
# Perform detection
detections = self.detector.detect(image)
# Log metrics
mlflow.log_metric("num_detections", len(detections))
mlflow.log_metric("avg_confidence",
detections.confidence.mean())
if log_artifacts:
# Save and log annotated image
annotator = sv.BoundingBoxAnnotator()
annotated_image = annotator.annotate(
scene=image.copy(),
detections=detections
)
mlflow.log_artifact(annotated_image, "detections.jpg")
return detections Best Practices
def safe_detect(detector, image):
try:
detections = detector.detect(image)
if detections is None:
raise ValueError("No detections returned")
return detections
except Exception as e:
logger.error(f"Detection failed: {str(e)}")
return None
class SupervisionManager:
def __init__(self):
self.detector = None
self.annotators = {}
def initialize(self, model_path):
if self.detector is None:
self.detector = sv.Detector(model=model_path)
def get_annotator(self, annotator_type):
if annotator_type not in self.annotators:
self.annotators[annotator_type] = \
getattr(sv, annotator_type)()
return self.annotators[annotator_type]
def cleanup(self):
self.detector = None
self.annotators.clear() Common Pitfalls and Solutions
Testing Integrationimport pytest
import supervision as sv
class TestSupervisionIntegration:
@pytest.fixture
def detector(self):
return sv.Detector(model="yolov8n.pt")
def test_detection(self, detector):
image = load_test_image()
detections = detector.detect(image)
assert detections is not None
assert len(detections) > 0
def test_annotation(self, detector):
image = load_test_image()
detections = detector.detect(image)
annotator = sv.BoundingBoxAnnotator()
annotated = annotator.annotate(
scene=image.copy(),
detections=detections
)
assert annotated.shape == image.shape |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello all, I am creating this discussion to create a list of good frameworks which supports computer vision tasks. The main idea is to integrate result format conversion to
supervision
. Once we have the final list of frameworks then we can do lot of things with it such an example would be to benchmark accuracy performance with all the possible models.List of frameworks:
The community will have the standard way to compare performance. It is observed that all the frameworks uses their own benchmarking method such as mAP for object detection. This way we all have fair comparision rather than the results from their own implementation. This can leads to standardizing the metric for comparision.
@SkalskiP @onuralpszr @kirilllzaitsev
Beta Was this translation helpful? Give feedback.
All reactions