Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Commit f2a0519

Browse files
PranavDhulipalaPranav Dhulipalalilustga
authored
Azure Custom Vision support (#16)
* Modified the node to enable building code using catkin_make * made the requested changes to the CMakeLists.txt and deleted the config files since they are no longer required * minor changes to the readme file * Added support to run trained onnx models from customvision.ai * Deleting onnx model * updated readme * adding dynamic reconfigure to switch onnx models at runtime * deleting redundant headers from main.cpp * moved the anchors to a yaml file and updated the readme * updated the readme * updating to onnx runtime version 1.7.0 for cpu and 1.7.1 for gpu * Updates to readme and camera node selection based on OS. (#17) * Updates to readme and camera node selection based on OS. * removed the engine block stl and added instructions to download. * Updated date. * README fixes for Linux * c_str to fix build error on linux. * Fixed link in README * Made path to STL explicit in README. * Switched to OTL cv_camera for linux and formatted link in README * Modified the node to enable building code using catkin_make * made the requested changes to the CMakeLists.txt and deleted the config files since they are no longer required * minor changes to the readme file * Added support to run trained onnx models from customvision.ai * Deleting onnx model * updated readme * adding dynamic reconfigure to switch onnx models at runtime * deleting redundant headers from main.cpp * moved the anchors to a yaml file and updated the readme * updated the readme * updating to onnx runtime version 1.7.0 for cpu and 1.7.1 for gpu * rebased with the latest commit, made changes in a few files Co-authored-by: Pranav Dhulipala <[email protected]> Co-authored-by: Lior Lustgarten <[email protected]>
1 parent 03116af commit f2a0519

File tree

13 files changed

+286
-215
lines changed

13 files changed

+286
-215
lines changed

README.md

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ catkin_make -DCUDA_SUPPORT=ON
6161

6262
## Running the samples
6363
There are two launch files included as samples in the launch folder.
64-
An object tracking demo and a deep pose detection demo.
64+
An object tracking demo and a deep pose detection demo.
6565

66-
### Person Tracker Demo
67-
`tracker.launch` demonstrates tracking people in images/video.
66+
### Object Tracker Demo
67+
`tracker.launch` demonstrates tracking of up to 20 classes icluding people in images/video.
6868

6969
To run the person tracking detection demo, source the workspace and then roslaunch the launch file.
7070

@@ -78,6 +78,15 @@ In another command prompt or terminal, run rviz and add the `/tracked_objects/im
7878
rosrun rviz rviz
7979
```
8080

81+
For a project trained using customvision.ai at runtime you can change the parameters using rqt_reconfigure.
82+
83+
```Batchfile
84+
rosrun rqt_reconfigure rqt_reconfigure
85+
```
86+
Please follow this order, start by placing the files of interest i.e. the onnx model(.onnx) file and labels.txt obtained from the relevant onnx zip file downloaded from customvision.ai to `ros_msft_onnx/testdata/` or a known location. Change the anchor values in the cfg/anchors.yaml file by commenting the first line and uncommenting the second line, finally update `input_node_name` to "data", `output_node_name` to "model_outputs0" and update any other relevant parameters shown below:
87+
88+
![Rqt Reconfigure](./ros_msft_onnx/testdata/rqt_reconfigure.PNG)
89+
8190
### Deep Pose Detection Demo
8291
`pose.launch` demonstrates estimating the position and rotation of an engine block from images\video.In preperation for running the engine pose demo:
8392
* Copy [Engine pose ONNX model](https://github.com/ms-iot/ros_msft_onnx_demo/releases/download/0.0/engine.onnx) to `ros_msft_onnx/testdata/`.
@@ -88,57 +97,27 @@ To run the engine pose detection demo, source the workspace and then roslaunch t
8897
roslaunch ros_msft_onnx pose.launch
8998
```
9099

91-
For your own project, you can create a launch file in the following format:
92-
93-
```xml
94-
<launch>
95-
<arg name="onnx_model_path_arg" default="$(find ros_msft_onnx)/testdata/model.onnx"/>
96-
<node pkg="ros_msft_onnx" type="ros_msft_onnx_node" name="ros_msft_onnx" output="screen">
97-
<param name="onnx_model_path" value="$(arg onnx_model_path_arg)"/>
98-
<param name="confidence" value="0.5"/>
99-
<param name="tensor_width" value="416"/>
100-
<param name="tensor_height" value="416"/>
101-
<param name="tracker_type" value="yolo"/>
102-
<param name="image_processing" value="resize"/>
103-
<param name="debug" value="true"/>
104-
<param name="image_topic" value="/camera/image_raw" />
105-
</node>
106-
107-
<!-- NOTE: The image properties need to be valid for the camera, or the node will auto select the closest values -->
108-
<node pkg="ros_msft_camera" type="ros_msft_camera_node" name="camera">
109-
<param name="camera_info_url" value="file://$(find ros_msft_camera)/config/default_calibration.yaml" />
110-
<param name="frame_id" value="camera" />
111-
<param name="image_width" value="1280" />
112-
<param name="image_height" value="720" />
113-
<param name="frame_rate" value="30.0" />
114-
</node>
115-
116-
<node pkg="tf" type="static_transform_publisher" name="onnx_link"
117-
args="0 -0.02 0 0 0 0 map base_link 100" />
118-
119-
</launch>
120-
```
121100

122101
> While 'Pose' processing is enabled, the service required to generate the model has not been published as of April 2021
123102
124103
## Property Descriptions
125104

126-
| Property | Description |
127-
|----------| ------------|
128-
| onnx_model_path | Path to the model.onnx file |
129-
| confidence | Minimum confidence before publishing an event. 0 to 1 |
130-
| tensor_width| The Width of the input to the model. |
131-
| tensor_height| The Height of the input to the model. |
132-
| tracker_type| Currently enabled - `yolo` or `pose`. |
133-
| image_processing| `resize`, `scale` or `crop` |
134-
| debug| `true` or `false` determines if a debug image is published |
135-
| image_topic| The image topic to subscribe to |
136-
| label | used to filter the found object to a specific label |
137-
| mesh_rotation| The orientation of the mesh when debug rendering pose |
138-
| mesh_scale| The scale of the mesh when debug rendering pose |
139-
| mesh_resource| The mesh used for debug rendering pose |
140-
| model_bounds| 9 coordinates used to perform the point in perspective caluclation for pose |
141-
| calibration | Path to the OpenCV calibration file for point in persective |
105+
| Property | Description |
106+
| ---------------- | --------------------------------------------------------------------------- |
107+
| onnx_model_path | Path to the model.onnx file |
108+
| confidence | Minimum confidence before publishing an event. 0 to 1 |
109+
| tensor_width | The Width of the input to the model. |
110+
| tensor_height | The Height of the input to the model. |
111+
| tracker_type | Currently enabled - `yolo` or `pose`. |
112+
| image_processing | `resize`, `scale` or `crop` |
113+
| debug | `true` or `false` determines if a debug image is published |
114+
| image_topic | The image topic to subscribe to |
115+
| label | used to filter the found object to a specific label |
116+
| mesh_rotation | The orientation of the mesh when debug rendering pose |
117+
| mesh_scale | The scale of the mesh when debug rendering pose |
118+
| mesh_resource | The mesh used for debug rendering pose |
119+
| model_bounds | 9 coordinates used to perform the point in perspective caluclation for pose |
120+
| calibration | Path to the OpenCV calibration file for point in persective |
142121

143122
## Subscriptions
144123
Onnx subscribes to the topic listed in the `image_topic` property, or `/camera/image_raw`
@@ -167,4 +146,4 @@ provided by the bot. You will only need to do this once across all repos using o
167146

168147
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
169148
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
170-
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
149+
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

ros_msft_onnx/CMakeLists.txt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@ find_package(Eigen3 REQUIRED)
2323

2424
find_package(OpenCV REQUIRED)
2525

26+
find_package(yaml-cpp CONFIG REQUIRED)
27+
2628
find_package(catkin REQUIRED COMPONENTS
2729
std_msgs
2830
geometry_msgs
2931
visualization_msgs
3032
ros_msft_onnx_msgs
3133
image_transport
34+
dynamic_reconfigure
35+
yaml-cpp
3236
roscpp
3337
cv_bridge
3438
tf
3539
)
3640

41+
generate_dynamic_reconfigure_options(
42+
cfg/reconfig.cfg
43+
)
44+
3745
catkin_package(
3846
INCLUDE_DIRS include
39-
CATKIN_DEPENDS std_msgs geometry_msgs visualization_msgs ros_msft_onnx_msgs image_transport roscpp cv_bridge tf
47+
CATKIN_DEPENDS std_msgs geometry_msgs visualization_msgs ros_msft_onnx_msgs image_transport dynamic_reconfigure roscpp cv_bridge tf
4048
CFG_EXTRAS "onnxruntime_vendor-extras.cmake"
4149
)
4250

@@ -47,19 +55,19 @@ include_directories(
4755
)
4856

4957
add_executable(${PROJECT_NAME}_node src/ros_msft_onnx.cpp src/main.cpp src/yolo_box.cpp src/pose_parser.cpp)
50-
add_dependencies(${PROJECT_NAME}_node ${catkin_EXPORTED_TARGETS})
51-
target_link_libraries(${PROJECT_NAME}_node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} ${EIGEN3_LIBS})
58+
add_dependencies(${PROJECT_NAME}_node ${PROJECT_NAME}_gencfg ${catkin_EXPORTED_TARGETS})
59+
target_link_libraries(${PROJECT_NAME}_node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} ${EIGEN3_LIBS} ${YAML_CPP_LIBRARIES})
5260

5361
message("Installing onnxruntime_vendor Nuget package")
5462

5563
if(CUDA_SUPPORT)
56-
set(ONNX_RUNTIME "Microsoft.ML.OnnxRuntime.Gpu.1.4.0")
57-
set(PACKAGE_URL "https://www.nuget.org/api/v2/package/Microsoft.ML.OnnxRuntime.Gpu/1.4.0")
58-
set(PACKAGE_SHA512 "c9c2ba5c594c92c1e426e9c53f9909e8851a41c99f48f8a369e082f8047d521b236f2fbb943e73975cbb45bd9957f20139c25959e50e1679dca9eeac08f73b31")
64+
set(ONNX_RUNTIME "Microsoft.ML.OnnxRuntime.Gpu.1.7.1")
65+
set(PACKAGE_URL "https://www.nuget.org/api/v2/package/Microsoft.ML.OnnxRuntime.Gpu/1.7.1")
66+
set(PACKAGE_SHA512 "41112118007aae34fcc38100152df6e6fa5fc567e61aa4ded42a26d39751f1be7ec225c0d73799f065015e284f0fb9bd7e0835c733e9abad5b0243a391411f8d")
5967
else()
60-
set(ONNX_RUNTIME "Microsoft.ML.OnnxRuntime.1.4.0")
61-
set(PACKAGE_URL "https://www.nuget.org/api/v2/package/Microsoft.ML.OnnxRuntime/1.4.0")
62-
set(PACKAGE_SHA512 "cbff106bb1f114ee1d510b594abb487e9f965b7b7a3f37b92846013eb086126a4cd69eb4564717fe1acf04d4399d1fcd0a52c3ca508f330e91a3e5d0fe560ca3")
68+
set(ONNX_RUNTIME "Microsoft.ML.OnnxRuntime.1.7.0")
69+
set(PACKAGE_URL "https://www.nuget.org/api/v2/package/Microsoft.ML.OnnxRuntime/1.7.0")
70+
set(PACKAGE_SHA512 "1fc15386bdfa455f457e50899e3c9c454aafbdc345799dcf4ecfd6990a9dbd8cd7f0b1f3bf412c47c900543c535f95aa1cb1e14e9851cb9b600c60a981f38a50")
6371
endif()
6472

6573
file(DOWNLOAD
@@ -95,8 +103,8 @@ if(MSVC)
95103
configure_file(${CMAKE_CURRENT_BINARY_DIR}/${ONNX_RUNTIME}/runtimes/${ARCH}/native/onnxruntime.lib ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/onnxruntime.lib COPYONLY)
96104
configure_file(${CMAKE_CURRENT_BINARY_DIR}/${ONNX_RUNTIME}/runtimes/${ARCH}/native/onnxruntime.pdb ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/onnxruntime.pdb COPYONLY)
97105
else()
98-
configure_file(${CMAKE_CURRENT_BINARY_DIR}/${ONNX_RUNTIME}/runtimes/${ARCH}/native/libonnxruntime.so ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}/libonnxruntime.so.1.4.0 COPYONLY)
99-
target_link_libraries(${PROJECT_NAME}_node ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}/libonnxruntime.so.1.4.0)
106+
target_link_libraries(${PROJECT_NAME}_node ${CMAKE_CURRENT_BINARY_DIR}/${ONNX_RUNTIME}/runtimes/${ARCH}/native/libonnxruntime.so)
107+
configure_file(${CMAKE_CURRENT_BINARY_DIR}/${ONNX_RUNTIME}/runtimes/${ARCH}/native/libonnxruntime.so ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/libonnxruntime.so COPYONLY)
100108
endif()
101109

102110
# The node expects to use the Tiny YOLO model available in the ONNX model zoo.
@@ -106,4 +114,4 @@ file(DOWNLOAD
106114
${CMAKE_CURRENT_SOURCE_DIR}/testdata/model.onnx
107115
SHOW_PROGRESS
108116
)
109-
endif()
117+
endif()

ros_msft_onnx/cfg/anchors.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
anchors: [1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52]
2+
# anchors: [0.573, 0.677, 1.87, 2.06, 3.34, 5.47, 7.88, 3.53, 9.77, 9.17]

ros_msft_onnx/cfg/reconfig.cfg

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
PACKAGE = "ros_msft_onnx"
3+
4+
from dynamic_reconfigure.parameter_generator_catkin import *
5+
import os
6+
7+
os.chdir('../../../')
8+
path = os.getcwd()
9+
10+
newPath = path.replace(os.sep, '/') + "/src/ros_msft_onnx/ros_msft_onnx/"
11+
gen = ParameterGenerator()
12+
13+
gen.add("tensor_height", int_t, 0, "The height of the tensor", 416, 0, 3072)
14+
gen.add("tensor_width", int_t, 0, "The width of the tensor", 416, 0, 4096)
15+
gen.add("confidence", double_t, 0, "The confidence parameter", .5, 0, 1)
16+
gen.add("onnx_model_path", str_t, 0, "The path of the onnx model", newPath + "testdata/model.onnx")
17+
gen.add("onnx_label_path", str_t, 0, "The path of the class labels", newPath + "testdata/labels.txt")
18+
gen.add("anchors_path", str_t, 0, "The path of the anchors.yaml file", newPath + "cfg/anchors.yaml")
19+
gen.add("tracker_type", str_t, 0, "Name of the tracker", "yolo")
20+
gen.add("image_processing", str_t, 0, "Changes the image processing type", "resize")
21+
gen.add("input_node_name", str_t, 0, "Name of the input for onnx model", "image")
22+
gen.add("output_node_name", str_t, 0, "Name of the output for onnx model", "grid")
23+
gen.add("debug", bool_t, 0, "Sets the debug value", True)
24+
25+
exit(gen.generate(PACKAGE, "ros_msft_onnx", "reconfig"))
Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#pragma once
22

33
#include <onnxruntime_cxx_api.h>
4-
4+
#include <dynamic_reconfigure/server.h>
5+
#include <ros_msft_onnx/reconfigConfig.h>
6+
#include <yaml-cpp/yaml.h>
57
class OnnxProcessor
68
{
79
public:
@@ -39,65 +41,43 @@ class OnnxProcessor
3941

4042
std::string _linkName;
4143
std::string _onnxModel;
42-
std::string _calibration;
43-
44-
cv::Mat _camera_matrix;
45-
cv::Mat _dist_coeffs;
4644

47-
float _confidence;
48-
49-
bool _debug;
50-
bool _normalize;
51-
52-
ros::Publisher _detect_pub;
53-
image_transport::Publisher _image_pub;
54-
image_transport::Publisher _debug_image_pub;
55-
image_transport::Subscriber _cameraSub;
56-
/* TODO (lilustga): remove this
57-
bool _fake;
58-
winrt::hstring _inName;
59-
winrt::hstring _outName;
60-
std::string frame_id_;
61-
std::string _onnxModel;
45+
std::string _imageProcessingType;
6246
std::string _calibration;
6347

6448
cv::Mat _camera_matrix;
6549
cv::Mat _dist_coeffs;
6650

67-
6851
float _confidence;
6952

7053
bool _debug;
7154
bool _normalize;
7255

73-
uint _tensorWidth;
74-
uint _tensorHeight;
75-
76-
int _channelCount;
77-
int _rowCount;
78-
int _colCount;
79-
winrt::Windows::AI::MachineLearning::LearningModel _model = nullptr;
80-
winrt::Windows::AI::MachineLearning::LearningModelSession _session = nullptr;
8156

8257
ros::Publisher _detect_pub;
8358
image_transport::Publisher _image_pub;
8459
image_transport::Publisher _debug_image_pub;
8560
image_transport::Subscriber _cameraSub;
86-
*/
8761

8862
};
8963

9064
class OnnxTracker
9165
{
9266
ros::NodeHandle _nh;
9367
ros::NodeHandle _nhPrivate;
68+
dynamic_reconfigure::Server<ros_msft_onnx::reconfigConfig> server;
69+
dynamic_reconfigure::Server<ros_msft_onnx::reconfigConfig>::CallbackType f;
70+
bool _status;
9471

9572
std::shared_ptr<OnnxProcessor> _processor;
9673

9774
public:
9875
OnnxTracker() { };
9976

10077
bool init(ros::NodeHandle& nh, ros::NodeHandle& nhPrivate);
78+
void callback(ros_msft_onnx::reconfigConfig &config, uint32_t level);
79+
void startProcessor(ros_msft_onnx::reconfigConfig &config);
80+
void stopProcessor();
10181
bool shutdown();
10282
};
10383

ros_msft_onnx/include/ros_msft_onnx/yolo_box.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77

88
namespace yolo
99
{
10-
struct YoloInitOptions
11-
{
12-
std::string modelFullPath;
13-
};
14-
1510
struct YoloBox
1611
{
1712
public:
@@ -21,7 +16,15 @@ namespace yolo
2116

2217
class YoloProcessor : public OnnxProcessor
2318
{
24-
std::string _label;
19+
std::string _labelPath;
20+
std::vector<float> _anchors;
21+
std::vector<std::string> _labels;
22+
std::string _anchorsPath;
23+
int _class_count;
24+
int _row_count;
25+
int _col_count;
26+
std::string _inputName;
27+
std::string _outputName;
2528
public:
2629
YoloProcessor();
2730

@@ -30,7 +33,7 @@ namespace yolo
3033
std::vector<YoloBox> GetRecognizedObjects(std::vector<float> modelOutputs, float threshold = 0.3f);
3134
virtual void ProcessOutput(std::vector<float> output, cv::Mat& image);
3235
private:
33-
static int GetOffset(int x, int y, int channel);
36+
int GetOffset(int x, int y, int channel);
3437
static float IntersectionOverUnion(YoloBox a, YoloBox b);
3538
static float Sigmoid(float value);
3639
static void Softmax(std::vector<float> &values);

ros_msft_onnx/launch/tracker.launch

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
<launch>
2-
<arg name="onnx_model_path_arg" default="$(find ros_msft_onnx)/testdata/model.onnx"/>
32
<arg name="os_windows_arg" value="$(eval 'false' if not optenv('OS', 'unknown').lower().startswith('windows') else 'true')" />
43

54
<node pkg="ros_msft_onnx" type="ros_msft_onnx_node" name="ros_msft_onnx" output="screen">
6-
<param name="onnx_model_path" value="$(arg onnx_model_path_arg)"/>
7-
<param name="confidence" value="0.5"/>
8-
<param name="tensor_width" value="416"/>
9-
<param name="tensor_height" value="416"/>
10-
<param name="tracker_type" value="yolo"/>
11-
<param name="image_processing" value="resize"/>
125
<param name="image_topic" value="$(eval '/camera/image_raw' if os_windows_arg else '/cv_camera/image_raw')"/>
13-
<param name="debug" value="true"/>
146
</node>
157

168
<!-- The camera node will be selected based on os. ros_msft_camera for Windows and cv_camera for others. -->

ros_msft_onnx/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<depend>cv_bridge</depend>
1717
<depend>roscpp</depend>
1818
<depend>tf</depend>
19+
<depend>dynamic_reconfigure</depend>
1920
<depend>image_transport</depend>
2021
<depend>onnxruntime_vendor</depend>
2122

ros_msft_onnx/src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ int main(int argc, char **argv)
2424
ros::NodeHandle nhPrivate("~");
2525

2626
OnnxTracker tracker;
27+
2728

2829
if (tracker.init(nh, nhPrivate))
2930
{
@@ -37,4 +38,6 @@ int main(int argc, char **argv)
3738
{
3839
return 1;
3940
}
41+
42+
4043
}

0 commit comments

Comments
 (0)