1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| #ifndef MEDIAPIPE_CALCULATORS_IMAGE_IMAGE_PROCESSOR_CALCULATOR_H_ #define MEDIAPIPE_CALCULATORS_IMAGE_IMAGE_PROCESSOR_CALCULATOR_H_
#include "mediapipe/framework/calculator_framework.h" #include "mediapipe/framework/formats/image_frame.h" #include "mediapipe/framework/port/opencv_imgproc.h" #include "mediapipe/framework/port/opencv_imgcodecs.h" #include "mediapipe/framework/port/logging.h" #include "mediapipe/framework/port/ret_check.h" #include "mediapipe/framework/port/status.h"
namespace mediapipe {
message ImageProcessorOptions { optional int32 target_width = 1 [default = 0]; optional int32 target_height = 2 [default = 0]; optional bool grayscale = 3 [default = false]; optional bool normalize = 4 [default = false]; optional bool enable_debug = 5 [default = false]; optional float rotation_angle = 6 [default = 0.0]; }
class ImageProcessorCalculator : public CalculatorBase { public: static absl::Status GetContract(CalculatorContract* cc) { cc->Inputs().Tag("IMAGE").Set<ImageFrame>(); if (cc->Inputs().HasTag("ROI")) { cc->Inputs().Tag("ROI").Set<Rect>(); } cc->Outputs().Tag("IMAGE").Set<ImageFrame>(); if (cc->Outputs().HasTag("METRICS")) { cc->Outputs().Tag("METRICS").Set<PerformanceMetrics>(); } cc->Options<ImageProcessorOptions>(); return absl::OkStatus(); }
absl::Status Open(CalculatorContext* cc) override { const auto& options = cc->Options<ImageProcessorOptions>(); target_width_ = options.target_width(); target_height_ = options.target_height(); grayscale_ = options.grayscale(); normalize_ = options.normalize(); enable_debug_ = options.enable_debug(); rotation_angle_ = options.rotation_angle(); if (target_width_ < 0 || target_height_ < 0) { return absl::InvalidArgumentError("Invalid target size"); } if (rotation_angle_ != 0.0f && rotation_angle_ != 90.0f && rotation_angle_ != 180.0f && rotation_angle_ != 270.0f) { return absl::InvalidArgumentError("Invalid rotation angle"); } LOG(INFO) << "ImageProcessorCalculator initialized: " << "target_size=" << target_width_ << "x" << target_height_ << ", grayscale=" << grayscale_ << ", normalize=" << normalize_ << ", rotation=" << rotation_angle_; return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) override { if (cc->Inputs().Tag("IMAGE").IsEmpty()) { return absl::OkStatus(); } auto start_time = std::chrono::high_resolution_clock::now(); const ImageFrame& input = cc->Inputs().Tag("IMAGE").Get<ImageFrame>(); cv::Mat input_mat = formats::MatView(&input); cv::Mat output_mat; if (rotation_angle_ != 0.0f) { RotateImage(input_mat, &output_mat, rotation_angle_); } else { output_mat = input_mat.clone(); } if (target_width_ > 0 && target_height_ > 0) { cv::resize(output_mat, output_mat, cv::Size(target_width_, target_height_)); } if (grayscale_) { cv::cvtColor(output_mat, output_mat, cv::COLOR_RGB2GRAY); cv::cvtColor(output_mat, output_mat, cv::COLOR_GRAY2RGB); } if (normalize_) { output_mat.convertTo(output_mat, CV_32F, 1.0 / 255.0); output_mat.convertTo(output_mat, CV_8U, 255.0); } auto output_frame = absl::make_unique<ImageFrame>( output_mat.channels() == 3 ? ImageFormat::SRGB : ImageFormat::GRAY8, output_mat.cols, output_mat.rows, output_mat.step, output_mat.data, [output_mat](uint8*) mutable { output_mat.release(); }); cc->Outputs().Tag("IMAGE").Add(output_frame.release(), cc->InputTimestamp()); if (cc->Outputs().HasTag("METRICS")) { auto end_time = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::microseconds>( end_time - start_time).count(); PerformanceMetrics metrics; metrics.process_time_us = duration; metrics.input_size = cv::Size(input.Width(), input.Height()); metrics.output_size = cv::Size(output_mat.cols, output_mat.rows); cc->Outputs().Tag("METRICS").AddPacket( MakePacket<PerformanceMetrics>(metrics).At(cc->InputTimestamp())); } if (enable_debug_) { LOG(INFO) << "Processed image: " << input.Width() << "x" << input.Height() << " -> " << output_mat.cols << "x" << output_mat.rows; } process_count_++; return absl::OkStatus(); }
absl::Status Close(CalculatorContext* cc) override { LOG(INFO) << "ImageProcessorCalculator closed after processing " << process_count_ << " frames"; return absl::OkStatus(); }
private: int target_width_ = 0; int target_height_ = 0; bool grayscale_ = false; bool normalize_ = false; bool enable_debug_ = false; float rotation_angle_ = 0.0f; int process_count_ = 0; void RotateImage(const cv::Mat& input, cv::Mat* output, float angle) { if (angle == 90.0f) { cv::rotate(input, *output, cv::ROTATE_90_CLOCKWISE); } else if (angle == 180.0f) { cv::rotate(input, *output, cv::ROTATE_180); } else if (angle == 270.0f) { cv::rotate(input, *output, cv::ROTATE_90_COUNTERCLOCKWISE); } else { cv::Point2f center(input.cols / 2.0f, input.rows / 2.0f); cv::Mat rot_mat = cv::getRotationMatrix2D(center, angle, 1.0); cv::warpAffine(input, *output, rot_mat, input.size()); } } };
REGISTER_CALCULATOR(ImageProcessorCalculator);
}
#endif
|