From 666427df08f69d67dba6116a72e58d6827bec4c7 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Sat, 2 Sep 2023 20:36:00 +0300 Subject: [PATCH 1/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20empty=20check=20for?= =?UTF-8?q?=20xyxy=20results=20for=20prevent=20ValueError=20and=20return?= =?UTF-8?q?=20as=20empty=20detection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- supervision/detection/core.py | 56 +++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 5e63e346..e2487e20 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -178,6 +178,10 @@ class Detections: ``` """ yolov5_detections_predictions = yolov5_results.pred[0].cpu().cpu().numpy() + + if np.asarray(yolov5_detections_predictions[:, :4]).shape[0] == 0: + return cls.empty() + return cls( xyxy=yolov5_detections_predictions[:, :4], confidence=yolov5_detections_predictions[:, 4], @@ -216,6 +220,10 @@ class Detections: >>> detections = sv.Detections.from_yolov8(result) ``` """ + + if np.asarray(yolov8_results.boxes.xyxy.cpu().numpy()).shape[0] == 0: + return cls.empty() + return cls( xyxy=yolov8_results.boxes.xyxy.cpu().numpy(), confidence=yolov8_results.boxes.conf.cpu().numpy(), @@ -255,6 +263,10 @@ class Detections: >>> detections = sv.Detections.from_ultralytics(result) ``` """ + + if np.asarray(ultralytics_results.boxes.xyxy.cpu().numpy()).shape[0] == 0: + return cls.empty() + return cls( xyxy=ultralytics_results.boxes.xyxy.cpu().numpy(), confidence=ultralytics_results.boxes.conf.cpu().numpy(), @@ -293,6 +305,9 @@ class Detections: >>> detections = sv.Detections.from_yolo_nas(result) ``` """ + if np.asarray(yolo_nas_results.bboxes_xyxy).shape[0] == 0: + return cls.empty() + return cls( xyxy=yolo_nas_results.prediction.bboxes_xyxy, confidence=yolo_nas_results.prediction.confidence, @@ -329,14 +344,12 @@ class Detections: """ if np.asarray(deepsparse_results.boxes[0]).shape[0] == 0: return cls.empty() - else: - return cls( - xyxy=np.array(deepsparse_results.boxes[0]), - confidence=np.array(deepsparse_results.scores[0]), - class_id=np.array(deepsparse_results.labels[0]) - .astype(float) - .astype(int), - ) + + return cls( + xyxy=np.array(deepsparse_results.boxes[0]), + confidence=np.array(deepsparse_results.scores[0]), + class_id=np.array(deepsparse_results.labels[0]).astype(float).astype(int), + ) @classmethod def from_mmdetection(cls, mmdet_results) -> Detections: @@ -364,6 +377,10 @@ class Detections: >>> detections = sv.Detections.from_mmdet(mmdet_result) ``` """ + + if np.asarray(mmdet_results.pred_instances.bboxes.cpu().numpy()).shape[0] == 0: + return cls.empty() + return cls( xyxy=mmdet_results.pred_instances.bboxes.cpu().numpy(), confidence=mmdet_results.pred_instances.scores.cpu().numpy(), @@ -379,6 +396,10 @@ class Detections: Returns: Detections: A new Detections object. """ + + if np.asarray(transformers_results["boxes"].cpu().numpy()).shape[0] == 0: + return cls.empty() + return cls( xyxy=transformers_results["boxes"].cpu().numpy(), confidence=transformers_results["scores"].cpu().numpy(), @@ -415,6 +436,15 @@ class Detections: >>> detections = sv.Detections.from_detectron2(result) ``` """ + + if ( + np.asarray( + detectron2_results["instances"].pred_boxes.tensor.cpu().numpy() + ).shape[0] + == 0 + ): + return cls.empty() + return cls( xyxy=detectron2_results["instances"].pred_boxes.tensor.cpu().numpy(), confidence=detectron2_results["instances"].scores.cpu().numpy(), @@ -465,7 +495,11 @@ class Detections: xyxy, confidence, class_id, masks = process_roboflow_result( roboflow_result=roboflow_result, class_list=class_list ) - return Detections( + + if np.asarray(xyxy).shape[0] == 0: + return cls.empty() + + return cls( xyxy=xyxy, confidence=confidence, class_id=class_id, @@ -542,6 +576,10 @@ class Detections: >>> detections = sv.Detections.from_paddledet(paddledet_result) ``` """ + + if np.asarray(paddledet_result["bbox"][:, 2:6]).shape[0] == 0: + return cls.empty() + return cls( xyxy=paddledet_result["bbox"][:, 2:6], confidence=paddledet_result["bbox"][:, 1], From 5dae02e93e20aa5a04a2b3db3bf3363773f5d141 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Sat, 2 Sep 2023 20:40:35 +0300 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20deepsparse=20model=5F?= =?UTF-8?q?stub=20to=20model=5Fpath=20docs=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- 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 e2487e20..0e58e312 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -335,7 +335,7 @@ class Detections: >>> yolo_pipeline = Pipeline.create( ... task="yolo", - ... model_stub = "zoo:cv/detection/yolov5-l/pytorch/" \ + ... model_path = "zoo:cv/detection/yolov5-l/pytorch/" \ ... "ultralytics/coco/pruned80_quant-none" >>> pipeline_outputs = yolo_pipeline(SOURCE_IMAGE_PATH, ... iou_thres=0.6, conf_thres=0.001) From 775a3f279550fe8972d45c197031e89d11562d09 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Sat, 2 Sep 2023 22:43:27 +0300 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20cpu().numpy()=20conve?= =?UTF-8?q?rts=20not=20require=20empty=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- supervision/detection/core.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 0e58e312..c5af5a75 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -179,9 +179,6 @@ class Detections: """ yolov5_detections_predictions = yolov5_results.pred[0].cpu().cpu().numpy() - if np.asarray(yolov5_detections_predictions[:, :4]).shape[0] == 0: - return cls.empty() - return cls( xyxy=yolov5_detections_predictions[:, :4], confidence=yolov5_detections_predictions[:, 4], @@ -221,9 +218,6 @@ class Detections: ``` """ - if np.asarray(yolov8_results.boxes.xyxy.cpu().numpy()).shape[0] == 0: - return cls.empty() - return cls( xyxy=yolov8_results.boxes.xyxy.cpu().numpy(), confidence=yolov8_results.boxes.conf.cpu().numpy(), @@ -264,9 +258,6 @@ class Detections: ``` """ - if np.asarray(ultralytics_results.boxes.xyxy.cpu().numpy()).shape[0] == 0: - return cls.empty() - return cls( xyxy=ultralytics_results.boxes.xyxy.cpu().numpy(), confidence=ultralytics_results.boxes.conf.cpu().numpy(), @@ -378,9 +369,6 @@ class Detections: ``` """ - if np.asarray(mmdet_results.pred_instances.bboxes.cpu().numpy()).shape[0] == 0: - return cls.empty() - return cls( xyxy=mmdet_results.pred_instances.bboxes.cpu().numpy(), confidence=mmdet_results.pred_instances.scores.cpu().numpy(), @@ -397,9 +385,6 @@ class Detections: Detections: A new Detections object. """ - if np.asarray(transformers_results["boxes"].cpu().numpy()).shape[0] == 0: - return cls.empty() - return cls( xyxy=transformers_results["boxes"].cpu().numpy(), confidence=transformers_results["scores"].cpu().numpy(), @@ -437,14 +422,6 @@ class Detections: ``` """ - if ( - np.asarray( - detectron2_results["instances"].pred_boxes.tensor.cpu().numpy() - ).shape[0] - == 0 - ): - return cls.empty() - return cls( xyxy=detectron2_results["instances"].pred_boxes.tensor.cpu().numpy(), confidence=detectron2_results["instances"].scores.cpu().numpy(), From 7b2bcf00ad372298e7ae1319b61b256a62b0708b Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Sat, 2 Sep 2023 22:45:29 +0300 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20sam=20xyxy=20check=20?= =?UTF-8?q?added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- supervision/detection/core.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index c5af5a75..95426264 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -518,8 +518,13 @@ class Detections: xywh = np.array([mask["bbox"] for mask in sorted_generated_masks]) mask = np.array([mask["segmentation"] for mask in sorted_generated_masks]) + - return Detections(xyxy=xywh_to_xyxy(boxes_xywh=xywh), mask=mask) + if np.asarray(xywh).shape[0] == 0: + return cls.empty() + + xyxy = xywh_to_xyxy(boxes_xywh=xywh) + return cls(xyxy=xyxy, mask=mask) @classmethod def from_paddledet(cls, paddledet_result) -> Detections: From bbd9a955f8e103457c26e60e90c4d50b5b81558b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 2 Sep 2023 19:45:42 +0000 Subject: [PATCH 5/5] =?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 | 1 - 1 file changed, 1 deletion(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 95426264..e07b1020 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -518,7 +518,6 @@ class Detections: xywh = np.array([mask["bbox"] for mask in sorted_generated_masks]) mask = np.array([mask["segmentation"] for mask in sorted_generated_masks]) - if np.asarray(xywh).shape[0] == 0: return cls.empty()