Merge pull request #573 from roboflow/add-tf-data-loader

Add `sv.Detections.from_tensorflow_hub` data loader.
This commit is contained in:
Piotr Skalski
2023-12-12 13:50:01 +01:00
committed by GitHub
2 changed files with 46 additions and 1 deletions
+2 -1
View File
@@ -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
)/
'''
+44
View File
@@ -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:
"""