2022-11-21 23:37:42 +01:00
|
|
|
#include "mediapipe/util/image_test_utils.h"
|
|
|
|
|
2023-08-25 03:00:46 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2023-08-30 22:41:21 +02:00
|
|
|
#include "absl/log/absl_log.h"
|
2023-08-25 03:00:46 +02:00
|
|
|
#include "mediapipe/framework/formats/image.h"
|
|
|
|
#include "mediapipe/framework/formats/image_format.pb.h"
|
2022-11-21 23:37:42 +01:00
|
|
|
#include "mediapipe/framework/formats/image_frame.h"
|
|
|
|
#include "mediapipe/framework/formats/image_frame_opencv.h"
|
2023-08-25 03:00:46 +02:00
|
|
|
#include "mediapipe/framework/packet.h"
|
2022-11-21 23:37:42 +01:00
|
|
|
#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;
|
|
|
|
}
|
2023-08-30 22:41:21 +02:00
|
|
|
ABSL_LOG(FATAL) << "Unsupported input image channles: " << image_channels;
|
2022-11-21 23:37:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Packet MakeImageFramePacket(cv::Mat input, int timestamp) {
|
|
|
|
ImageFrame input_image(GetImageFormat(input.channels()), input.cols,
|
2023-08-25 03:00:46 +02:00
|
|
|
input.rows, input.step, input.data,
|
|
|
|
[input](uint8_t*) mutable { input.release(); });
|
|
|
|
return MakePacket<ImageFrame>(std::move(input_image))
|
|
|
|
.At(Timestamp(timestamp));
|
2022-11-21 23:37:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-08-25 03:00:46 +02:00
|
|
|
input.data, [input](uint8_t*) mutable { input.release(); }));
|
|
|
|
return MakePacket<mediapipe::Image>(std::move(input_image))
|
|
|
|
.At(Timestamp(timestamp));
|
|
|
|
}
|
|
|
|
|
|
|
|
cv::Mat RgbaToBgr(cv::Mat rgba) {
|
|
|
|
cv::Mat bgra;
|
|
|
|
cv::cvtColor(rgba, bgra, cv::COLOR_RGBA2BGR);
|
|
|
|
return bgra;
|
2022-11-21 23:37:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mediapipe
|