Pipeline.cc 10.2 KB
Newer Older
G
Guanghua Yu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2019 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 "Pipeline.h"

W
wangguanzhong 已提交
17 18 19 20 21 22
Detector::Detector(const std::string &modelDir,
                   const std::string &labelPath,
                   const int cpuThreadNum,
                   const std::string &cpuPowerMode,
                   int inputWidth,
                   int inputHeight,
G
Guanghua Yu 已提交
23
                   const std::vector<float> &inputMean,
W
wangguanzhong 已提交
24 25 26 27 28 29 30
                   const std::vector<float> &inputStd,
                   float scoreThreshold)
    : inputWidth_(inputWidth),
      inputHeight_(inputHeight),
      inputMean_(inputMean),
      inputStd_(inputStd),
      scoreThreshold_(scoreThreshold) {
G
Guanghua Yu 已提交
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
  paddle::lite_api::MobileConfig config;
  config.set_model_from_file(modelDir + "/model.nb");
  config.set_threads(cpuThreadNum);
  config.set_power_mode(ParsePowerMode(cpuPowerMode));
  predictor_ =
      paddle::lite_api::CreatePaddlePredictor<paddle::lite_api::MobileConfig>(
          config);
  labelList_ = LoadLabelList(labelPath);
  colorMap_ = GenerateColorMap(labelList_.size());
}

std::vector<std::string> Detector::LoadLabelList(const std::string &labelPath) {
  std::ifstream file;
  std::vector<std::string> labels;
  file.open(labelPath);
  while (file) {
    std::string line;
    std::getline(file, line);
    labels.push_back(line);
  }
  file.clear();
  file.close();
  return labels;
}

std::vector<cv::Scalar> Detector::GenerateColorMap(int numOfClasses) {
  std::vector<cv::Scalar> colorMap = std::vector<cv::Scalar>(numOfClasses);
  for (int i = 0; i < numOfClasses; i++) {
    int j = 0;
    int label = i;
    int R = 0, G = 0, B = 0;
    while (label) {
      R |= (((label >> 0) & 1) << (7 - j));
      G |= (((label >> 1) & 1) << (7 - j));
      B |= (((label >> 2) & 1) << (7 - j));
      j++;
      label >>= 3;
    }
    colorMap[i] = cv::Scalar(R, G, B);
  }
  return colorMap;
}

void Detector::Preprocess(const cv::Mat &rgbaImage) {
  // Set the data of input image
  auto inputTensor = predictor_->GetInput(0);
  std::vector<int64_t> inputShape = {1, 3, inputHeight_, inputWidth_};
  inputTensor->Resize(inputShape);
  auto inputData = inputTensor->mutable_data<float>();
  cv::Mat resizedRGBAImage;
W
wangguanzhong 已提交
81 82
  cv::resize(
      rgbaImage, resizedRGBAImage, cv::Size(inputShape[3], inputShape[2]));
G
Guanghua Yu 已提交
83 84 85
  cv::Mat resizedRGBImage;
  cv::cvtColor(resizedRGBAImage, resizedRGBImage, cv::COLOR_BGRA2RGB);
  resizedRGBImage.convertTo(resizedRGBImage, CV_32FC3, 1.0 / 255.0f);
W
wangguanzhong 已提交
86 87 88 89 90
  NHWC3ToNC3HW(reinterpret_cast<const float *>(resizedRGBImage.data),
               inputData,
               inputMean_.data(),
               inputStd_.data(),
               inputShape[3],
G
Guanghua Yu 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
               inputShape[2]);
  // Set the size of input image
  auto sizeTensor = predictor_->GetInput(1);
  sizeTensor->Resize({1, 2});
  auto sizeData = sizeTensor->mutable_data<int32_t>();
  sizeData[0] = inputShape[3];
  sizeData[1] = inputShape[2];
}

