picodet_openvino.h 2.2 KB
Newer Older
qq_30618961's avatar
qq_30618961 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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.
// reference from https://github.com/RangiLyu/nanodet/tree/main/demo_openvino

#ifndef _PICODET_OPENVINO_H_
#define _PICODET_OPENVINO_H_

#include <inference_engine.hpp>
20 21
#include <opencv2/core.hpp>
#include <string>
qq_30618961's avatar
qq_30618961 已提交
22

J
Jewel 已提交
23 24
#define image_size 416

25 26 27 28
typedef struct HeadInfo {
  std::string cls_layer;
  std::string dis_layer;
  int stride;
qq_30618961's avatar
qq_30618961 已提交
29 30
} HeadInfo;

31 32 33 34 35 36 37
typedef struct BoxInfo {
  float x1;
  float y1;
  float x2;
  float y2;
  float score;
  int label;
qq_30618961's avatar
qq_30618961 已提交
38 39
} BoxInfo;

40
class PicoDet {
qq_30618961's avatar
qq_30618961 已提交
41
public:
42
  PicoDet(const char *param);
qq_30618961's avatar
qq_30618961 已提交
43

44
  ~PicoDet();
qq_30618961's avatar
qq_30618961 已提交
45

46 47 48
  InferenceEngine::ExecutableNetwork network_;
  InferenceEngine::InferRequest infer_request_;
  // static bool hasGPU;
qq_30618961's avatar
qq_30618961 已提交
49

50 51 52 53 54 55 56
  std::vector<HeadInfo> heads_info_{
      // cls_pred|dis_pred|stride
      {"transpose_0.tmp_0", "transpose_1.tmp_0", 8},
      {"transpose_2.tmp_0", "transpose_3.tmp_0", 16},
      {"transpose_4.tmp_0", "transpose_5.tmp_0", 32},
      {"transpose_6.tmp_0", "transpose_7.tmp_0", 64},
  };
qq_30618961's avatar
qq_30618961 已提交
57

58 59
  std::vector<BoxInfo> detect(cv::Mat image, float score_threshold,
                              float nms_threshold);
qq_30618961's avatar
qq_30618961 已提交
60 61

private:
62 63 64 65 66 67 68 69 70 71 72
  void preprocess(cv::Mat &image, InferenceEngine::Blob::Ptr &blob);
  void decode_infer(const float *&cls_pred, const float *&dis_pred, int stride,
                    float threshold,
                    std::vector<std::vector<BoxInfo>> &results);
  BoxInfo disPred2Bbox(const float *&dfl_det, int label, float score, int x,
                       int y, int stride);
  static void nms(std::vector<BoxInfo> &result, float nms_threshold);
  std::string input_name_;
  int input_size_ = image_size;
  int num_class_ = 80;
  int reg_max_ = 7;
qq_30618961's avatar
qq_30618961 已提交
73 74 75
};

#endif