Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ros_deep_learning/src/node_imagenet.cpp at master · dusty-nv/ros_deep_learning · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
dusty-nv
ros_deep_learning
Repository navigation
Code
Issues
73
(73)
Pull requests
20
(20)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
ros_deep_learning
/
src
/
node_imagenet.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
288 lines (220 loc) · 7.76 KB
master
Breadcrumbs
ros_deep_learning
/
src
/
node_imagenet.cpp
Copy path
Top
File metadata and controls
Code
Blame
288 lines (220 loc) · 7.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#
include
"
ros_compat.h
"
#
include
"
image_converter.h
"
#
include
<
jetson-inference/imageNet.h
>
#
include
<
jetson-utils/cudaFont.h
>
#
include
<
unordered_map
>
//
globals
imageNet* net =
NULL
;
cudaFont* font =
NULL
;
imageConverter* input_cvt =
NULL
;
imageConverter* overlay_cvt =
NULL
;
Publisher<vision_msgs::Classification2D> classify_pub =
NULL
;
Publisher<sensor_msgs::Image> overlay_pub =
NULL
;
Publisher<vision_msgs::VisionInfo> info_pub =
NULL
;
vision_msgs::VisionInfo info_msg;
//
triggered when a new subscriber connected
void
info_callback
()
{
ROS_INFO
(
"
new subscriber connected to vision_info topic, sending VisionInfo msg
"
);
info_pub->
publish
(info_msg);
}
//
publish overlay image
bool
publish_overlay
(
int
img_class,
float
confidence )
{
//
create font for image overlay
font =
cudaFont::Create
();
if
( !font )
{
ROS_ERROR
(
"
failed to load font for overlay
"
);
return
false
;
}
//
get the image dimensions
const
uint32_t
width = input_cvt->
GetWidth
();
const
uint32_t
height = input_cvt->
GetHeight
();
//
assure correct image size
if
( !overlay_cvt->
Resize
(width, height, imageConverter::ROSOutputFormat) )
return
false
;
//
generate the overlay
char
str[
256
];
sprintf
(str,
"
%05.2f%% %s
"
, confidence *
100
.
0f
, net->
GetClassDesc
(img_class));
font->
OverlayText
(input_cvt->
ImageGPU
(), width, height,
str,
5
,
5
,
make_float4
(
255
,
255
,
255
,
255
),
make_float4
(
0
,
0
,
0
,
100
));
//
populate the message
sensor_msgs::Image msg;
if
( !overlay_cvt->
Convert
(msg, imageConverter::ROSOutputFormat, input_cvt->
ImageGPU
()) )
return
false
;
//
populate timestamp in header field
msg.
header
.
stamp
=
ROS_TIME_NOW
();
//
publish the message
overlay_pub->
publish
(msg);
ROS_DEBUG
(
"
publishing %ux%u overlay image
"
, width, height);
}
//
triggered when recieved a new image on input topic
void
img_callback
(
const
sensor_msgs::ImageConstPtr input )
{
//
convert the image to reside on GPU
if
( !input_cvt || !input_cvt->
Convert
(input) )
{
ROS_INFO
(
"
failed to convert %ux%u %s image
"
, input->
width
, input->
height
, input->
encoding
.
c_str
());
return
;
}
//
classify the image
float
confidence =
0
.
0f
;
const
int
img_class = net->
Classify
(input_cvt->
ImageGPU
(), input_cvt->
GetWidth
(), input_cvt->
GetHeight
(), &confidence);
//
verify the output
if
( img_class >=
0
)
{
ROS_INFO
(
"
classified image, %f %s (class=%i)
"
, confidence, net->
GetClassDesc
(img_class), img_class);
//
create the classification message
vision_msgs::Classification2D msg;
vision_msgs::ObjectHypothesis obj;
#
if
ROS_DISTRO >= ROS_GALACTIC
obj.
class_id
= img_class;
#
else
obj.
id
= img_class;
#
endif
obj.
score
= confidence;
msg.
results
.
push_back
(obj);
//
TODO optionally add source image to msg
//
populate timestamp in header field
msg.
header
.
stamp
=
ROS_TIME_NOW
();
//
publish the classification message
classify_pub->
publish
(msg);
//
generate the overlay (if there are subscribers)
if
(
ROS_NUM_SUBSCRIBERS
(overlay_pub) >
0
)
publish_overlay
(img_class, confidence);
}
else
{
//
an error occurred if the output class is < 0
ROS_ERROR
(
"
failed to classify %ux%u image
"
, input->
width
, input->
height
);
}
}
//
node main loop
int
main
(
int
argc,
char
**argv)
{
/*
* create node instance
*/
ROS_CREATE_NODE
(
"
imagenet
"
);
/*
* declare parameters
*/
std::string model_name =
"
googlenet
"
;
std::string model_path;
std::string prototxt_path;
std::string class_labels_path;
std::string input_blob =
IMAGENET_DEFAULT_INPUT
;
std::string output_blob =
IMAGENET_DEFAULT_OUTPUT
;
ROS_DECLARE_PARAMETER
(
"
model_name
"
, model_name);
ROS_DECLARE_PARAMETER
(
"
model_path
"
, model_path);
ROS_DECLARE_PARAMETER
(
"
prototxt_path
"
, prototxt_path);
ROS_DECLARE_PARAMETER
(
"
class_labels_path
"
, class_labels_path);
ROS_DECLARE_PARAMETER
(
"
input_blob
"
, input_blob);
ROS_DECLARE_PARAMETER
(
"
output_blob
"
, output_blob);
/*
* retrieve parameters
*/
ROS_GET_PARAMETER
(
"
model_name
"
, model_name);
ROS_GET_PARAMETER
(
"
model_path
"
, model_path);
ROS_GET_PARAMETER
(
"
prototxt_path
"
, prototxt_path);
ROS_GET_PARAMETER
(
"
class_labels_path
"
, class_labels_path);
ROS_GET_PARAMETER
(
"
input_blob
"
, input_blob);
ROS_GET_PARAMETER
(
"
output_blob
"
, output_blob);
/*
* load image recognition network
*/
if
( model_path.
size
() >
0
)
{
//
create network using custom model paths
net =
imageNet::Create
(prototxt_path.
c_str
(), model_path.
c_str
(),
NULL
, class_labels_path.
c_str
(),
input_blob.
c_str
(), output_blob.
c_str
());
}
else
{
//
create network using the built-in model
net =
imageNet::Create
(model_name.
c_str
());
}
if
( !net )
{
ROS_ERROR
(
"
failed to load imageNet model
"
);
return
0
;
}
/*
* create the class labels parameter vector
*/
std::hash<std::string> model_hasher;
//
hash the model path to avoid collisions on the param server
std::string model_hash_str =
std::string
(net->
GetModelPath
()) +
std::string
(net->
GetClassPath
());
const
size_t
model_hash =
model_hasher
(model_hash_str);
ROS_INFO
(
"
model hash => %zu
"
, model_hash);
ROS_INFO
(
"
hash string => %s
"
, model_hash_str.
c_str
());
//
obtain the list of class descriptions
std::vector<std::string> class_descriptions;
const
uint32_t
num_classes = net->
GetNumClasses
();
for
(
uint32_t
n=
0
; n < num_classes; n++ )
class_descriptions.
push_back
(net->
GetClassDesc
(n));
//
create the key on the param server
std::string class_key =
std::string
(
"
class_labels_
"
) +
std::to_string
(model_hash);
ROS_DECLARE_PARAMETER
(class_key, class_descriptions);
ROS_SET_PARAMETER
(class_key, class_descriptions);
//
populate the vision info msg
const
std::string node_namespace =
ROS_GET_NAMESPACE
();
ROS_INFO
(
"
node namespace => %s
"
, node_namespace.
c_str
());
info_msg.
database_location
= node_namespace +
std::string
(
"
/
"
) + class_key;
info_msg.
database_version
=
0
;
info_msg.
method
= net->
GetModelPath
();
ROS_INFO
(
"
class labels => %s
"
, info_msg.
database_location
.
c_str
());
/*
* create image converter objects
*/
input_cvt =
new
imageConverter
();
overlay_cvt =
new
imageConverter
();
if
( !input_cvt || !overlay_cvt )
{
ROS_ERROR
(
"
failed to create imageConverter objects
"
);
return
0
;
}
/*
* advertise publisher topics
*/
ROS_CREATE_PUBLISHER
(vision_msgs::Classification2D,
"
classification
"
,
5
, classify_pub);
ROS_CREATE_PUBLISHER
(sensor_msgs::Image,
"
overlay
"
,
2
, overlay_pub);
ROS_CREATE_PUBLISHER_STATUS
(vision_msgs::VisionInfo,
"
vision_info
"
,
1
, info_callback, info_pub);
/*
* subscribe to image topic
*/
auto
img_sub =
ROS_CREATE_SUBSCRIBER
(sensor_msgs::Image,
"
image_in
"
,
5
, img_callback);
/*
* wait for messages
*/
ROS_INFO
(
"
imagenet node initialized, waiting for messages
"
);
ROS_SPIN
();
/*
* free resources
*/
delete
net;
delete
input_cvt;
delete
overlay_cvt;
return
0
;
}
You can’t perform that action at this time.