FastDeploy  latest
Fast & Easy to Deploy!
mat.h
1 // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 #include "fastdeploy/core/fd_tensor.h"
16 #include "fastdeploy/vision/common/processors/proc_lib.h"
17 #include "opencv2/core/core.hpp"
18 
19 #ifdef ENABLE_FLYCV
20 #include "flycv.h" // NOLINT
21 #endif
22 
23 #ifdef WITH_GPU
24 #include <cuda_runtime_api.h>
25 #endif
26 
27 namespace fastdeploy {
28 namespace vision {
29 
30 enum Layout { HWC, CHW };
31 
34 struct FASTDEPLOY_DECL Mat {
35  Mat() = default;
36  explicit Mat(const cv::Mat& mat) {
37  cpu_mat = mat;
38  layout = Layout::HWC;
39  height = cpu_mat.rows;
40  width = cpu_mat.cols;
41  channels = cpu_mat.channels();
42  mat_type = ProcLib::OPENCV;
43  }
44 
45 #ifdef ENABLE_FLYCV
46  explicit Mat(const fcv::Mat& mat) {
47  fcv_mat = mat;
48  layout = Layout::HWC;
49  height = fcv_mat.height();
50  width = fcv_mat.width();
51  channels = fcv_mat.channels();
52  mat_type = ProcLib::FLYCV;
53  }
54 #endif
55 
56  Mat(const Mat& mat) = default;
57  Mat& operator=(const Mat& mat) = default;
58 
59  // Move constructor
60  Mat(Mat&& other) = default;
61 
62  // Careful if you use this interface
63  // this only used if you don't want to write
64  // the original data, and write to a new cv::Mat
65  // then replace the old cv::Mat of this structure
66  void SetMat(const cv::Mat& mat) {
67  cpu_mat = mat;
68  mat_type = ProcLib::OPENCV;
69  }
70 
71  cv::Mat* GetOpenCVMat();
72 
73 #ifdef ENABLE_FLYCV
74  void SetMat(const fcv::Mat& mat) {
75  fcv_mat = mat;
76  mat_type = ProcLib::FLYCV;
77  }
78  fcv::Mat* GetFlyCVMat();
79 #endif
80 
81  void* Data();
82 
83  // Get fd_tensor
84  FDTensor* Tensor();
85 
86  // Set fd_tensor
87  void SetTensor(FDTensor* tensor);
88 
89  void SetTensor(std::shared_ptr<FDTensor>& tensor);
90 
91  private:
92  int channels;
93  int height;
94  int width;
95  cv::Mat cpu_mat;
96 #ifdef ENABLE_FLYCV
97  fcv::Mat fcv_mat;
98 #endif
99 #ifdef WITH_GPU
100  cudaStream_t stream = nullptr;
101 #endif
102  // Currently, fd_tensor is only used by CUDA and CV-CUDA,
103  // OpenCV and FlyCV are not using it.
104  std::shared_ptr<FDTensor> fd_tensor = std::make_shared<FDTensor>();
105 
106  public:
107  FDDataType Type();
108  int Channels() const { return channels; }
109  int Width() const { return width; }
110  int Height() const { return height; }
111  void SetChannels(int s) { channels = s; }
112  void SetWidth(int w) { width = w; }
113  void SetHeight(int h) { height = h; }
114 
115  // When using CV-CUDA/CUDA, please set input/output cache,
116  // refer to manager.cc
117  FDTensor* input_cache = nullptr;
118  FDTensor* output_cache = nullptr;
119 #ifdef WITH_GPU
120  cudaStream_t Stream() const { return stream; }
121  void SetStream(cudaStream_t s) { stream = s; }
122 #endif
123 
124  // Transfer the vision::Mat to FDTensor
125  void ShareWithTensor(FDTensor* tensor);
126  // Only support copy to cpu tensor now
127  bool CopyToTensor(FDTensor* tensor);
128 
129  // Debug functions
130  // TODO(jiangjiajun) Develop a right process pipeline with c++
131  // is not a easy things, Will add more debug function here to
132  // help debug processed image. This function will print shape
133  // and mean of each channels of the Mat
134  void PrintInfo(const std::string& flag);
135 
136  ProcLib mat_type = ProcLib::OPENCV;
137  Layout layout = Layout::HWC;
138  Device device = Device::CPU;
139  ProcLib proc_lib = ProcLib::DEFAULT;
140 
141  // Create FD Mat from FD Tensor. This method only create a
142  // new FD Mat with zero copy and it's data pointer is reference
143  // to the original memory buffer of input FD Tensor. Carefully,
144  // any operation on this Mat may change memory that points to
145  // FDTensor. We assume that the memory Mat points to is mutable.
146  // This method will create a FD Mat according to current global
147  // default ProcLib (OPENCV,FLYCV,...).
148  static Mat Create(const FDTensor& tensor);
149  static Mat Create(const FDTensor& tensor, ProcLib lib);
150  static Mat Create(int height, int width, int channels,
151  FDDataType type, void* data);
152  static Mat Create(int height, int width, int channels,
153  FDDataType type, void* data, ProcLib lib);
154 };
155 
156 typedef Mat FDMat;
157 /*
158  * @brief Wrap a cv::Mat to FDMat, there's no memory copy, memory buffer is managed by user
159  */
160 FASTDEPLOY_DECL FDMat WrapMat(const cv::Mat& image);
161 /*
162  * Warp a vector<cv::Mat> to vector<FDMat>, there's no memory copy, memory buffer is managed by user
163  */
164 FASTDEPLOY_DECL std::vector<FDMat> WrapMat(const std::vector<cv::Mat>& images);
165 
166 bool CheckShapeConsistency(std::vector<Mat>* mats);
167 
168 // Create an input tensor on GPU and save into input_cache.
169 // If the Mat is on GPU, return the mat->Tensor() directly.
170 // If the Mat is on CPU, then update the input cache tensor and copy the mat's
171 // CPU tensor to this new GPU input cache tensor.
172 FDTensor* CreateCachedGpuInputTensor(Mat* mat);
173 } // namespace vision
174 } // namespace fastdeploy
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
FDMat is a structure for replace cv::Mat.
Definition: mat.h:34
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16