preprocess_op.cc 7.5 KB
Newer Older
D
dongshuilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
//   Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <string>
#include <thread>
#include <vector>

#include "include/preprocess_op.h"

namespace PPShiTu {

D
dongshuilong 已提交
23
void InitInfo::Run(cv::Mat *im, ImageBlob *data) {
D
dongshuilong 已提交
24 25 26 27 28 29 30
  data->im_shape_ = {static_cast<float>(im->rows),
                     static_cast<float>(im->cols)};
  data->scale_factor_ = {1., 1.};
  data->in_net_shape_ = {static_cast<float>(im->rows),
                         static_cast<float>(im->cols)};
}

D
dongshuilong 已提交
31
void NormalizeImage::Run(cv::Mat *im, ImageBlob *data) {
D
dongshuilong 已提交
32 33
  double e = 1.0;
  if (is_scale_) {
D
dongshuilong 已提交
34
    e *= 1. / 255.0;
D
dongshuilong 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48
  }
  (*im).convertTo(*im, CV_32FC3, e);
  for (int h = 0; h < im->rows; h++) {
    for (int w = 0; w < im->cols; w++) {
      im->at<cv::Vec3f>(h, w)[0] =
          (im->at<cv::Vec3f>(h, w)[0] - mean_[0]) / scale_[0];
      im->at<cv::Vec3f>(h, w)[1] =
          (im->at<cv::Vec3f>(h, w)[1] - mean_[1]) / scale_[1];
      im->at<cv::Vec3f>(h, w)[2] =
          (im->at<cv::Vec3f>(h, w)[2] - mean_[2]) / scale_[2];
    }
  }
}

D
dongshuilong 已提交
49
void NormalizeImage::Run_feature(cv::Mat *im, const std::vector<float> &mean,
D
dongshuilong 已提交
50
                                 const std::vector<float> &std, float scale) {
D
dongshuilong 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64
  (*im).convertTo(*im, CV_32FC3, scale);
  for (int h = 0; h < im->rows; h++) {
    for (int w = 0; w < im->cols; w++) {
      im->at<cv::Vec3f>(h, w)[0] =
          (im->at<cv::Vec3f>(h, w)[0] - mean[0]) / std[0];
      im->at<cv::Vec3f>(h, w)[1] =
          (im->at<cv::Vec3f>(h, w)[1] - mean[1]) / std[1];
      im->at<cv::Vec3f>(h, w)[2] =
          (im->at<cv::Vec3f>(h, w)[2] - mean[2]) / std[2];
    }
  }
}

void Permute::Run(cv::Mat *im, ImageBlob *data) {
D
dongshuilong 已提交
65 66 67 68 69
  (*im).convertTo(*im, CV_32FC3);
  int rh = im->rows;
  int rw = im->cols;
  int rc = im->channels();
  (data->im_data_).resize(rc * rh * rw);
D
dongshuilong 已提交
70
  float *base = (data->im_data_).data();
D
dongshuilong 已提交
71 72 73 74 75
  for (int i = 0; i < rc; ++i) {
    cv::extractChannel(*im, cv::Mat(rh, rw, CV_32FC1, base + i * rh * rw), i);
  }
}

D
dongshuilong 已提交
76 77 78 79 80 81 82 83 84 85
void Permute::Run_feature(const cv::Mat *im, float *data) {
  int rh = im->rows;
  int rw = im->cols;
  int rc = im->channels();
  for (int i = 0; i < rc; ++i) {
    cv::extractChannel(*im, cv::Mat(rh, rw, CV_32FC1, data + i * rh * rw), i);
  }
}

void Resize::Run(cv::Mat *im, ImageBlob *data) {
D
dongshuilong 已提交
86 87 88 89 90
  auto resize_scale = GenerateScale(*im);
  data->im_shape_ = {static_cast<float>(im->cols * resize_scale.first),
                     static_cast<float>(im->rows * resize_scale.second)};
  data->in_net_shape_ = {static_cast<float>(im->cols * resize_scale.first),
                         static_cast<float>(im->rows * resize_scale.second)};
D
dongshuilong 已提交
91 92
  cv::resize(*im, *im, cv::Size(), resize_scale.first, resize_scale.second,
             interp_);
D
dongshuilong 已提交
93
  data->im_shape_ = {
D
dongshuilong 已提交
94 95
      static_cast<float>(im->rows),
      static_cast<float>(im->cols),
D
dongshuilong 已提交
96 97
  };
  data->scale_factor_ = {
D
dongshuilong 已提交
98 99
      resize_scale.second,
      resize_scale.first,
D
dongshuilong 已提交
100 101 102
  };
}

D
dongshuilong 已提交
103
std::pair<float, float> Resize::GenerateScale(const cv::Mat &im) {
D
dongshuilong 已提交
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
  std::pair<float, float> resize_scale;
  int origin_w = im.cols;
  int origin_h = im.rows;

  if (keep_ratio_) {
    int im_size_max = std::max(origin_w, origin_h);
    int im_size_min = std::min(origin_w, origin_h);
    int target_size_max =
        *std::max_element(target_size_.begin(), target_size_.end());
    int target_size_min =
        *std::min_element(target_size_.begin(), target_size_.end());
    float scale_min =
        static_cast<float>(target_size_min) / static_cast<float>(im_size_min);
    float scale_max =
        static_cast<float>(target_size_max) / static_cast<float>(im_size_max);
    float scale_ratio = std::min(scale_min, scale_max);
    resize_scale = {scale_ratio, scale_ratio};
  } else {
    resize_scale.first =
        static_cast<float>(target_size_[1]) / static_cast<float>(origin_w);
    resize_scale.second =
        static_cast<float>(target_size_[0]) / static_cast<float>(origin_h);
  }
  return resize_scale;
}

D
dongshuilong 已提交
130 131
void Resize::Run_feature(const cv::Mat &img, cv::Mat &resize_img,
                         int resize_short_size, int size) {
D
dongshuilong 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  int resize_h = 0;
  int resize_w = 0;
  if (size > 0) {
    resize_h = size;
    resize_w = size;
  } else {
    int w = img.cols;
    int h = img.rows;

    float ratio = 1.f;
    if (h < w) {
      ratio = float(resize_short_size) / float(h);
    } else {
      ratio = float(resize_short_size) / float(w);
    }
    resize_h = round(float(h) * ratio);
    resize_w = round(float(w) * ratio);
  }
  cv::resize(img, resize_img, cv::Size(resize_w, resize_h));
}

void PadStride::Run(cv::Mat *im, ImageBlob *data) {
D
dongshuilong 已提交
154 155 156 157 158 159 160 161
  if (stride_ <= 0) {
    return;
  }
  int rc = im->channels();
  int rh = im->rows;
  int rw = im->cols;
  int nh = (rh / stride_) * stride_ + (rh % stride_ != 0) * stride_;
  int nw = (rw / stride_) * stride_ + (rw % stride_ != 0) * stride_;
D
dongshuilong 已提交
162 163
  cv::copyMakeBorder(*im, *im, 0, nh - rh, 0, nw - rw, cv::BORDER_CONSTANT,
                     cv::Scalar(0));
D
dongshuilong 已提交
164
  data->in_net_shape_ = {
D
dongshuilong 已提交
165 166
      static_cast<float>(im->rows),
      static_cast<float>(im->cols),
D
dongshuilong 已提交
167 168 169
  };
}

D
dongshuilong 已提交
170
void TopDownEvalAffine::Run(cv::Mat *im, ImageBlob *data) {
D
dongshuilong 已提交
171 172 173
  cv::resize(*im, *im, cv::Size(trainsize_[0], trainsize_[1]), 0, 0, interp_);
  // todo: Simd::ResizeBilinear();
  data->in_net_shape_ = {
D
dongshuilong 已提交
174 175
      static_cast<float>(trainsize_[1]),
      static_cast<float>(trainsize_[0]),
D
dongshuilong 已提交
176 177 178 179
  };
}

// Preprocessor op running order
D
dongshuilong 已提交
180 181 182 183 184 185
const std::vector<std::string> Preprocessor::RUN_ORDER = {
    "InitInfo",          "DetTopDownEvalAffine", "DetResize",
    "DetNormalizeImage", "DetPadStride",         "DetPermute"};

void Preprocessor::Run(cv::Mat *im, ImageBlob *data) {
  for (const auto &name : RUN_ORDER) {
D
dongshuilong 已提交
186 187 188 189 190 191
    if (ops_.find(name) != ops_.end()) {
      ops_[name]->Run(im, data);
    }
  }
}

D
dongshuilong 已提交
192 193
void CropImg(cv::Mat &img, cv::Mat &crop_img, std::vector<int> &area,
             std::vector<float> &center, std::vector<float> &scale,
D
dongshuilong 已提交
194 195 196 197 198
             float expandratio) {
  int crop_x1 = std::max(0, area[0]);
  int crop_y1 = std::max(0, area[1]);
  int crop_x2 = std::min(img.cols - 1, area[2]);
  int crop_y2 = std::min(img.rows - 1, area[3]);
D
dongshuilong 已提交
199

D
dongshuilong 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  int center_x = (crop_x1 + crop_x2) / 2.;
  int center_y = (crop_y1 + crop_y2) / 2.;
  int half_h = (crop_y2 - crop_y1) / 2.;
  int half_w = (crop_x2 - crop_x1) / 2.;

  if (half_h * 3 > half_w * 4) {
    half_w = static_cast<int>(half_h * 0.75);
  } else {
    half_h = static_cast<int>(half_w * 4 / 3);
  }

  crop_x1 =
      std::max(0, center_x - static_cast<int>(half_w * (1 + expandratio)));
  crop_y1 =
      std::max(0, center_y - static_cast<int>(half_h * (1 + expandratio)));
  crop_x2 = std::min(img.cols - 1,
                     static_cast<int>(center_x + half_w * (1 + expandratio)));
  crop_y2 = std::min(img.rows - 1,
                     static_cast<int>(center_y + half_h * (1 + expandratio)));
  crop_img =
      img(cv::Range(crop_y1, crop_y2 + 1), cv::Range(crop_x1, crop_x2 + 1));

  center.clear();
  center.emplace_back((crop_x1 + crop_x2) / 2);
  center.emplace_back((crop_y1 + crop_y2) / 2);
  scale.clear();
  scale.emplace_back((crop_x2 - crop_x1));
  scale.emplace_back((crop_y2 - crop_y1));
}

D
dongshuilong 已提交
230
} // namespace PPShiTu