From 73f4475c170d2a9fb30cc6e92254afca7875eefa Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Thu, 13 Aug 2020 04:01:00 -0400 Subject: [PATCH] Project import generated by Copybara. GitOrigin-RevId: 5cd50ad1abca6bd5d52832975bb60e6c0fc60a27 --- .../image/image_cropping_calculator.cc | 27 +++++++++++++++---- .../image/image_cropping_calculator.h | 4 +++ .../image/image_cropping_calculator.proto | 6 +++++ .../pose_landmark_upper_body_by_roi_cpu.pbtxt | 2 ++ .../pose_landmark_upper_body_by_roi_gpu.pbtxt | 2 ++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/mediapipe/calculators/image/image_cropping_calculator.cc b/mediapipe/calculators/image/image_cropping_calculator.cc index 7754c1198..b008a9e1e 100644 --- a/mediapipe/calculators/image/image_cropping_calculator.cc +++ b/mediapipe/calculators/image/image_cropping_calculator.cc @@ -127,6 +127,10 @@ REGISTER_CALCULATOR(ImageCroppingCalculator); } options_ = cc->Options(); + output_max_width_ = + options_.has_output_max_width() ? options_.output_max_width() : FLT_MAX; + output_max_height_ = + options_.has_output_max_height() ? options_.output_max_height() : FLT_MAX; if (use_gpu_) { #if !defined(MEDIAPIPE_DISABLE_GPU) @@ -234,20 +238,27 @@ REGISTER_CALCULATOR(ImageCroppingCalculator); cv::Mat src_points; cv::boxPoints(min_rect, src_points); + float output_width = min_rect.size.width; + float output_height = min_rect.size.height; + float scale = std::min({1.0f, output_max_width_ / output_width, + output_max_height_ / output_height}); + output_width *= scale; + output_height *= scale; + float dst_corners[8] = {0, - min_rect.size.height - 1, + output_height - 1, 0, 0, - min_rect.size.width - 1, + output_width - 1, 0, - min_rect.size.width - 1, - min_rect.size.height - 1}; + output_width - 1, + output_height - 1}; cv::Mat dst_points = cv::Mat(4, 2, CV_32F, dst_corners); cv::Mat projection_matrix = cv::getPerspectiveTransform(src_points, dst_points); cv::Mat cropped_image; cv::warpPerspective(input_mat, cropped_image, projection_matrix, - cv::Size(min_rect.size.width, min_rect.size.height), + cv::Size(output_width, output_height), /* flags = */ 0, /* borderMode = */ border_mode); @@ -439,6 +450,12 @@ void ImageCroppingCalculator::GetOutputDimensions(CalculatorContext* cc, int width = static_cast(std::round((col_max - col_min) * src_width)); int height = static_cast(std::round((row_max - row_min) * src_height)); + + float scale = + std::min({1.0f, output_max_width_ / width, output_max_height_ / height}); + width *= scale; + height *= scale; + // Minimum output dimension 1x1 prevents creation of textures with 0x0. *dst_width = std::max(1, width); *dst_height = std::max(1, height); diff --git a/mediapipe/calculators/image/image_cropping_calculator.h b/mediapipe/calculators/image/image_cropping_calculator.h index 38938c223..5d50b6647 100644 --- a/mediapipe/calculators/image/image_cropping_calculator.h +++ b/mediapipe/calculators/image/image_cropping_calculator.h @@ -1,6 +1,8 @@ #ifndef MEDIAPIPE_CALCULATORS_IMAGE_IMAGE_CROPPING_CALCULATOR_H_ #define MEDIAPIPE_CALCULATORS_IMAGE_IMAGE_CROPPING_CALCULATOR_H_ +#include + #include "mediapipe/calculators/image/image_cropping_calculator.pb.h" #include "mediapipe/framework/calculator_framework.h" @@ -80,6 +82,8 @@ class ImageCroppingCalculator : public CalculatorBase { bool use_gpu_ = false; // Output texture corners (4) after transoformation in normalized coordinates. float transformed_points_[8]; + float output_max_width_ = FLT_MAX; + float output_max_height_ = FLT_MAX; #if !defined(MEDIAPIPE_DISABLE_GPU) bool gpu_initialized_ = false; mediapipe::GlCalculatorHelper gpu_helper_; diff --git a/mediapipe/calculators/image/image_cropping_calculator.proto b/mediapipe/calculators/image/image_cropping_calculator.proto index 83dce6ae7..55d3467d1 100644 --- a/mediapipe/calculators/image/image_cropping_calculator.proto +++ b/mediapipe/calculators/image/image_cropping_calculator.proto @@ -51,4 +51,10 @@ message ImageCroppingCalculatorOptions { // Specifies behaviour for crops that go beyond image borders. optional BorderMode border_mode = 8 [default = BORDER_ZERO]; + + // Specifies limits for the size of the output image. It will be scaled down, + // preserving ratio, to fit within. These do not change which area of the + // input is selected for cropping. + optional int32 output_max_width = 9; + optional int32 output_max_height = 10; } diff --git a/mediapipe/modules/pose_landmark/pose_landmark_upper_body_by_roi_cpu.pbtxt b/mediapipe/modules/pose_landmark/pose_landmark_upper_body_by_roi_cpu.pbtxt index 9b19c4b72..6a557ae20 100644 --- a/mediapipe/modules/pose_landmark/pose_landmark_upper_body_by_roi_cpu.pbtxt +++ b/mediapipe/modules/pose_landmark/pose_landmark_upper_body_by_roi_cpu.pbtxt @@ -66,6 +66,8 @@ node { options: { [mediapipe.ImageCroppingCalculatorOptions.ext] { border_mode: BORDER_REPLICATE + output_max_width: 256 + output_max_height: 256 } } } diff --git a/mediapipe/modules/pose_landmark/pose_landmark_upper_body_by_roi_gpu.pbtxt b/mediapipe/modules/pose_landmark/pose_landmark_upper_body_by_roi_gpu.pbtxt index 6197bdea0..aadbd1888 100644 --- a/mediapipe/modules/pose_landmark/pose_landmark_upper_body_by_roi_gpu.pbtxt +++ b/mediapipe/modules/pose_landmark/pose_landmark_upper_body_by_roi_gpu.pbtxt @@ -66,6 +66,8 @@ node { options: { [mediapipe.ImageCroppingCalculatorOptions.ext] { border_mode: BORDER_REPLICATE + output_max_width: 256 + output_max_height: 256 } } }