void Detector::Postprocess(std::vector<RESULT> *results) {
  auto outputTensor = predictor_->GetOutput(0);
  auto outputData = outputTensor->data<float>();
  auto outputShape = outputTensor->shape();
  int outputSize = ShapeProduction(outputShape);
  for (int i = 0; i < outputSize; i += 6) {
    // Class id
    auto class_id = static_cast<int>(round(outputData[i]));
    // Confidence score
    auto score = outputData[i + 1];
W
wangguanzhong 已提交
110
    if (score < scoreThreshold_) continue;
G
Guanghua Yu 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    RESULT object;
    object.class_name = class_id >= 0 && class_id < labelList_.size()
                            ? labelList_[class_id]
                            : "Unknow";
    object.fill_color = class_id >= 0 && class_id < colorMap_.size()
                            ? colorMap_[class_id]
                            : cv::Scalar(0, 0, 0);
    object.score = score;
    object.x = outputData[i + 2] / inputWidth_;
    object.y = outputData[i + 3] / inputHeight_;
    object.w = (outputData[i + 4] - outputData[i + 2] + 1) / inputWidth_;
    object.h = (outputData[i + 5] - outputData[i + 3] + 1) / inputHeight_;
    results->push_back(object);
  }
}

W
wangguanzhong 已提交
127 128 129 130
void Detector::Predict(const cv::Mat &rgbaImage,
                       std::vector<RESULT> *results,
                       double *preprocessTime,
                       double *predictTime,
G
Guanghua Yu 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                       double *postprocessTime) {
  auto t = GetCurrentTime();

  t = GetCurrentTime();
  Preprocess(rgbaImage);
  *preprocessTime = GetElapsedTime(t);
  LOGD("Detector postprocess costs %f ms", *preprocessTime);

  t = GetCurrentTime();
  predictor_->Run();
  *predictTime = GetElapsedTime(t);
  LOGD("Detector predict costs %f ms", *predictTime);

  t = GetCurrentTime();
  Postprocess(results);
  *postprocessTime = GetElapsedTime(t);
  LOGD("Detector postprocess costs %f ms", *postprocessTime);
}

W
wangguanzhong 已提交
150 151 152 153 154 155
Pipeline::Pipeline(const std::string &modelDir,
                   const std::string &labelPath,
                   const int cpuThreadNum,
                   const std::string &cpuPowerMode,
                   int inputWidth,
                   int inputHeight,
G
Guanghua Yu 已提交
156
                   const std::vector<float> &inputMean,
W
wangguanzhong 已提交
157 158 159 160 161 162 163 164 165 166
                   const std::vector<float> &inputStd,
                   float scoreThreshold) {
  detector_.reset(new Detector(modelDir,
                               labelPath,
                               cpuThreadNum,
                               cpuPowerMode,
                               inputWidth,
                               inputHeight,
                               inputMean,
                               inputStd,
G
Guanghua Yu 已提交
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
                               scoreThreshold));
}

void Pipeline::VisualizeResults(const std::vector<RESULT> &results,
                                cv::Mat *rgbaImage) {
  int w = rgbaImage->cols;
  int h = rgbaImage->rows;
  for (int i = 0; i < results.size(); i++) {
    RESULT object = results[i];
    cv::Rect boundingBox =
        cv::Rect(object.x * w, object.y * h, object.w * w, object.h * h) &
        cv::Rect(0, 0, w - 1, h - 1);
    // Configure text size
    std::string text = object.class_name + " ";
    text += std::to_string(static_cast<int>(object.score * 100)) + "%";
    int fontFace = cv::FONT_HERSHEY_PLAIN;
    double fontScale = 1.5f;
    float fontThickness = 1.0f;
    cv::Size textSize =
        cv::getTextSize(text, fontFace, fontScale, fontThickness, nullptr);
    // Draw roi object, text, and background
    cv::rectangle(*rgbaImage, boundingBox, object.fill_color, 2);
    cv::rectangle(*rgbaImage,
                  cv::Point2d(boundingBox.x,
                              boundingBox.y - round(textSize.height * 1.25f)),
                  cv::Point2d(boundingBox.x + boundingBox.width, boundingBox.y),
W
wangguanzhong 已提交
193 194 195 196 197 198 199 200 201
                  object.fill_color,
                  -1);
    cv::putText(*rgbaImage,
                text,
                cv::Point2d(boundingBox.x, boundingBox.y),
                fontFace,
                fontScale,
                cv::Scalar(255, 255, 255),
                fontThickness);
G
Guanghua Yu 已提交
202 203 204
  }
}

