Apollo 11.0
自动驾驶开放平台
data_provider.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
17
18#include "cyber/common/log.h"
20
21namespace apollo {
22namespace perception {
23namespace camera {
24
26 src_height_ = options.image_height;
27 src_width_ = options.image_width;
28 sensor_name_ = options.sensor_name;
29 device_id_ = options.device_id;
30
31 if (cudaSetDevice(device_id_) != cudaSuccess) {
32 AERROR << "Failed to set device to: " << device_id_;
33 return false;
34 }
35
36 // Initialize uint8 blobs
40
41 // Allocate CPU memory for uint8 blobs
42 gray_->cpu_data();
43 rgb_->cpu_data();
44 bgr_->cpu_data();
45
46 // Allocate GPU memory for uint8 blobs
47 gray_->gpu_data();
48 rgb_->gpu_data();
49 bgr_->gpu_data();
50
51 if (options.do_undistortion) {
52 handler_.reset(new UndistortionHandler());
53 if (!handler_->Init(options.sensor_name, device_id_)) {
54 return false;
55 }
56 // Initialize uint8 blobs
57 ori_gray_.reset(
59 ori_rgb_.reset(
61 ori_bgr_.reset(
63
64 // Allocate CPU memory for uint8 blobs
65 ori_gray_->cpu_data();
66 ori_rgb_->cpu_data();
67 ori_bgr_->cpu_data();
68
69 // Allocate GPU memory for uint8 blobs
70 ori_gray_->gpu_data();
71 ori_rgb_->gpu_data();
72 ori_bgr_->gpu_data();
73 }
74
75 // Warm up nppi functions
76 {
77 bgr_ready_ = false;
78 rgb_ready_ = true;
79 gray_ready_ = false;
81 bgr_ready_ = false;
82 rgb_ready_ = false;
83 gray_ready_ = true;
85 }
86 {
87 bgr_ready_ = false;
88 rgb_ready_ = false;
89 gray_ready_ = true;
91 bgr_ready_ = true;
92 rgb_ready_ = false;
93 gray_ready_ = false;
95 }
96 {
97 bgr_ready_ = false;
98 rgb_ready_ = true;
99 gray_ready_ = false;
101 bgr_ready_ = true;
102 rgb_ready_ = false;
103 gray_ready_ = false;
105 }
106 bgr_ready_ = false;
107 rgb_ready_ = false;
108 gray_ready_ = false;
109
110 return true;
111}
112
113bool DataProvider::FillImageData(int rows, int cols, const uint8_t *data,
114 const std::string &encoding) {
115 if (cudaSetDevice(device_id_) != cudaSuccess) {
116 AERROR << "Failed to set device to: " << device_id_;
117 return false;
118 }
119
120 gray_ready_ = false;
121 rgb_ready_ = false;
122 bgr_ready_ = false;
123
124 bool success = false;
125
126#if USE_GPU == 0 // copy to host memory
127 AINFO << "Fill in CPU mode ...";
128 if (handler_ != nullptr) {
129 AERROR << "Undistortion DO NOT support CPU mode!";
130 return false;
131 }
132 if (encoding == "rgb8") {
133 memcpy(rgb_->mutable_cpu_data(), data, rgb_->count() * sizeof(data[0]));
134 rgb_ready_ = true;
135 success = true;
136 } else if (encoding == "bgr8") {
137 memcpy(bgr_->mutable_cpu_data(), data, bgr_->count() * sizeof(data[0]));
138 bgr_ready_ = true;
139 success = true;
140 } else if (encoding == "gray" || encoding == "y") {
141 memcpy(gray_->mutable_cpu_data(), data, gray_->count() * sizeof(data[0]));
142 gray_ready_ = true;
143 success = true;
144 } else {
145 AERROR << "Unrecognized image encoding: " << encoding;
146 }
147#else // copy to device memory directly
148 AINFO << "Fill in GPU mode ...";
149 if (encoding == "rgb8") {
150 if (handler_ != nullptr) {
151 cudaMemcpy(ori_rgb_->mutable_gpu_data(), data,
152 ori_rgb_->rows() * ori_rgb_->width_step(), cudaMemcpyDefault);
153 success = handler_->Handle(*ori_rgb_, rgb_.get());
154 } else {
155 cudaMemcpy(rgb_->mutable_gpu_data(), data,
156 rgb_->rows() * rgb_->width_step(), cudaMemcpyDefault);
157 success = true;
158 }
159 rgb_ready_ = true;
160 } else if (encoding == "bgr8") {
161 if (handler_ != nullptr) {
162 cudaMemcpy(ori_bgr_->mutable_gpu_data(), data,
163 ori_bgr_->rows() * ori_bgr_->width_step(), cudaMemcpyDefault);
164 success = handler_->Handle(*ori_bgr_, bgr_.get());
165 } else {
166 cudaMemcpy(bgr_->mutable_gpu_data(), data,
167 bgr_->rows() * bgr_->width_step(), cudaMemcpyDefault);
168 success = true;
169 }
170 bgr_ready_ = true;
171 } else if (encoding == "gray" || encoding == "y") {
172 if (handler_ != nullptr) {
173 cudaMemcpy(ori_gray_->mutable_gpu_data(), data,
174 ori_gray_->rows() * ori_gray_->width_step(),
175 cudaMemcpyDefault);
176 success = handler_->Handle(*ori_gray_, gray_.get());
177 } else {
178 cudaMemcpy(gray_->mutable_gpu_data(), data,
179 gray_->rows() * gray_->width_step(), cudaMemcpyDefault);
180 success = true;
181 }
182 gray_ready_ = true;
183 } else {
184 AERROR << "Unrecognized image encoding: " << encoding;
185 }
186#endif
187
188 AINFO << "Done! (" << success << ")";
189 return success;
190}
191
192#if 0
194 base::Blob<float> *blob) {
195 bool ret = GetImageBlob(options, &temp_uint8_);
196 if (!ret) {
197 return false;
198 }
199 blob->Reshape(temp_uint8_.shape());
200 NppiSize roi;
201 roi.height = temp_uint8_.shape(1);
202 roi.width = temp_uint8_.shape(2);
203 int channels = base::kChannelsMap.at(options.target_color);
204 const uint8_t *temp_ptr = temp_uint8_.gpu_data();
205 float *blob_ptr = blob->mutable_gpu_data();
206 int temp_step = temp_uint8_.count(2) * sizeof(uint8_t);
207 int blob_step = blob->count(2) * sizeof(float);
208#if GPU_PLATFORM == NVIDIA
209 if (channels == 1) {
210 nppiConvert_8u32f_C1R(temp_ptr, temp_step, blob_ptr, blob_step, roi);
211 } else {
212 nppiConvert_8u32f_C3R(temp_ptr, temp_step, blob_ptr, blob_step, roi);
213 }
214#elif GPU_PLATFORM == AMD
215 // TODO(B1tway): Add necesssary RPP API
216#endif
217 return true;
218}
219#endif
220
222 base::Blob<uint8_t> *blob) {
223 base::Image8U image;
224 if (!GetImage(options, &image)) {
225 return false;
226 }
227 imageToBlob(image, blob);
228 return true;
229}
230
232 base::Image8U *image) {
233 AINFO << "GetImage ...";
234 if (image == nullptr) {
235 return false;
236 }
237 bool success = false;
238 switch (options.target_color) {
239 case base::Color::RGB:
240 success = to_rgb_image();
241 *image = (*rgb_);
242 break;
243 case base::Color::BGR:
244 success = to_bgr_image();
245 *image = (*bgr_);
246 break;
248 success = to_gray_image();
249 *image = (*gray_);
250 break;
251 default:
252 AERROR << "Unsupported Color: "
253 << static_cast<uint8_t>(options.target_color);
254 }
255 if (!success) {
256 return false;
257 }
258
259 if (options.do_crop) {
260 AINFO << "\tcropping ...";
261 *image = (*image)(options.crop_roi);
262 }
263 AINFO << "Done!";
264 return true;
265}
266
268 if (!gray_ready_) {
269 if (bgr_ready_) {
270 float coeffs[] = {0.114f, 0.587f, 0.299f};
272 gray_ready_ = true;
273 } else if (rgb_ready_) {
274 float coeffs[] = {0.299f, 0.587f, 0.114f};
276 gray_ready_ = true;
277 } else {
278 AWARN << "No image data filled yet, return uninitialized blob!";
279 return false;
280 }
281 }
282 return true;
283}
284
286 if (!rgb_ready_) {
287 if (bgr_ready_) {
288 // BGR2RGB takes less than 0.010ms on K2200
289 const int order[] = {2, 1, 0};
291 rgb_ready_ = true;
292 } else if (gray_ready_) {
294 rgb_ready_ = true;
295 } else {
296 AWARN << "No image data filled yet, return uninitialized blob!";
297 return false;
298 }
299 }
300 return true;
301}
302
304 if (!bgr_ready_) {
305 if (rgb_ready_) {
306 const int order[] = {2, 1, 0};
308 bgr_ready_ = true;
309 } else if (gray_ready_) {
311 bgr_ready_ = true;
312 } else {
313 AWARN << "No image data filled yet, return uninitialized blob!";
314 return false;
315 }
316 }
317 return true;
318}
319
320} // namespace camera
321} // namespace perception
322} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
const Dtype * gpu_data() const
Definition blob.cc:154
const std::vector< int > & shape() const
Definition blob.h:130
void Reshape(const int num, const int channels, const int height, const int width)
Deprecated; use Reshape(const std::vector<int>& shape).
Definition blob.cc:72
A wrapper around Blob holders serving as the basic computational unit for images.
Definition image_8u.h:44
bool Init(const InitOptions &options=InitOptions())
std::shared_ptr< base::Image8U > ori_gray_
std::shared_ptr< base::Image8U > gray_
bool GetImageBlob(const ImageOptions &options, base::Blob< uint8_t > *blob)
std::shared_ptr< base::Image8U > ori_bgr_
std::shared_ptr< base::Image8U > bgr_
std::shared_ptr< base::Image8U > ori_rgb_
bool GetImage(const ImageOptions &options, base::Image8U *image)
std::shared_ptr< UndistortionHandler > handler_
std::shared_ptr< base::Image8U > rgb_
bool FillImageData(int rows, int cols, const uint8_t *data, const std::string &encoding)
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
const std::map< Color, int > kChannelsMap
Definition image_8u.h:35
bool imageToBlob(const base::Image8U &image, base::Blob< uint8_t > *blob)
bool imageToGray(const base::Image8UPtr &src, const base::Image8UPtr &dst, const int src_width, const int src_height, const float coeffs[3])
bool swapImageChannels(const base::Image8UPtr &src, const base::Image8UPtr &dst, const int src_width, const int src_height, const int order[3])
bool dupImageChannels(const base::Image8UPtr &src, const base::Image8UPtr &dst, const int src_width, const int src_height)
class register implement
Definition arena_queue.h:37