From d7e8a11139ffc7f2dff4d3631d175b156b25a1b9 Mon Sep 17 00:00:00 2001 From: James Gallagher Date: Fri, 3 Nov 2023 19:31:46 +0000 Subject: [PATCH 1/9] add sv.Detections.from_tensorflow_hub --- supervision/detection/core.py | 57 +++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 77bfca9d..aa6a7932 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -264,6 +264,63 @@ class Detections: class_id=yolo_nas_results.prediction.labels.astype(int), ) + @classmethod + def from_tensorflow_hub(cls, tensorflow_hub_results, image_size) -> Detections: + """ + Creates a Detections instance from a + [Tensorflow Hub](https://www.tensorflow.org/hub/tutorials/object_detection) + inference result. + + Args: + tensorflow_hub_results (dict): + The output results from Tensorflow Hub. + + Returns: + Detections: A new Detections object. + + Example: + ```python + >>> import tensorflow as tf + >>> from super_gradients.training import models + >>> import tensorflow_hub as hub + + >>> module_handle = "..." + + >>> detector = hub.load(module_handle).signatures['default'] + + >>> # follow https://www.tensorflow.org/hub/tutorials/object_detection to load an image + >>> img = ... + + >>> result = detector(img) + + >>> detections = sv.Detections.from_tensorflow_hub(result) + ``` + """ + if np.asarray(tensorflow_hub_results["detection_boxes"]).shape[0] == 0: + return cls.empty() + + boxes = tensorflow_hub_results["detection_boxes"].numpy() + + # convert boxes from normalized to pixel coordinates + boxes[:, [0, 2]] *= image_size[0] + boxes[:, [1, 3]] *= image_size[1] + + # convert ymin, xmin, ymax, xmax, to x[x1, y1, x2, y2 + boxes = np.array( + [ + boxes[:, 1], + boxes[:, 0], + boxes[:, 3], + boxes[:, 2], + ] + ).transpose() + + return cls( + xyxy=boxes, + confidence=tensorflow_hub_results["detection_scores"].numpy(), + class_id=tensorflow_hub_results["detection_class_labels"].numpy(), + ) + @classmethod def from_deepsparse(cls, deepsparse_results) -> Detections: """ From 2335b61d1e0fae8e7f13894438949a2722ff998b Mon Sep 17 00:00:00 2001 From: James Gallagher Date: Fri, 3 Nov 2023 19:48:42 +0000 Subject: [PATCH 2/9] move to use detection_classes for tf2 compat --- supervision/detection/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index aa6a7932..b004357d 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -318,7 +318,7 @@ class Detections: return cls( xyxy=boxes, confidence=tensorflow_hub_results["detection_scores"].numpy(), - class_id=tensorflow_hub_results["detection_class_labels"].numpy(), + class_id=tensorflow_hub_results["detection_classes"].numpy(), ) @classmethod From 417656f17693575033dccc116e6fd952ed07f7cd Mon Sep 17 00:00:00 2001 From: James Gallagher Date: Fri, 3 Nov 2023 20:08:28 +0000 Subject: [PATCH 3/9] upgrade to work with tf2 --- supervision/detection/core.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index b004357d..19ec8a38 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -299,13 +299,12 @@ class Detections: if np.asarray(tensorflow_hub_results["detection_boxes"]).shape[0] == 0: return cls.empty() - boxes = tensorflow_hub_results["detection_boxes"].numpy() + boxes = tensorflow_hub_results["detection_boxes"][0].numpy() - # convert boxes from normalized to pixel coordinates boxes[:, [0, 2]] *= image_size[0] boxes[:, [1, 3]] *= image_size[1] - # convert ymin, xmin, ymax, xmax, to x[x1, y1, x2, y2 + # convert ymin, xmin, ymax, xmax, to x1, y1, x2, y2 boxes = np.array( [ boxes[:, 1], @@ -317,8 +316,8 @@ class Detections: return cls( xyxy=boxes, - confidence=tensorflow_hub_results["detection_scores"].numpy(), - class_id=tensorflow_hub_results["detection_classes"].numpy(), + confidence=tensorflow_hub_results["detection_scores"][0].numpy(), + class_id=tensorflow_hub_results["detection_classes"][0].numpy().astype(int), ) @classmethod From 1fd248f1fffb673173c726fc208f70c290ee3ff5 Mon Sep 17 00:00:00 2001 From: James Gallagher Date: Fri, 3 Nov 2023 20:11:32 +0000 Subject: [PATCH 4/9] upgrade to work with tf2 --- supervision/detection/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 19ec8a38..b640dd8e 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -288,7 +288,7 @@ class Detections: >>> detector = hub.load(module_handle).signatures['default'] - >>> # follow https://www.tensorflow.org/hub/tutorials/object_detection to load an image + >>> # https://www.tensorflow.org/hub/tutorials/object_detection >>> img = ... >>> result = detector(img) From 32bf235b39e40dd3d801cd848bebcfb12eca2add Mon Sep 17 00:00:00 2001 From: James Gallagher Date: Thu, 7 Dec 2023 09:26:40 +0000 Subject: [PATCH 5/9] add type hints, remove comment --- supervision/detection/core.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index b640dd8e..41d808ab 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -265,7 +265,7 @@ class Detections: ) @classmethod - def from_tensorflow_hub(cls, tensorflow_hub_results, image_size) -> Detections: + def from_tensorflow_hub(cls, tensorflow_hub_results: dict, image_size: tuple) -> Detections: """ Creates a Detections instance from a [Tensorflow Hub](https://www.tensorflow.org/hub/tutorials/object_detection) @@ -304,7 +304,6 @@ class Detections: boxes[:, [0, 2]] *= image_size[0] boxes[:, [1, 3]] *= image_size[1] - # convert ymin, xmin, ymax, xmax, to x1, y1, x2, y2 boxes = np.array( [ boxes[:, 1], From 0ab35b1d8818c48517eb2d733635a2fb5dd223ed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:28:03 +0000 Subject: [PATCH 6/9] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/detection/core.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 41d808ab..98ecb3ae 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -265,7 +265,9 @@ class Detections: ) @classmethod - def from_tensorflow_hub(cls, tensorflow_hub_results: dict, image_size: tuple) -> Detections: + def from_tensorflow_hub( + cls, tensorflow_hub_results: dict, image_size: tuple + ) -> Detections: """ Creates a Detections instance from a [Tensorflow Hub](https://www.tensorflow.org/hub/tutorials/object_detection) From 29a2bb726bfa3b7aab96f50b301f7e58a1ec4aa2 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Thu, 7 Dec 2023 12:16:53 +0100 Subject: [PATCH 7/9] Refactor detection conversion method in Supervision Modified the method of converting TensorFlow detection results to match the Detections class within the Supervision package. --- supervision/detection/core.py | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 98ecb3ae..d5520e54 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -265,8 +265,8 @@ class Detections: ) @classmethod - def from_tensorflow_hub( - cls, tensorflow_hub_results: dict, image_size: tuple + def from_tensorflow( + cls, tensorflow_results: dict, resolution_wh: tuple ) -> Detections: """ Creates a Detections instance from a @@ -274,7 +274,7 @@ class Detections: inference result. Args: - tensorflow_hub_results (dict): + tensorflow_results (dict): The output results from Tensorflow Hub. Returns: @@ -283,7 +283,6 @@ class Detections: Example: ```python >>> import tensorflow as tf - >>> from super_gradients.training import models >>> import tensorflow_hub as hub >>> module_handle = "..." @@ -295,30 +294,17 @@ class Detections: >>> result = detector(img) - >>> detections = sv.Detections.from_tensorflow_hub(result) + >>> detections = sv.Detections.from_tensorflow(result) ``` """ - if np.asarray(tensorflow_hub_results["detection_boxes"]).shape[0] == 0: - return cls.empty() - - boxes = tensorflow_hub_results["detection_boxes"][0].numpy() - - boxes[:, [0, 2]] *= image_size[0] - boxes[:, [1, 3]] *= image_size[1] - - boxes = np.array( - [ - boxes[:, 1], - boxes[:, 0], - boxes[:, 3], - boxes[:, 2], - ] - ).transpose() - + 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_hub_results["detection_scores"][0].numpy(), - class_id=tensorflow_hub_results["detection_classes"][0].numpy().astype(int), + confidence=tensorflow_results["detection_scores"][0].numpy(), + class_id=tensorflow_results["detection_classes"][0].numpy().astype(int), ) @classmethod From c43b69911342e549ffe1bbcd48eae8c5367575a0 Mon Sep 17 00:00:00 2001 From: James Gallagher Date: Tue, 12 Dec 2023 11:42:35 +0000 Subject: [PATCH 8/9] update docstring example --- supervision/detection/core.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index d5520e54..dda3b1e2 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -270,7 +270,7 @@ class Detections: ) -> Detections: """ Creates a Detections instance from a - [Tensorflow Hub](https://www.tensorflow.org/hub/tutorials/object_detection) + [Tensorflow Hub](https://www.tensorflow.org/hub/tutorials/tf2_object_detection) inference result. Args: @@ -284,15 +284,16 @@ class Detections: ```python >>> import tensorflow as tf >>> import tensorflow_hub as hub + >>> import numpy as np + >>> import cv2 - >>> module_handle = "..." + >>> module_handle = "https://tfhub.dev/tensorflow/centernet/hourglass_512x512_kpts/1" - >>> detector = hub.load(module_handle).signatures['default'] + >>> model = hub.load(module_handle) - >>> # https://www.tensorflow.org/hub/tutorials/object_detection - >>> img = ... + >>> img = np.array(cv2.imread(SOURCE_IMAGE_PATH)) - >>> result = detector(img) + >>> result = model(img) >>> detections = sv.Detections.from_tensorflow(result) ``` From 63616b95f7f8c2fd0cfa70c7fb5f4d1c282effc6 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Tue, 12 Dec 2023 15:45:03 +0300 Subject: [PATCH 9/9] =?UTF-8?q?fix(format):=20=F0=9F=90=9E=20supervision/d?= =?UTF-8?q?etection/core.py=20excluded=20from=20black=20and=20flake=20chec?= =?UTF-8?q?k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index cd47f273..9bd6c44b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,7 +68,7 @@ mkdocs-material = "^9.1.4" mkdocstrings = {extras = ["python"], version = "^0.20.0"} [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 )/ '''