Perception: YoloObsacleDetectorRegisterer macro is not executed in the Offline Obstacle Detector Tool
Describe the bug The perception module has an offline tool for obstacle detection in
modules/perception/camera/tools/offline/offline_obstacle_detector.cc
Building and running this file gives the following error:
E0226 20:43:18.229156 15844 base_obstacle_detector.h:60] [] Get instance YoloObstacleDetector failed.
F0226 20:43:18.229164 15844 obstacle_camera_perception.cc:85] Check failed: name_detector_map_.at(detector_param.camera_name()) != nullptr
*** Check failure stack trace: ***
We have stepped into the code to find the macro PERCEPTION_REGISTER_REGISTERER which should be executed by components that implement interfaces, such as BaseCameraPerception and its Yolo implementation class YoloObstalceDetector
There are many working examples of this in unit tests such as this.
We have tried including the header file into the offline_obstacle_pipeline.cc, then bazel clean and re-build, but still seeing the same error.
We are wondering if it is a misunderstanding on our part of how this should work?
Related issue #6656
@snuffysasa Where you able to get this to work?
To Reproduce Steps to reproduce the behavior:
user@in_dev_docker:/apollo$ bazel run //modules/perception/camera/tools/offline:offline_obstacle_pipeline
Expected behavior
Popup windows with bounding boxes of detected obstacles in test images in the testdata directory
Screenshots

Desktop (please complete the following information):
- Host OS (Ubuntu 18.04)
- Latest Dev Docker Container
- Nvidia Driver installed and fully tested on other apollo code
These debug messages are from master branch
E0226 22:47:03.748994 3606 offline_obstacle_pipeline.cc:69] [] config_root: /apollo/modules/perception/production/conf/perception/camera/
E0226 22:47:03.749120 3606 offline_obstacle_pipeline.cc:71] [] config_file: obstacle.pt
The obstacle.pt file is where the YoloObstalceDetector plugin param is defined. However, the code is trying to get an instance of ObstacleCameraPerception which has a different base class from YoloObstalceDetector.
They are two different types completely! Has this never worked, or am I missing something?
I have the same error. Problem is not reproduce if I try to run offline tool for obstacle detection as gtest.
@aboarya @kkhitsko Please see this PR. #7142 (Please also pull the latest master)
@KaWaiTsoiBaidu thanks! I eventually got it to work last week. It was a missunderstanding on my part.
What solved it for me was adding linkstatic=False to the BUILD files in both tools directories.
If my understanding is right, the linkstatic=False is required since the __attribute__ ((constructor)) on RegisterFactory##name would only be called if that library is loaded as a shared object and not statically linked, which is the default for cc_binary in bazel.
From bazel docs
For cc_binary and cc_test: link the binary in static mode. For cc_library.linkstatic: see below. By default this option is on for cc_binary and off for the rest.
@kkhitsko The reason it works under gtest is because it's loaded as a shared object. You can inspect this in the:
<docker-container>/bazel-out/bin/modules/perception/camera/test/camera_lib_obstacle_detector_yolo_yolo_obstacle_detector_test-2.params
There were a number of other missing files and symlinks. I will pull latest master and try again and report here.
Closing this ... Thanks again!
@aboarya You are right. Another way to make these __attribute__((constructor)) to run when statically linked is to explicitly call them, as in #7142. However, your solution is cleaner in this case.
Also, the missing files and symlinks have been fixed in this PR.