diff --git a/pyproject.toml b/pyproject.toml index 4344a972..9fa73af0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,7 +67,7 @@ mkdocs-material = "^9.1.4" mkdocstrings = {extras = ["python"], version = ">=0.20,<0.25"} [tool.flake8] -exclude = ".venv" +exclude = ".venv,build,dist,docs,.git,.tox,.eggs,supervision/detection/core.py" max-complexity = 10 max-line-length = 88 extend-ignore = """ @@ -110,6 +110,7 @@ exclude = ''' | build | dist | docs + | supervision/detection/core.py )/ ''' diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 70285c2d..b0aea96e 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -266,6 +266,50 @@ class Detections: class_id=yolo_nas_results.prediction.labels.astype(int), ) + @classmethod + def from_tensorflow( + cls, tensorflow_results: dict, resolution_wh: tuple + ) -> Detections: + """ + Creates a Detections instance from a + [Tensorflow Hub](https://www.tensorflow.org/hub/tutorials/tf2_object_detection) + inference result. + + Args: + tensorflow_results (dict): + The output results from Tensorflow Hub. + + Returns: + Detections: A new Detections object. + + Example: + ```python + >>> import tensorflow as tf + >>> import tensorflow_hub as hub + >>> import numpy as np + >>> import cv2 + + >>> module_handle = "https://tfhub.dev/tensorflow/centernet/hourglass_512x512_kpts/1" + + >>> model = hub.load(module_handle) + + >>> img = np.array(cv2.imread(SOURCE_IMAGE_PATH)) + + >>> result = model(img) + + >>> detections = sv.Detections.from_tensorflow(result) + ``` + """ + boxes = tensorflow_results["detection_boxes"][0].numpy() + boxes[:, [0, 2]] *= resolution_wh[0] + boxes[:, [1, 3]] *= resolution_wh[1] + boxes = boxes[:, [1, 0, 3, 2]] + return cls( + xyxy=boxes, + confidence=tensorflow_results["detection_scores"][0].numpy(), + class_id=tensorflow_results["detection_classes"][0].numpy().astype(int), + ) + @classmethod def from_deepsparse(cls, deepsparse_results) -> Detections: """