@@ -214,11 +214,16 @@ deep_ros::CallbackReturn DeepObjectDetectionNode::on_configure_impl(const rclcpp
214214 class_names_,
215215 use_letterbox);
216216
217- auto pub = this ->create_publisher <Detection2DArrayMsg>(params_.output_detections_topic , rclcpp::SensorDataQoS{});
217+ // best_effort avoids blocking on slow subscribers and provides better performance for high-frequency sensor data
218+ auto detection_qos = rclcpp::QoS (rclcpp::KeepLast (1 ));
219+ detection_qos.best_effort ();
220+ auto pub = this ->create_publisher <Detection2DArrayMsg>(params_.output_detections_topic , detection_qos);
218221 detection_pub_ = std::static_pointer_cast<rclcpp_lifecycle::LifecyclePublisher<Detection2DArrayMsg>>(pub);
219222
223+ auto marker_qos = rclcpp::QoS (rclcpp::KeepLast (1 ));
224+ marker_qos.best_effort ();
220225 auto marker_pub =
221- this ->create_publisher <visualization_msgs::msg::ImageMarker>(output_annotations_topic_, rclcpp::SensorDataQoS{} );
226+ this ->create_publisher <visualization_msgs::msg::ImageMarker>(output_annotations_topic_, marker_qos );
222227 image_marker_pub_ =
223228 std::static_pointer_cast<rclcpp_lifecycle::LifecyclePublisher<visualization_msgs::msg::ImageMarker>>(marker_pub);
224229
@@ -467,7 +472,7 @@ void DeepObjectDetectionNode::processImages(
467472 return ;
468473 }
469474
470- auto start_time = std::chrono::steady_clock::now ();
475+ auto total_start_time = std::chrono::steady_clock::now ();
471476 std::vector<cv::Mat> processed;
472477 std::vector<ImageMeta> metas;
473478 std::vector<std_msgs::msg::Header> processed_headers;
@@ -504,28 +509,45 @@ void DeepObjectDetectionNode::processImages(
504509 return ;
505510 }
506511
507- // Build input tensor
512+ // Build input tensor and copy to device
508513 deep_ros::Tensor input_tensor (packed_input.shape , deep_ros::DataType::FLOAT32, allocator);
509514 const size_t bytes = packed_input.data .size () * sizeof (float );
510515 allocator->copy_from_host (input_tensor.data (), packed_input.data .data (), bytes);
511516
512517 // Run inference
518+ auto inference_start = std::chrono::steady_clock::now ();
513519 auto output_tensor = run_inference (input_tensor);
520+ auto inference_end = std::chrono::steady_clock::now ();
521+ auto inference_ms = std::chrono::duration_cast<std::chrono::milliseconds>(inference_end - inference_start).count ();
522+
523+ // Postprocess
514524 std::vector<std::vector<SimpleDetection>> batch_detections = postprocessor_->decode (output_tensor, metas);
515525
516- auto end_time = std::chrono::steady_clock::now ();
517- auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count ();
526+ auto total_end_time = std::chrono::steady_clock::now ();
527+ auto total_ms = std::chrono::duration_cast<std::chrono::milliseconds>(total_end_time - total_start_time).count ();
528+
529+ size_t total_detections_count =
530+ std::accumulate (batch_detections.begin (), batch_detections.end (), size_t (0 ), [](size_t sum, const auto & dets) {
531+ return sum + dets.size ();
532+ });
533+
534+ // Build per-image detection count string for logging
535+ std::string per_image_counts;
536+ for (size_t i = 0 ; i < batch_detections.size (); ++i) {
537+ if (i > 0 ) per_image_counts += " , " ;
538+ per_image_counts += std::to_string (batch_detections[i].size ());
539+ }
518540
519541 RCLCPP_INFO_THROTTLE (
520542 this ->get_logger (),
521543 *this ->get_clock (),
522544 2000 ,
523- " Image processing completed: %zu images in %" PRId64 " ms, total detections: %zu " ,
545+ " Processing %zu images: total=% " PRId64 " ms (inference= %" PRId64 " ms), detections=%zu [per image : %s] " ,
524546 processed.size (),
525- static_cast <int64_t >(elapsed_ms ),
526- std::accumulate (batch_detections. begin (), batch_detections. end (), size_t ( 0 ), []( size_t sum, const auto & dets) {
527- return sum + dets. size ();
528- } ));
547+ static_cast <int64_t >(total_ms ),
548+ static_cast < int64_t >(inference_ms),
549+ total_detections_count,
550+ per_image_counts. c_str ( ));
529551
530552 publishDetections (batch_detections, processed_headers, metas);
531553}
@@ -545,33 +567,50 @@ void DeepObjectDetectionNode::publishDetections(
545567 return ;
546568 }
547569
548- size_t total_published = 0 ;
570+ size_t total_detections = 0 ;
571+ size_t total_messages = 0 ;
572+
573+ // Publish all messages as quickly as possible without blocking
549574 for (size_t i = 0 ; i < batch_detections.size () && i < headers.size () && i < metas.size (); ++i) {
550- Detection2DArrayMsg msg;
551- postprocessor_->fillDetectionMessage (headers[i], batch_detections[i], metas[i], msg);
552- detection_pub_->publish (msg);
553- total_published += batch_detections[i].size ();
575+ const size_t num_dets = batch_detections[i].size ();
576+
577+ try {
578+ if (num_dets == 0 ) {
579+ // Still publish empty messages to maintain frame sync
580+ Detection2DArrayMsg msg;
581+ msg.header = headers[i];
582+ msg.detections .clear ();
583+ detection_pub_->publish (msg);
584+ total_messages++;
585+ continue ;
586+ }
554587
555- RCLCPP_DEBUG (
556- this ->get_logger (),
557- " Published [%zu]: frame_id=%s, %zu detections" ,
558- i,
559- headers[i].frame_id .c_str (),
560- batch_detections[i].size ());
588+ total_detections += num_dets;
589+ total_messages++;
561590
562- // Also publish ImageMarker annotations if publisher is available
563- if (image_marker_pub_) {
564- auto marker_msg = detectionsToImageMarker (headers[i], batch_detections[i]);
565- image_marker_pub_->publish (marker_msg);
591+ Detection2DArrayMsg msg;
592+ postprocessor_->fillDetectionMessage (headers[i], batch_detections[i], metas[i], msg);
593+ detection_pub_->publish (msg);
594+
595+ // Also publish ImageMarker annotations if publisher is available
596+ if (image_marker_pub_) {
597+ auto marker_msg = detectionsToImageMarker (headers[i], batch_detections[i]);
598+ image_marker_pub_->publish (marker_msg);
599+ }
600+ } catch (const std::exception & e) {
601+ RCLCPP_ERROR_THROTTLE (
602+ this ->get_logger (), *this ->get_clock (), 1000 , " Exception while publishing detection [%zu]: %s" , i, e.what ());
603+ // Continue with next detection instead of stopping
566604 }
567605 }
606+
568607 RCLCPP_INFO_THROTTLE (
569608 this ->get_logger (),
570609 *this ->get_clock (),
571610 2000 ,
572- " Published %zu detection messages (%zu total detections)" ,
573- batch_detections. size () ,
574- total_published );
611+ " Published %zu detection messages (%zu total detections across all images )" ,
612+ total_messages ,
613+ total_detections );
575614}
576615
577616void DeepObjectDetectionNode::loadClassNames ()
0 commit comments