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:39:56 -08:00
committed by Copybara-Service
parent 652423a23d
commit adddf2c2ab
5 changed files with 166 additions and 112 deletions
+18
View File
@@ -368,3 +368,21 @@ cc_test(
"//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",
],
)
+57
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
+32
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_