Extracted common test helper functions out from the unittest into a sharable

library. Also migrated away from OpenCVX.

PiperOrigin-RevId: 490074410
This commit is contained in:
MediaPipe Team 2022-11-21 14:37:42 -08:00 committed by Copybara-Service
parent 652423a23d
commit adddf2c2ab
5 changed files with 166 additions and 112 deletions

View File

@ -30,6 +30,7 @@ exports_files(
glob(["testdata/image_to_tensor/*"]), glob(["testdata/image_to_tensor/*"]),
visibility = [ visibility = [
"//mediapipe/calculators/image:__subpackages__", "//mediapipe/calculators/image:__subpackages__",
"//mediapipe/util:__subpackages__",
], ],
) )
@ -1133,6 +1134,7 @@ cc_test(
"//mediapipe/framework/port:opencv_imgcodecs", "//mediapipe/framework/port:opencv_imgcodecs",
"//mediapipe/framework/port:opencv_imgproc", "//mediapipe/framework/port:opencv_imgproc",
"//mediapipe/framework/port:parse_text_proto", "//mediapipe/framework/port:parse_text_proto",
"//mediapipe/util:image_test_utils",
"@com_google_absl//absl/flags:flag", "@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/memory", "@com_google_absl//absl/memory",
"@com_google_absl//absl/strings", "@com_google_absl//absl/strings",

View File

@ -36,29 +36,17 @@
#include "mediapipe/framework/port/opencv_imgproc_inc.h" #include "mediapipe/framework/port/opencv_imgproc_inc.h"
#include "mediapipe/framework/port/parse_text_proto.h" #include "mediapipe/framework/port/parse_text_proto.h"
#include "mediapipe/framework/port/status_matchers.h" #include "mediapipe/framework/port/status_matchers.h"
#include "mediapipe/util/image_test_utils.h"
namespace mediapipe { namespace mediapipe {
namespace { namespace {
cv::Mat GetRgb(absl::string_view path) { constexpr char kTestDataDir[] =
cv::Mat bgr = cv::imread(file::JoinPath("./", path)); "/mediapipe/calculators/tensor/testdata/"
cv::Mat rgb; "image_to_tensor/";
cv::cvtColor(bgr, rgb, cv::COLOR_BGR2RGB);
return rgb;
}
cv::Mat GetRgba(absl::string_view path) { std::string GetFilePath(absl::string_view filename) {
cv::Mat bgr = cv::imread(file::JoinPath("./", path)); return file::JoinPath("./", kTestDataDir, filename);
cv::Mat rgb;
cv::cvtColor(bgr, rgb, cv::COLOR_BGR2RGBA);
return rgb;
}
cv::Mat GetGray(absl::string_view path) {
cv::Mat bgr = cv::imread(file::JoinPath("./", path));
cv::Mat gray;
cv::cvtColor(bgr, gray, cv::COLOR_BGR2GRAY);
return gray;
} }
// Image to tensor test template. // Image to tensor test template.
@ -259,15 +247,12 @@ TEST(ImageToTensorCalculatorTest, MediumSubRectKeepAspect) {
roi.set_width(0.5f); roi.set_width(0.5f);
roi.set_height(0.5f); roi.set_height(0.5f);
roi.set_rotation(0); roi.set_rotation(0);
RunTest( RunTest(GetRgb(GetFilePath("input.jpg")),
GetRgb("/mediapipe/calculators/" GetRgb(GetFilePath("medium_sub_rect_keep_aspect.png")),
"tensor/testdata/image_to_tensor/input.jpg"), /*float_ranges=*/{{0.0f, 1.0f}},
GetRgb("/mediapipe/calculators/" /*int_ranges=*/{{0, 255}, {-128, 127}},
"tensor/testdata/image_to_tensor/medium_sub_rect_keep_aspect.png"), /*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/true,
/*float_ranges=*/{{0.0f, 1.0f}}, /*border mode*/ {}, roi);
/*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/true,
/*border mode*/ {}, roi);
} }
TEST(ImageToTensorCalculatorTest, MediumSubRectKeepAspectBorderZero) { TEST(ImageToTensorCalculatorTest, MediumSubRectKeepAspectBorderZero) {
@ -277,11 +262,8 @@ TEST(ImageToTensorCalculatorTest, MediumSubRectKeepAspectBorderZero) {
roi.set_width(0.5f); roi.set_width(0.5f);
roi.set_height(0.5f); roi.set_height(0.5f);
roi.set_rotation(0); roi.set_rotation(0);
RunTest(GetRgb("/mediapipe/calculators/" RunTest(GetRgb(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath("medium_sub_rect_keep_aspect_border_zero.png")),
GetRgb("/mediapipe/calculators/"
"tensor/testdata/image_to_tensor/"
"medium_sub_rect_keep_aspect_border_zero.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}, {-128, 127}}, /*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/true, /*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/true,
@ -295,11 +277,8 @@ TEST(ImageToTensorCalculatorTest, MediumSubRectKeepAspectWithRotation) {
roi.set_width(0.5f); roi.set_width(0.5f);
roi.set_height(0.5f); roi.set_height(0.5f);
roi.set_rotation(M_PI * 90.0f / 180.0f); roi.set_rotation(M_PI * 90.0f / 180.0f);
RunTest(GetRgb("/mediapipe/calculators/" RunTest(GetRgb(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath("medium_sub_rect_keep_aspect_with_rotation.png")),
GetRgb("/mediapipe/calculators/"
"tensor/testdata/image_to_tensor/"
"medium_sub_rect_keep_aspect_with_rotation.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}}, /*int_ranges=*/{{0, 255}},
/*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/true, /*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/true,
@ -314,11 +293,9 @@ TEST(ImageToTensorCalculatorTest,
roi.set_width(0.5f); roi.set_width(0.5f);
roi.set_height(0.5f); roi.set_height(0.5f);
roi.set_rotation(M_PI * 90.0f / 180.0f); roi.set_rotation(M_PI * 90.0f / 180.0f);
RunTest(GetRgb("/mediapipe/calculators/" RunTest(GetRgb(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath(
GetRgb("/mediapipe/calculators/" "medium_sub_rect_keep_aspect_with_rotation_border_zero.png")),
"tensor/testdata/image_to_tensor/"
"medium_sub_rect_keep_aspect_with_rotation_border_zero.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}, {-128, 127}}, /*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/true, /*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/true,
@ -332,16 +309,12 @@ TEST(ImageToTensorCalculatorTest, MediumSubRectWithRotation) {
roi.set_width(0.5f); roi.set_width(0.5f);
roi.set_height(0.5f); roi.set_height(0.5f);
roi.set_rotation(M_PI * -45.0f / 180.0f); roi.set_rotation(M_PI * -45.0f / 180.0f);
RunTest( RunTest(GetRgb(GetFilePath("input.jpg")),
GetRgb("/mediapipe/calculators/" GetRgb(GetFilePath("medium_sub_rect_with_rotation.png")),
"tensor/testdata/image_to_tensor/input.jpg"), /*float_ranges=*/{{-1.0f, 1.0f}},
GetRgb( /*int_ranges=*/{{0, 255}, {-128, 127}},
"/mediapipe/calculators/" /*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/false,
"tensor/testdata/image_to_tensor/medium_sub_rect_with_rotation.png"), BorderMode::kReplicate, roi);
/*float_ranges=*/{{-1.0f, 1.0f}},
/*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/false,
BorderMode::kReplicate, roi);
} }
TEST(ImageToTensorCalculatorTest, MediumSubRectWithRotationBorderZero) { TEST(ImageToTensorCalculatorTest, MediumSubRectWithRotationBorderZero) {
@ -351,11 +324,8 @@ TEST(ImageToTensorCalculatorTest, MediumSubRectWithRotationBorderZero) {
roi.set_width(0.5f); roi.set_width(0.5f);
roi.set_height(0.5f); roi.set_height(0.5f);
roi.set_rotation(M_PI * -45.0f / 180.0f); roi.set_rotation(M_PI * -45.0f / 180.0f);
RunTest(GetRgb("/mediapipe/calculators/" RunTest(GetRgb(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath("medium_sub_rect_with_rotation_border_zero.png")),
GetRgb("/mediapipe/calculators/"
"tensor/testdata/image_to_tensor/"
"medium_sub_rect_with_rotation_border_zero.png"),
/*float_ranges=*/{{-1.0f, 1.0f}}, /*float_ranges=*/{{-1.0f, 1.0f}},
/*int_ranges=*/{{0, 255}, {-128, 127}}, /*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/false, /*tensor_width=*/256, /*tensor_height=*/256, /*keep_aspect=*/false,
@ -369,10 +339,8 @@ TEST(ImageToTensorCalculatorTest, LargeSubRect) {
roi.set_width(1.5f); roi.set_width(1.5f);
roi.set_height(1.1f); roi.set_height(1.1f);
roi.set_rotation(0); roi.set_rotation(0);
RunTest(GetRgb("/mediapipe/calculators/" RunTest(GetRgb(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath("large_sub_rect.png")),
GetRgb("/mediapipe/calculators/"
"tensor/testdata/image_to_tensor/large_sub_rect.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}}, /*int_ranges=*/{{0, 255}},
/*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/false, /*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/false,
@ -386,15 +354,12 @@ TEST(ImageToTensorCalculatorTest, LargeSubRectBorderZero) {
roi.set_width(1.5f); roi.set_width(1.5f);
roi.set_height(1.1f); roi.set_height(1.1f);
roi.set_rotation(0); roi.set_rotation(0);
RunTest( RunTest(GetRgb(GetFilePath("input.jpg")),
GetRgb("/mediapipe/calculators/" GetRgb(GetFilePath("large_sub_rect_border_zero.png")),
"tensor/testdata/image_to_tensor/input.jpg"), /*float_ranges=*/{{0.0f, 1.0f}},
GetRgb("/mediapipe/calculators/" /*int_ranges=*/{{0, 255}, {-128, 127}},
"tensor/testdata/image_to_tensor/large_sub_rect_border_zero.png"), /*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/false,
/*float_ranges=*/{{0.0f, 1.0f}}, BorderMode::kZero, roi);
/*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/false,
BorderMode::kZero, roi);
} }
TEST(ImageToTensorCalculatorTest, LargeSubRectKeepAspect) { TEST(ImageToTensorCalculatorTest, LargeSubRectKeepAspect) {
@ -404,15 +369,12 @@ TEST(ImageToTensorCalculatorTest, LargeSubRectKeepAspect) {
roi.set_width(1.5f); roi.set_width(1.5f);
roi.set_height(1.1f); roi.set_height(1.1f);
roi.set_rotation(0); roi.set_rotation(0);
RunTest( RunTest(GetRgb(GetFilePath("input.jpg")),
GetRgb("/mediapipe/calculators/" GetRgb(GetFilePath("large_sub_rect_keep_aspect.png")),
"tensor/testdata/image_to_tensor/input.jpg"), /*float_ranges=*/{{0.0f, 1.0f}},
GetRgb("/mediapipe/calculators/" /*int_ranges=*/{{0, 255}, {-128, 127}},
"tensor/testdata/image_to_tensor/large_sub_rect_keep_aspect.png"), /*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true,
/*float_ranges=*/{{0.0f, 1.0f}}, BorderMode::kReplicate, roi);
/*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true,
BorderMode::kReplicate, roi);
} }
TEST(ImageToTensorCalculatorTest, LargeSubRectKeepAspectBorderZero) { TEST(ImageToTensorCalculatorTest, LargeSubRectKeepAspectBorderZero) {
@ -422,11 +384,8 @@ TEST(ImageToTensorCalculatorTest, LargeSubRectKeepAspectBorderZero) {
roi.set_width(1.5f); roi.set_width(1.5f);
roi.set_height(1.1f); roi.set_height(1.1f);
roi.set_rotation(0); roi.set_rotation(0);
RunTest(GetRgb("/mediapipe/calculators/" RunTest(GetRgb(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath("large_sub_rect_keep_aspect_border_zero.png")),
GetRgb("/mediapipe/calculators/"
"tensor/testdata/image_to_tensor/"
"large_sub_rect_keep_aspect_border_zero.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}, {-128, 127}}, /*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true, /*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true,
@ -440,11 +399,8 @@ TEST(ImageToTensorCalculatorTest, LargeSubRectKeepAspectWithRotation) {
roi.set_width(1.5f); roi.set_width(1.5f);
roi.set_height(1.1f); roi.set_height(1.1f);
roi.set_rotation(M_PI * -15.0f / 180.0f); roi.set_rotation(M_PI * -15.0f / 180.0f);
RunTest(GetRgba("/mediapipe/calculators/" RunTest(GetRgba(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath("large_sub_rect_keep_aspect_with_rotation.png")),
GetRgb("/mediapipe/calculators/"
"tensor/testdata/image_to_tensor/"
"large_sub_rect_keep_aspect_with_rotation.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}, {-128, 127}}, /*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true, /*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true,
@ -458,11 +414,8 @@ TEST(ImageToTensorCalculatorTest, LargeSubRectKeepAspectWithRotationGray) {
roi.set_width(1.5f); roi.set_width(1.5f);
roi.set_height(1.1f); roi.set_height(1.1f);
roi.set_rotation(M_PI * -15.0f / 180.0f); roi.set_rotation(M_PI * -15.0f / 180.0f);
RunTest(GetGray("/mediapipe/calculators/" RunTest(GetGray(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetGray(GetFilePath("large_sub_rect_keep_aspect_with_rotation.png")),
GetGray("/mediapipe/calculators/"
"tensor/testdata/image_to_tensor/"
"large_sub_rect_keep_aspect_with_rotation.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}, {-128, 127}}, /*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true, /*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true,
@ -477,11 +430,9 @@ TEST(ImageToTensorCalculatorTest,
roi.set_width(1.5f); roi.set_width(1.5f);
roi.set_height(1.1f); roi.set_height(1.1f);
roi.set_rotation(M_PI * -15.0f / 180.0f); roi.set_rotation(M_PI * -15.0f / 180.0f);
RunTest(GetRgba("/mediapipe/calculators/" RunTest(GetRgba(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath(
GetRgb("/mediapipe/calculators/" "large_sub_rect_keep_aspect_with_rotation_border_zero.png")),
"tensor/testdata/image_to_tensor/"
"large_sub_rect_keep_aspect_with_rotation_border_zero.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}}, /*int_ranges=*/{{0, 255}},
/*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true, /*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true,
@ -496,11 +447,9 @@ TEST(ImageToTensorCalculatorTest,
roi.set_width(1.5f); roi.set_width(1.5f);
roi.set_height(1.1f); roi.set_height(1.1f);
roi.set_rotation(M_PI * -15.0f / 180.0f); roi.set_rotation(M_PI * -15.0f / 180.0f);
RunTest(GetGray("/mediapipe/calculators/" RunTest(GetGray(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetGray(GetFilePath(
GetGray("/mediapipe/calculators/" "large_sub_rect_keep_aspect_with_rotation_border_zero.png")),
"tensor/testdata/image_to_tensor/"
"large_sub_rect_keep_aspect_with_rotation_border_zero.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}}, /*int_ranges=*/{{0, 255}},
/*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true, /*tensor_width=*/128, /*tensor_height=*/128, /*keep_aspect=*/true,
@ -514,10 +463,8 @@ TEST(ImageToTensorCalculatorTest, NoOpExceptRange) {
roi.set_width(1.0f); roi.set_width(1.0f);
roi.set_height(1.0f); roi.set_height(1.0f);
roi.set_rotation(0); roi.set_rotation(0);
RunTest(GetRgba("/mediapipe/calculators/" RunTest(GetRgba(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath("noop_except_range.png")),
GetRgb("/mediapipe/calculators/"
"tensor/testdata/image_to_tensor/noop_except_range.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}, {-128, 127}}, /*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/64, /*tensor_height=*/128, /*keep_aspect=*/true, /*tensor_width=*/64, /*tensor_height=*/128, /*keep_aspect=*/true,
@ -531,10 +478,8 @@ TEST(ImageToTensorCalculatorTest, NoOpExceptRangeBorderZero) {
roi.set_width(1.0f); roi.set_width(1.0f);
roi.set_height(1.0f); roi.set_height(1.0f);
roi.set_rotation(0); roi.set_rotation(0);
RunTest(GetRgba("/mediapipe/calculators/" RunTest(GetRgba(GetFilePath("input.jpg")),
"tensor/testdata/image_to_tensor/input.jpg"), GetRgb(GetFilePath("noop_except_range.png")),
GetRgb("/mediapipe/calculators/"
"tensor/testdata/image_to_tensor/noop_except_range.png"),
/*float_ranges=*/{{0.0f, 1.0f}}, /*float_ranges=*/{{0.0f, 1.0f}},
/*int_ranges=*/{{0, 255}, {-128, 127}}, /*int_ranges=*/{{0, 255}, {-128, 127}},
/*tensor_width=*/64, /*tensor_height=*/128, /*keep_aspect=*/true, /*tensor_width=*/64, /*tensor_height=*/128, /*keep_aspect=*/true,

View File

@ -368,3 +368,21 @@ cc_test(
"//mediapipe/framework/port:gtest_main", "//mediapipe/framework/port:gtest_main",
], ],
) )
cc_library(
name = "image_test_utils",
testonly = 1,
srcs = ["image_test_utils.cc"],
hdrs = ["image_test_utils.h"],
visibility = ["//visibility:public"],
deps = [
"//mediapipe/framework:packet",
"//mediapipe/framework:timestamp",
"//mediapipe/framework/formats:image",
"//mediapipe/framework/formats:image_frame",
"//mediapipe/framework/formats:image_frame_opencv",
"//mediapipe/framework/port:opencv_core",
"//mediapipe/framework/port:opencv_imgcodecs",
"//mediapipe/framework/port:opencv_imgproc",
],
)

View File

@ -0,0 +1,57 @@
#include "mediapipe/util/image_test_utils.h"
#include "mediapipe/framework/formats/image_frame.h"
#include "mediapipe/framework/formats/image_frame_opencv.h"
#include "mediapipe/framework/port/opencv_core_inc.h"
#include "mediapipe/framework/port/opencv_imgcodecs_inc.h"
#include "mediapipe/framework/port/opencv_imgproc_inc.h"
#include "mediapipe/framework/timestamp.h"
namespace mediapipe {
cv::Mat GetRgb(const std::string& path) {
cv::Mat bgr = cv::imread(path);
cv::Mat rgb;
cv::cvtColor(bgr, rgb, cv::COLOR_BGR2RGB);
return rgb;
}
cv::Mat GetRgba(const std::string& path) {
cv::Mat bgr = cv::imread(path);
cv::Mat rgb;
cv::cvtColor(bgr, rgb, cv::COLOR_BGR2RGBA);
return rgb;
}
cv::Mat GetGray(const std::string& path) {
cv::Mat bgr = cv::imread(path);
cv::Mat gray;
cv::cvtColor(bgr, gray, cv::COLOR_BGR2GRAY);
return gray;
}
mediapipe::ImageFormat::Format GetImageFormat(int image_channels) {
if (image_channels == 4) {
return ImageFormat::SRGBA;
} else if (image_channels == 3) {
return ImageFormat::SRGB;
} else if (image_channels == 1) {
return ImageFormat::GRAY8;
}
LOG(FATAL) << "Unsupported input image channles: " << image_channels;
}
Packet MakeImageFramePacket(cv::Mat input, int timestamp) {
ImageFrame input_image(GetImageFormat(input.channels()), input.cols,
input.rows, input.step, input.data, [](uint8*) {});
return MakePacket<ImageFrame>(std::move(input_image)).At(Timestamp(0));
}
Packet MakeImagePacket(cv::Mat input, int timestamp) {
mediapipe::Image input_image(std::make_shared<mediapipe::ImageFrame>(
GetImageFormat(input.channels()), input.cols, input.rows, input.step,
input.data, [](uint8*) {}));
return MakePacket<mediapipe::Image>(std::move(input_image)).At(Timestamp(0));
}
} // namespace mediapipe

View File

@ -0,0 +1,32 @@
#ifndef MEDIAPIPE_UTIL_IMAGE_TEST_UTILS_H_
#define MEDIAPIPE_UTIL_IMAGE_TEST_UTILS_H_
#include <string>
#include "mediapipe/framework/formats/image.h"
#include "mediapipe/framework/packet.h"
#include "mediapipe/framework/port/opencv_core_inc.h"
namespace mediapipe {
// Reads the image file into cv::Mat with RGB channels.
cv::Mat GetRgb(const std::string& path);
// Reads the image file into cv::Mat with RGBA channels.
cv::Mat GetRgba(const std::string& path);
// Reads the image file into cv::Mat with Gray channel.
cv::Mat GetGray(const std::string& path);
// Converts the image channels into corresponding ImageFormat.
mediapipe::ImageFormat::Format GetImageFormat(int image_channels);
// Converts the cv::Mat into ImageFrame packet.
Packet MakeImageFramePacket(cv::Mat input, int timestamp = 0);
// Converts the cv::Mat into Image packet.
Packet MakeImagePacket(cv::Mat input, int timestamp = 0);
} // namespace mediapipe
#endif // MEDIAPIPE_UTIL_IMAGE_TEST_UTILS_H_