W
wangguanzhong 已提交
205 206 207 208 209 210
void Pipeline::VisualizeStatus(double readGLFBOTime,
                               double writeGLTextureTime,
                               double preprocessTime,
                               double predictTime,
                               double postprocessTime,
                               cv::Mat *rgbaImage) {
G
Guanghua Yu 已提交
211 212 213 214 215 216 217 218 219 220
  char text[255];
  cv::Scalar fontColor = cv::Scalar(255, 255, 255);
  int fontFace = cv::FONT_HERSHEY_PLAIN;
  double fontScale = 1.f;
  float fontThickness = 1;
  sprintf(text, "Read GLFBO time: %.1f ms", readGLFBOTime);
  cv::Size textSize =
      cv::getTextSize(text, fontFace, fontScale, fontThickness, nullptr);
  textSize.height *= 1.25f;
  cv::Point2d offset(10, textSize.height + 15);
W
wangguanzhong 已提交
221 222
  cv::putText(
      *rgbaImage, text, offset, fontFace, fontScale, fontColor, fontThickness);
G
Guanghua Yu 已提交
223 224
  sprintf(text, "Write GLTexture time: %.1f ms", writeGLTextureTime);
  offset.y += textSize.height;
W
wangguanzhong 已提交
225 226
  cv::putText(
      *rgbaImage, text, offset, fontFace, fontScale, fontColor, fontThickness);
G
Guanghua Yu 已提交
227 228
  sprintf(text, "Preprocess time: %.1f ms", preprocessTime);
  offset.y += textSize.height;
W
wangguanzhong 已提交
229 230
  cv::putText(
      *rgbaImage, text, offset, fontFace, fontScale, fontColor, fontThickness);
G
Guanghua Yu 已提交
231 232
  sprintf(text, "Predict time: %.1f ms", predictTime);
  offset.y += textSize.height;
W
wangguanzhong 已提交
233 234
  cv::putText(
      *rgbaImage, text, offset, fontFace, fontScale, fontColor, fontThickness);
G
Guanghua Yu 已提交
235 236
  sprintf(text, "Postprocess time: %.1f ms", postprocessTime);
  offset.y += textSize.height;
W
wangguanzhong 已提交
237 238
  cv::putText(
      *rgbaImage, text, offset, fontFace, fontScale, fontColor, fontThickness);
G
Guanghua Yu 已提交
239 240
}

W
wangguanzhong 已提交
241 242 243 244 245
bool Pipeline::Process(int inTexureId,
                       int outTextureId,
                       int textureWidth,
                       int textureHeight,
                       std::string savedImagePath) {
G
Guanghua Yu 已提交
246 247 248 249 250
  static double readGLFBOTime = 0, writeGLTextureTime = 0;
  double preprocessTime = 0, predictTime = 0, postprocessTime = 0;

  // Read pixels from FBO texture to CV image
  cv::Mat rgbaImage;
W
wangguanzhong 已提交
251 252
  CreateRGBAImageFromGLFBOTexture(
      textureWidth, textureHeight, &rgbaImage, &readGLFBOTime);
G
Guanghua Yu 已提交
253 254 255

  // Feed the image, run inference and parse the results
  std::vector<RESULT> results;
W
wangguanzhong 已提交
256 257
  detector_->Predict(
      rgbaImage, &results, &preprocessTime, &predictTime, &postprocessTime);
G
Guanghua Yu 已提交
258 259 260 261 262

  // Visualize the objects to the origin image
  VisualizeResults(results, &rgbaImage);

  // Visualize the status(performance data) to the origin image
W
wangguanzhong 已提交
263 264 265 266 267 268
  VisualizeStatus(readGLFBOTime,
                  writeGLTextureTime,
                  preprocessTime,
                  predictTime,
                  postprocessTime,
                  &rgbaImage);
G
Guanghua Yu 已提交
269 270 271 272 273 274 275 276 277 278 279 280

  // Dump modified image if savedImagePath is set
  if (!savedImagePath.empty()) {
    cv::Mat bgrImage;
    cv::cvtColor(rgbaImage, bgrImage, cv::COLOR_RGBA2BGR);
    imwrite(savedImagePath, bgrImage);
  }

  // Write back to texture2D
  WriteRGBAImageBackToGLTexture(rgbaImage, outTextureId, &writeGLTextureTime);
  return true;
}