Pipeline.h 4.6 KB
Newer Older
G
Guanghua Yu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// 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.

#pragma once

#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <string>
#include <vector>
W
wangguanzhong 已提交
25 26
#include "Utils.h"
#include "paddle_api.h"
G
Guanghua Yu 已提交
27 28 29 30 31 32 33 34 35 36 37 38

struct RESULT {
  std::string class_name;
  cv::Scalar fill_color;
  float score;
  float x;
  float y;
  float w;
  float h;
};

class Detector {
W
wangguanzhong 已提交
39 40 41 42 43 44 45
 public:
  explicit Detector(const std::string &modelDir,
                    const std::string &labelPath,
                    const int cpuThreadNum,
                    const std::string &cpuPowerMode,
                    int inputWidth,
                    int inputHeight,
G
Guanghua Yu 已提交
46
                    const std::vector<float> &inputMean,
W
wangguanzhong 已提交
47 48
                    const std::vector<float> &inputStd,
                    float scoreThreshold);
G
Guanghua Yu 已提交
49

W
wangguanzhong 已提交
50 51 52 53
  void Predict(const cv::Mat &rgbImage,
               std::vector<RESULT> *results,
               double *preprocessTime,
               double *predictTime,
G
Guanghua Yu 已提交
54 55
               double *postprocessTime);

W
wangguanzhong 已提交
56
 private:
G
Guanghua Yu 已提交
57 58 59 60 61
  std::vector<std::string> LoadLabelList(const std::string &path);
  std::vector<cv::Scalar> GenerateColorMap(int numOfClasses);
  void Preprocess(const cv::Mat &rgbaImage);
  void Postprocess(std::vector<RESULT> *results);

W
wangguanzhong 已提交
62
 private:
G
Guanghua Yu 已提交
63 64 65 66 67 68 69 70 71 72 73
  int inputWidth_;
  int inputHeight_;
  std::vector<float> inputMean_;
  std::vector<float> inputStd_;
  float scoreThreshold_;
  std::vector<std::string> labelList_;
  std::vector<cv::Scalar> colorMap_;
  std::shared_ptr<paddle::lite_api::PaddlePredictor> predictor_;
};

class Pipeline {
W
wangguanzhong 已提交
74 75 76 77 78 79 80 81 82 83
 public:
  Pipeline(const std::string &modelDir,
           const std::string &labelPath,
           const int cpuThreadNum,
           const std::string &cpuPowerMode,
           int inputWidth,
           int inputHeight,
           const std::vector<float> &inputMean,
           const std::vector<float> &inputStd,
           float scoreThreshold);
G
Guanghua Yu 已提交
84

W
wangguanzhong 已提交
85 86 87 88 89
  bool Process(int inTextureId,
               int outTextureId,
               int textureWidth,
               int textureHeight,
               std::string savedImagePath);
G
Guanghua Yu 已提交
90

W
wangguanzhong 已提交
91
 private:
G
Guanghua Yu 已提交
92
  // Read pixels from FBO texture to CV image
W
wangguanzhong 已提交
93 94
  void CreateRGBAImageFromGLFBOTexture(int textureWidth,
                                       int textureHeight,
G
Guanghua Yu 已提交
95 96 97 98
                                       cv::Mat *rgbaImage,
                                       double *readGLFBOTime) {
    auto t = GetCurrentTime();
    rgbaImage->create(textureHeight, textureWidth, CV_8UC4);
W
wangguanzhong 已提交
99 100 101 102 103 104
    glReadPixels(0,
                 0,
                 textureWidth,
                 textureHeight,
                 GL_RGBA,
                 GL_UNSIGNED_BYTE,
G
Guanghua Yu 已提交
105 106 107 108 109 110
                 rgbaImage->data);
    *readGLFBOTime = GetElapsedTime(t);
    LOGD("Read from FBO texture costs %f ms", *readGLFBOTime);
  }

  // Write back to texture2D
W
wangguanzhong 已提交
111 112
  void WriteRGBAImageBackToGLTexture(const cv::Mat &rgbaImage,
                                     int textureId,
G
Guanghua Yu 已提交
113 114 115 116
                                     double *writeGLTextureTime) {
    auto t = GetCurrentTime();
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureId);
W
wangguanzhong 已提交
117 118 119 120 121 122 123 124 125
    glTexSubImage2D(GL_TEXTURE_2D,
                    0,
                    0,
                    0,
                    rgbaImage.cols,
                    rgbaImage.rows,
                    GL_RGBA,
                    GL_UNSIGNED_BYTE,
                    rgbaImage.data);
G
Guanghua Yu 已提交
126 127 128 129 130 131 132 133
    *writeGLTextureTime = GetElapsedTime(t);
    LOGD("Write back to texture2D costs %f ms", *writeGLTextureTime);
  }

  // Visualize the results to origin image
  void VisualizeResults(const std::vector<RESULT> &results, cv::Mat *rgbaImage);

  // Visualize the status(performace data) to origin image
W
wangguanzhong 已提交
134 135 136 137 138 139
  void VisualizeStatus(double readGLFBOTime,
                       double writeGLTextureTime,
                       double preprocessTime,
                       double predictTime,
                       double postprocessTime,
                       cv::Mat *rgbaImage);
G
Guanghua Yu 已提交
140

W
wangguanzhong 已提交
141
 private:
G
Guanghua Yu 已提交
142 143
  std::shared_ptr<Detector> detector_;
};