From d6103b41bcaaa21c78bad5bba01deaa64ded1267 Mon Sep 17 00:00:00 2001 From: kinaryml Date: Fri, 14 Apr 2023 10:34:03 -0700 Subject: [PATCH 1/3] Updated object_detector_test.py to correctly verify results --- .../test/vision/object_detector_test.py | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/mediapipe/tasks/python/test/vision/object_detector_test.py b/mediapipe/tasks/python/test/vision/object_detector_test.py index 8fdd81c17..7a7390051 100644 --- a/mediapipe/tasks/python/test/vision/object_detector_test.py +++ b/mediapipe/tasks/python/test/vision/object_detector_test.py @@ -113,6 +113,8 @@ _EXPECTED_DETECTION_RESULT = _DetectionResult( ), ] ) +_SCORE_DIFF_TOLERANCE = 1e-6 +_PIXEL_DIFF_TOLERANCE = 5 _ALLOW_LIST = ['cat', 'dog'] _DENY_LIST = ['cat'] _SCORE_THRESHOLD = 0.3 @@ -136,6 +138,32 @@ class ObjectDetectorTest(parameterized.TestCase): os.path.join(_TEST_DATA_DIR, _MODEL_FILE) ) + def _assert_approximately_equals_bounding_box(self, bbox, expected_bbox): + self.assertEqual(bbox.width, expected_bbox.width) + self.assertEqual(bbox.height, expected_bbox.height) + self.assertAlmostEqual( + bbox.origin_x, expected_bbox.origin_x, _PIXEL_DIFF_TOLERANCE + ) + self.assertAlmostEqual( + bbox.origin_y, expected_bbox.origin_y, _PIXEL_DIFF_TOLERANCE + ) + + def _assert_approximately_equals_category(self, category, expected_category): + self.assertEqual(category.category_name, expected_category.category_name) + self.assertAlmostEqual( + category.score, expected_category.score, _SCORE_DIFF_TOLERANCE + ) + + def _assert_approximately_equals_detection_result(self, result, expected_result): + self.assertLen(result.detections, 4) + for i, detection in enumerate(result.detections): + bbox = detection.bounding_box + category = detection.categories[0] + expected_bbox = expected_result.detections[i].bounding_box + expected_category = expected_result.detections[i].categories[0] + self._assert_approximately_equals_bounding_box(bbox, expected_bbox) + self._assert_approximately_equals_category(category, expected_category) + def test_create_from_file_succeeds_with_valid_model_path(self): # Creates with default option and valid model file successfully. with _ObjectDetector.create_from_model_path(self.model_path) as detector: @@ -192,7 +220,9 @@ class ObjectDetectorTest(parameterized.TestCase): # Performs object detection on the input. detection_result = detector.detect(self.test_image) # Comparing results. - self.assertEqual(detection_result, expected_detection_result) + self._assert_approximately_equals_detection_result( + detection_result, expected_detection_result + ) # Closes the detector explicitly when the detector is not used in # a context. detector.close() @@ -405,7 +435,9 @@ class ObjectDetectorTest(parameterized.TestCase): with _ObjectDetector.create_from_options(options) as detector: for timestamp in range(0, 300, 30): detection_result = detector.detect_for_video(self.test_image, timestamp) - self.assertEqual(detection_result, _EXPECTED_DETECTION_RESULT) + self._assert_approximately_equals_detection_result( + detection_result, _EXPECTED_DETECTION_RESULT + ) def test_calling_detect_in_live_stream_mode(self): options = _ObjectDetectorOptions( @@ -454,7 +486,12 @@ class ObjectDetectorTest(parameterized.TestCase): def check_result( result: _DetectionResult, output_image: _Image, timestamp_ms: int ): - self.assertEqual(result, expected_result) + if result.detections: + self._assert_approximately_equals_detection_result( + result, expected_result + ) + else: + self.assertEqual(result, expected_result) self.assertTrue( np.array_equal( output_image.numpy_view(), self.test_image.numpy_view() From e5e8c71390d3c680aa8dbf32b2a27c7c18e36d87 Mon Sep 17 00:00:00 2001 From: Kinar R <42828719+kinaryml@users.noreply.github.com> Date: Sat, 15 Apr 2023 00:29:33 +0530 Subject: [PATCH 2/3] Update object_detector_test.py --- mediapipe/tasks/python/test/vision/object_detector_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediapipe/tasks/python/test/vision/object_detector_test.py b/mediapipe/tasks/python/test/vision/object_detector_test.py index 7a7390051..58b549c12 100644 --- a/mediapipe/tasks/python/test/vision/object_detector_test.py +++ b/mediapipe/tasks/python/test/vision/object_detector_test.py @@ -155,7 +155,7 @@ class ObjectDetectorTest(parameterized.TestCase): ) def _assert_approximately_equals_detection_result(self, result, expected_result): - self.assertLen(result.detections, 4) + self.assertLen(result.detections, len(expected_result.detections)) for i, detection in enumerate(result.detections): bbox = detection.bounding_box category = detection.categories[0] From f1fbcfab265309fce308244d04967cf796022838 Mon Sep 17 00:00:00 2001 From: Kinar R <42828719+kinaryml@users.noreply.github.com> Date: Wed, 19 Apr 2023 12:10:50 +0530 Subject: [PATCH 3/3] Update test method names --- .../python/test/vision/object_detector_test.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mediapipe/tasks/python/test/vision/object_detector_test.py b/mediapipe/tasks/python/test/vision/object_detector_test.py index 58b549c12..b1198a952 100644 --- a/mediapipe/tasks/python/test/vision/object_detector_test.py +++ b/mediapipe/tasks/python/test/vision/object_detector_test.py @@ -138,7 +138,7 @@ class ObjectDetectorTest(parameterized.TestCase): os.path.join(_TEST_DATA_DIR, _MODEL_FILE) ) - def _assert_approximately_equals_bounding_box(self, bbox, expected_bbox): + def _expect_bounding_box_correct(self, bbox, expected_bbox): self.assertEqual(bbox.width, expected_bbox.width) self.assertEqual(bbox.height, expected_bbox.height) self.assertAlmostEqual( @@ -148,21 +148,21 @@ class ObjectDetectorTest(parameterized.TestCase): bbox.origin_y, expected_bbox.origin_y, _PIXEL_DIFF_TOLERANCE ) - def _assert_approximately_equals_category(self, category, expected_category): + def _expect_category_correct(self, category, expected_category): self.assertEqual(category.category_name, expected_category.category_name) self.assertAlmostEqual( category.score, expected_category.score, _SCORE_DIFF_TOLERANCE ) - def _assert_approximately_equals_detection_result(self, result, expected_result): + def _expect_detection_result_correct(self, result, expected_result): self.assertLen(result.detections, len(expected_result.detections)) for i, detection in enumerate(result.detections): bbox = detection.bounding_box category = detection.categories[0] expected_bbox = expected_result.detections[i].bounding_box expected_category = expected_result.detections[i].categories[0] - self._assert_approximately_equals_bounding_box(bbox, expected_bbox) - self._assert_approximately_equals_category(category, expected_category) + self._expect_bounding_box_correct(bbox, expected_bbox) + self._expect_category_correct(category, expected_category) def test_create_from_file_succeeds_with_valid_model_path(self): # Creates with default option and valid model file successfully. @@ -220,7 +220,7 @@ class ObjectDetectorTest(parameterized.TestCase): # Performs object detection on the input. detection_result = detector.detect(self.test_image) # Comparing results. - self._assert_approximately_equals_detection_result( + self._expect_detection_result_correct( detection_result, expected_detection_result ) # Closes the detector explicitly when the detector is not used in @@ -435,7 +435,7 @@ class ObjectDetectorTest(parameterized.TestCase): with _ObjectDetector.create_from_options(options) as detector: for timestamp in range(0, 300, 30): detection_result = detector.detect_for_video(self.test_image, timestamp) - self._assert_approximately_equals_detection_result( + self._expect_detection_result_correct( detection_result, _EXPECTED_DETECTION_RESULT ) @@ -487,7 +487,7 @@ class ObjectDetectorTest(parameterized.TestCase): result: _DetectionResult, output_image: _Image, timestamp_ms: int ): if result.detections: - self._assert_approximately_equals_detection_result( + self._expect_detection_result_correct( result, expected_result ) else: