Implement Image.create_from_file and update the object_detector_test.py file accordingly.
PiperOrigin-RevId: 477682930
This commit is contained in:
committed by
Copybara-Service
parent
80fd47b820
commit
554e2a9d69
@@ -16,7 +16,6 @@
|
||||
import os
|
||||
|
||||
from absl import flags
|
||||
import cv2
|
||||
|
||||
from mediapipe.python._framework_bindings import image as image_module
|
||||
from mediapipe.python._framework_bindings import image_frame as image_frame_module
|
||||
@@ -44,12 +43,3 @@ def get_test_data_path(file_or_dirname: str) -> str:
|
||||
if f.endswith(file_or_dirname):
|
||||
return os.path.join(directory, f)
|
||||
raise ValueError("No %s in test directory" % file_or_dirname)
|
||||
|
||||
|
||||
# TODO: Implement image util module to read image data from file.
|
||||
def read_test_image(image_file: str) -> _Image:
|
||||
"""Reads a MediaPipe Image from the image file."""
|
||||
image_data = cv2.imread(image_file)
|
||||
if image_data.shape[2] != _RGB_CHANNELS:
|
||||
raise ValueError("Input image must contain three channel rgb data.")
|
||||
return _Image(_ImageFormat.SRGB, cv2.cvtColor(image_data, cv2.COLOR_BGR2RGB))
|
||||
|
||||
@@ -44,7 +44,7 @@ _IMAGE_FILE = 'cats_and_dogs.jpg'
|
||||
_EXPECTED_DETECTION_RESULT = _DetectionResult(detections=[
|
||||
_Detection(
|
||||
bounding_box=_BoundingBox(
|
||||
origin_x=608, origin_y=164, width=381, height=432),
|
||||
origin_x=608, origin_y=161, width=381, height=439),
|
||||
categories=[
|
||||
_Category(
|
||||
index=None,
|
||||
@@ -64,7 +64,7 @@ _EXPECTED_DETECTION_RESULT = _DetectionResult(detections=[
|
||||
]),
|
||||
_Detection(
|
||||
bounding_box=_BoundingBox(
|
||||
origin_x=257, origin_y=394, width=173, height=202),
|
||||
origin_x=256, origin_y=395, width=173, height=202),
|
||||
categories=[
|
||||
_Category(
|
||||
index=None,
|
||||
@@ -74,7 +74,7 @@ _EXPECTED_DETECTION_RESULT = _DetectionResult(detections=[
|
||||
]),
|
||||
_Detection(
|
||||
bounding_box=_BoundingBox(
|
||||
origin_x=362, origin_y=195, width=325, height=412),
|
||||
origin_x=362, origin_y=191, width=325, height=419),
|
||||
categories=[
|
||||
_Category(
|
||||
index=None,
|
||||
@@ -98,7 +98,7 @@ class ObjectDetectorTest(parameterized.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.test_image = test_util.read_test_image(
|
||||
self.test_image = _Image.create_from_file(
|
||||
test_util.get_test_data_path(_IMAGE_FILE))
|
||||
self.model_path = test_util.get_test_data_path(_MODEL_FILE)
|
||||
|
||||
@@ -153,9 +153,9 @@ class ObjectDetectorTest(parameterized.TestCase):
|
||||
detector = _ObjectDetector.create_from_options(options)
|
||||
|
||||
# Performs object detection on the input.
|
||||
image_result = detector.detect(self.test_image)
|
||||
detection_result = detector.detect(self.test_image)
|
||||
# Comparing results.
|
||||
self.assertEqual(image_result, expected_detection_result)
|
||||
self.assertEqual(detection_result, expected_detection_result)
|
||||
# Closes the detector explicitly when the detector is not used in
|
||||
# a context.
|
||||
detector.close()
|
||||
@@ -179,9 +179,9 @@ class ObjectDetectorTest(parameterized.TestCase):
|
||||
base_options=base_options, max_results=max_results)
|
||||
with _ObjectDetector.create_from_options(options) as detector:
|
||||
# Performs object detection on the input.
|
||||
image_result = detector.detect(self.test_image)
|
||||
detection_result = detector.detect(self.test_image)
|
||||
# Comparing results.
|
||||
self.assertEqual(image_result, expected_detection_result)
|
||||
self.assertEqual(detection_result, expected_detection_result)
|
||||
|
||||
def test_score_threshold_option(self):
|
||||
options = _ObjectDetectorOptions(
|
||||
@@ -189,8 +189,8 @@ class ObjectDetectorTest(parameterized.TestCase):
|
||||
score_threshold=_SCORE_THRESHOLD)
|
||||
with _ObjectDetector.create_from_options(options) as detector:
|
||||
# Performs object detection on the input.
|
||||
image_result = detector.detect(self.test_image)
|
||||
detections = image_result.detections
|
||||
detection_result = detector.detect(self.test_image)
|
||||
detections = detection_result.detections
|
||||
|
||||
for detection in detections:
|
||||
score = detection.categories[0].score
|
||||
@@ -204,8 +204,8 @@ class ObjectDetectorTest(parameterized.TestCase):
|
||||
max_results=_MAX_RESULTS)
|
||||
with _ObjectDetector.create_from_options(options) as detector:
|
||||
# Performs object detection on the input.
|
||||
image_result = detector.detect(self.test_image)
|
||||
detections = image_result.detections
|
||||
detection_result = detector.detect(self.test_image)
|
||||
detections = detection_result.detections
|
||||
|
||||
self.assertLessEqual(
|
||||
len(detections), _MAX_RESULTS, 'Too many results returned.')
|
||||
@@ -216,8 +216,8 @@ class ObjectDetectorTest(parameterized.TestCase):
|
||||
category_allowlist=_ALLOW_LIST)
|
||||
with _ObjectDetector.create_from_options(options) as detector:
|
||||
# Performs object detection on the input.
|
||||
image_result = detector.detect(self.test_image)
|
||||
detections = image_result.detections
|
||||
detection_result = detector.detect(self.test_image)
|
||||
detections = detection_result.detections
|
||||
|
||||
for detection in detections:
|
||||
label = detection.categories[0].category_name
|
||||
@@ -230,8 +230,8 @@ class ObjectDetectorTest(parameterized.TestCase):
|
||||
category_denylist=_DENY_LIST)
|
||||
with _ObjectDetector.create_from_options(options) as detector:
|
||||
# Performs object detection on the input.
|
||||
image_result = detector.detect(self.test_image)
|
||||
detections = image_result.detections
|
||||
detection_result = detector.detect(self.test_image)
|
||||
detections = detection_result.detections
|
||||
|
||||
for detection in detections:
|
||||
label = detection.categories[0].category_name
|
||||
@@ -257,8 +257,8 @@ class ObjectDetectorTest(parameterized.TestCase):
|
||||
score_threshold=1)
|
||||
with _ObjectDetector.create_from_options(options) as detector:
|
||||
# Performs object detection on the input.
|
||||
image_result = detector.detect(self.test_image)
|
||||
self.assertEmpty(image_result.detections)
|
||||
detection_result = detector.detect(self.test_image)
|
||||
self.assertEmpty(detection_result.detections)
|
||||
|
||||
def test_missing_result_callback(self):
|
||||
options = _ObjectDetectorOptions(
|
||||
|
||||
Reference in New Issue
Block a user