1414 * limitations under the License.
1515 *****************************************************************************/
1616#include " modules/perception/camera/lib/obstacle/detector/yolo/yolo_obstacle_detector.h"
17+ #include < boost/algorithm/string.hpp>
1718
1819#include " cyber/common/file.h"
1920#include " cyber/common/log.h"
@@ -37,24 +38,30 @@ void YoloObstacleDetector::LoadInputShape(const yolo::ModelParam &model_param) {
3738 int resized_width = model_param.resized_width ();
3839 int aligned_pixel = model_param.aligned_pixel ();
3940 // inference input shape
40- int image_height = static_cast <int >(base_camera_model_->get_height ());
41- int image_width = static_cast <int >(base_camera_model_->get_width ());
42-
43- offset_y_ =
44- static_cast <int >(offset_ratio * static_cast <float >(image_height) + .5f );
45- float roi_ratio = cropped_ratio * static_cast <float >(image_height) /
46- static_cast <float >(image_width);
47- width_ = static_cast <int >(resized_width + aligned_pixel / 2 ) / aligned_pixel *
48- aligned_pixel;
49- height_ = static_cast <int >(static_cast <float >(width_) * roi_ratio +
50- static_cast <float >(aligned_pixel) / 2 .0f ) /
51- aligned_pixel * aligned_pixel;
52-
53- AINFO << " image_height=" << image_height << " , "
54- << " image_width=" << image_width << " , "
55- << " roi_ratio=" << roi_ratio;
56- AINFO << " offset_y=" << offset_y_ << " , height=" << height_
57- << " , width=" << width_;
41+ // TODO: need to optimization
42+ for (size_t i = 0 ; i < camera_names_.size (); ++i) {
43+ base_camera_model_ =
44+ name_basemodel_map_.at (camera_names_[i]);
45+ int image_height = static_cast <int >(base_camera_model_->get_height ());
46+ int image_width = static_cast <int >(base_camera_model_->get_width ());
47+
48+ offset_y_ =
49+ static_cast <int >(offset_ratio * static_cast <float >(image_height) + .5f );
50+ float roi_ratio = cropped_ratio * static_cast <float >(image_height) /
51+ static_cast <float >(image_width);
52+ width_ = static_cast <int >(resized_width + aligned_pixel / 2 ) / aligned_pixel *
53+ aligned_pixel;
54+ height_ = static_cast <int >(static_cast <float >(width_) * roi_ratio +
55+ static_cast <float >(aligned_pixel) / 2 .0f ) /
56+ aligned_pixel * aligned_pixel;
57+ offset_y_map_.insert (
58+ std::pair<std::string, int >(camera_names_[i], offset_y_));
59+ AINFO << " image_height=" << image_height << " , "
60+ << " image_width=" << image_width << " , "
61+ << " roi_ratio=" << roi_ratio;
62+ AINFO << " offset_y=" << offset_y_ << " , height=" << height_
63+ << " , width=" << width_;
64+ }
5865}
5966
6067void YoloObstacleDetector::LoadParam (const yolo::YoloParam &yolo_param) {
@@ -300,17 +307,37 @@ bool YoloObstacleDetector::Init(const StageConfig& stage_config) {
300307 return false ;
301308 }
302309
303- yolo_obstacle_detector_config_ = stage_config.yolo_obstacle_detector_config ();
310+ ACHECK (stage_config.has_camera_detector_config ());
311+ auto yolo_obstacle_detector_config_ =
312+ stage_config.camera_detector_config ();
313+
304314 gpu_id_ = yolo_obstacle_detector_config_.gpu_id ();
305315 BASE_CUDA_CHECK (cudaSetDevice (gpu_id_));
306316 BASE_CUDA_CHECK (cudaStreamCreate (&stream_));
307317
308- base_camera_model_ =
309- common::SensorManager::Instance ()->GetUndistortCameraModel (
310- yolo_obstacle_detector_config_.camera_name ());
311- ACHECK (base_camera_model_ != nullptr ) << " base_camera_model is nullptr!" ;
318+ std::string camera_name =
319+ yolo_obstacle_detector_config_.camera_name ();
320+ boost::algorithm::split (camera_names_, camera_name,
321+ boost::algorithm::is_any_of (" ," ));
322+
323+ for (size_t i = 0 ; i < camera_names_.size (); ++i) {
324+ std::shared_ptr<base::BaseCameraModel> base_model_ptr =
325+ common::SensorManager::Instance ()->GetUndistortCameraModel (
326+ camera_names_[i]);
327+ name_basemodel_map_.insert (
328+ std::pair<std::string, std::shared_ptr<base::BaseCameraModel>>(
329+ camera_names_[i], base_model_ptr));
330+ ACHECK (base_model_ptr != nullptr ) << " base_camera_model is nullptr!" ;
331+ }
332+
333+ std::string config_path =
334+ GetAbsolutePath (yolo_obstacle_detector_config_.root_dir (),
335+ yolo_obstacle_detector_config_.conf_file ());
336+ if (!cyber::common::GetProtoFromFile (config_path, &yolo_param_)) {
337+ AERROR << " read proto_config fail" ;
338+ return false ;
339+ }
312340
313- yolo_param_ = yolo_obstacle_detector_config_.yolo_param ();
314341 const auto &model_param = yolo_param_.model_param ();
315342 // todo(zero): options.root_dir
316343 std::string root_dir = yolo_obstacle_detector_config_.root_dir ();
@@ -347,6 +374,78 @@ bool YoloObstacleDetector::Init(const StageConfig& stage_config) {
347374}
348375
349376bool YoloObstacleDetector::Process (DataFrame* data_frame) {
377+ if (data_frame == nullptr ) {
378+ return false ;
379+ }
380+ auto frame = data_frame->camera_frame ;
381+
382+ Timer timer;
383+ if (cudaSetDevice (gpu_id_) != cudaSuccess) {
384+ AERROR << " Failed to set device to " << gpu_id_;
385+ return false ;
386+ }
387+
388+ auto input_blob = inference_->get_blob (yolo_param_.net_param ().input_blob ());
389+ AINFO << " Start: " << static_cast <double >(timer.Toc ()) * 0.001 << " ms" ;
390+ DataProvider::ImageOptions image_options;
391+ base_camera_model_ =
392+ name_basemodel_map_.at (frame->data_provider ->sensor_name ());
393+ offset_y_ = offset_y_map_.at (frame->data_provider ->sensor_name ());
394+ image_options.target_color = base::Color::BGR;
395+ image_options.crop_roi = base::RectI (
396+ 0 , offset_y_, static_cast <int >(base_camera_model_->get_width ()),
397+ static_cast <int >(base_camera_model_->get_height ()) - offset_y_);
398+ image_options.do_crop = true ;
399+ frame->data_provider ->GetImage (image_options, image_.get ());
400+ AINFO << " GetImageBlob: " << static_cast <double >(timer.Toc ()) * 0.001 << " ms" ;
401+ inference::ResizeGPU (*image_, input_blob, frame->data_provider ->src_width (),
402+ 0 );
403+ AINFO << " Resize: " << static_cast <double >(timer.Toc ()) * 0.001 << " ms" ;
404+
405+ // ///////////////////////// detection part ///////////////////////////
406+ inference_->Infer ();
407+ AINFO << " Network Forward: " << static_cast <double >(timer.Toc ()) * 0.001
408+ << " ms" ;
409+ get_objects_cpu (yolo_blobs_, stream_, types_, nms_, yolo_param_.model_param (),
410+ light_vis_conf_threshold_, light_swt_conf_threshold_,
411+ overlapped_.get (), idx_sm_.get (), &(frame->detected_objects ));
412+
413+ AINFO << " GetObj: " << static_cast <double >(timer.Toc ()) * 0.001 << " ms" ;
414+ filter_bbox (min_dims_, &(frame->detected_objects ));
415+ FeatureExtractorOptions feat_options;
416+ feat_options.normalized = true ;
417+ AINFO << " Post1: " << static_cast <double >(timer.Toc ()) * 0.001 << " ms" ;
418+ feature_extractor_->Extract (feat_options, frame);
419+ AINFO << " Extract: " << static_cast <double >(timer.Toc ()) * 0.001 << " ms" ;
420+ recover_bbox (frame->data_provider ->src_width (),
421+ frame->data_provider ->src_height () - offset_y_, offset_y_,
422+ &frame->detected_objects );
423+
424+ // post processing
425+ int left_boundary =
426+ static_cast <int >(border_ratio_ * static_cast <float >(image_->cols ()));
427+ int right_boundary = static_cast <int >((1 .0f - border_ratio_) *
428+ static_cast <float >(image_->cols ()));
429+ for (auto &obj : frame->detected_objects ) {
430+ // recover alpha
431+ obj->camera_supplement .alpha /= ori_cycle_;
432+ // get area_id from visible_ratios
433+ if (yolo_param_.model_param ().num_areas () == 0 ) {
434+ obj->camera_supplement .area_id =
435+ get_area_id (obj->camera_supplement .visible_ratios );
436+ }
437+ // clear cut off ratios
438+ auto &box = obj->camera_supplement .box ;
439+ if (box.xmin >= left_boundary) {
440+ obj->camera_supplement .cut_off_ratios [2 ] = 0 ;
441+ }
442+ if (box.xmax <= right_boundary) {
443+ obj->camera_supplement .cut_off_ratios [3 ] = 0 ;
444+ }
445+ }
446+ AINFO << " Post2: " << static_cast <double >(timer.Toc ()) * 0.001 << " ms" ;
447+
448+ return true ;
350449 return true ;
351450}
352451
0 commit comments