@@ -51,7 +51,7 @@ class ObjectDetectUDPStage : public PostProcessingStage
51
51
std::string udp_broadcast_address = " 127.0.0.1" ;
52
52
u_int16_t udp_broadcast_port = 12345 ;
53
53
int sockfd_;
54
- struct sockaddr_in servaddr_;
54
+ struct sockaddr_in servaddr_;
55
55
};
56
56
57
57
#define NAME " object_detect_udp"
@@ -64,11 +64,11 @@ char const *ObjectDetectUDPStage::Name() const
64
64
65
65
ObjectDetectUDPStage::~ObjectDetectUDPStage ()
66
66
{
67
- if (sockfd_ != -1 )
68
- {
69
- close (sockfd_);
70
- std::cerr << " UDP socket closed." << std::endl;
71
- }
67
+ if (sockfd_ != -1 )
68
+ {
69
+ close (sockfd_);
70
+ std::cerr << " UDP socket closed." << std::endl;
71
+ }
72
72
}
73
73
74
74
@@ -77,92 +77,97 @@ void ObjectDetectUDPStage::Configure()
77
77
stream_ = app_->GetMainStream ();
78
78
79
79
// Initialize UDP socket
80
- sockfd_ = socket (AF_INET, SOCK_DGRAM, 0 );
81
- if (sockfd_ < 0 )
82
- {
83
- perror (" UDP socket creation failed" );
84
- // Handle error, perhaps throw an exception or exit
85
- return ;
86
- }
87
-
88
- memset (&servaddr_, 0 , sizeof (servaddr_));
89
-
90
- // Filling server information
91
- servaddr_.sin_family = AF_INET;
92
- servaddr_.sin_port = htons (udp_broadcast_port);
93
- if (inet_pton (AF_INET, udp_broadcast_address.c_str (), &servaddr_.sin_addr ) <= 0 )
94
- {
95
- perror (" Invalid address/ Address not supported" );
96
- close (sockfd_);
97
- sockfd_ = -1 ; // Mark as invalid
98
- return ;
99
- }
100
-
101
- std::cerr << " UDP socket initialized for IP: " << udp_broadcast_address << " , Port: " << udp_broadcast_port << std::endl;
102
- }
80
+ sockfd_ = socket (AF_INET, SOCK_DGRAM, 0 );
81
+ if (sockfd_ < 0 )
82
+ {
83
+ perror (" UDP socket creation failed" );
84
+ // Handle error, perhaps throw an exception or exit
85
+ return ;
86
+ }
87
+
88
+ memset (&servaddr_, 0 , sizeof (servaddr_));
89
+
90
+ // Filling server information
91
+ servaddr_.sin_family = AF_INET;
92
+ servaddr_.sin_port = htons (udp_broadcast_port);
93
+ if (inet_pton (AF_INET, udp_broadcast_address.c_str (), &servaddr_.sin_addr ) <= 0 )
94
+ {
95
+ perror (" Invalid address/ Address not supported" );
96
+ close (sockfd_);
97
+ sockfd_ = -1 ; // Mark as invalid
98
+ return ;
99
+ }
103
100
101
+ std::cerr
102
+ << " UDP socket initialized for IP: " << udp_broadcast_address << " , Port: " << udp_broadcast_port << std::endl;
103
+ }
104
104
105
105
void ObjectDetectUDPStage::Read (boost::property_tree::ptree const ¶ms)
106
106
{
107
107
udp_broadcast_address = params.get <std::string>(" ip" , UDP_IP);
108
108
udp_broadcast_port = params.get <u_int16_t >(" port" , UDP_PORT);
109
109
}
110
110
111
+ template <typename T>
112
+ void append (std::vector<char > &buffer, const T &value)
113
+ {
114
+ const char *raw = reinterpret_cast <const char *>(&value);
115
+ buffer.insert (buffer.end (), raw, raw + sizeof (T));
116
+ }
117
+
111
118
bool ObjectDetectUDPStage::Process (CompletedRequestPtr &completed_request)
112
119
{
113
120
if (!stream_)
114
- return false ;
121
+ return false ;
115
122
116
- std::vector<Detection> detections;
117
- completed_request->post_process_metadata .Get (" object_detect.results" , detections);
123
+ std::vector<Detection> detections;
124
+ completed_request->post_process_metadata .Get (" object_detect.results" , detections);
118
125
119
- if (sockfd_ == -1 )
120
- return false ;
126
+ if (sockfd_ == -1 )
127
+ return false ;
121
128
122
- for (auto &detection : detections)
129
+ for (auto &detection : detections)
123
130
{
124
- // Draw rectangle and text on the image
125
- std::stringstream text_stream;
126
- text_stream << detection.name << " " << (int )(detection.confidence * 100 ) << " %" ;
127
- std::string text = text_stream.str ();
131
+ // Draw rectangle and text on the image
132
+ std::stringstream text_stream;
133
+ text_stream << detection.name << " " << (int )(detection.confidence * 100 ) << " %" ;
134
+ std::string text = text_stream.str ();
128
135
129
- // Use a vector to dynamically build the binary message
130
- std::vector<char > udp_data_buffer;
131
-
132
- // 1. Add start delimiter (4 bytes)
133
- udp_data_buffer.insert (udp_data_buffer.end (), (char *)&START_DELIMITER, (char *)&START_DELIMITER + sizeof (START_DELIMITER));
134
-
135
- // 2. Add x, y, width, height (4 bytes each)
136
- const int32_t x = detection.box .x ;
137
- const int32_t y = detection.box .y ;
138
- const int32_t width = detection.box .width ;
139
- const int32_t height = detection.box .height ;
140
- udp_data_buffer.insert (udp_data_buffer.end (), (char *)&x, (char *)&x + sizeof (x));
141
- udp_data_buffer.insert (udp_data_buffer.end (), (char *)&y, (char *)&y + sizeof (y));
142
- udp_data_buffer.insert (udp_data_buffer.end (), (char *)&width, (char *)&width + sizeof (width));
143
- udp_data_buffer.insert (udp_data_buffer.end (), (char *)&height, (char *)&height + sizeof (height));
144
-
145
- // 3. Add name length and name string
146
- uint32_t name_length = detection.name .length ();
147
- if (name_length > 255 ) {
148
- // Truncate or handle error for names longer than 255 chars
149
- name_length = 255 ;
150
- }
151
- udp_data_buffer.push_back (name_length);
152
- udp_data_buffer.insert (udp_data_buffer.end (), detection.name .begin (), detection.name .begin () + name_length);
153
-
154
- // 4. Add confidence (4 bytes)
155
- const float confidence = detection.confidence ;
156
- udp_data_buffer.insert (udp_data_buffer.end (), (char *)&confidence, (char *)&confidence + sizeof (confidence));
157
-
158
- // Send data via UDP
159
- const ssize_t bytes_sent = sendto (sockfd_, udp_data_buffer.data (), udp_data_buffer.size (), 0 ,
160
- (const struct sockaddr *)&servaddr_, sizeof (servaddr_));
161
- if (bytes_sent < 0 )
162
- {
163
- perror (" Failed to send UDP message" );
164
- }
165
-
136
+ // Use a vector to dynamically build the binary message
137
+ std::vector<char > udp_data_buffer;
138
+
139
+ // 1. Add start delimiter (4 bytes)
140
+ append (udp_data_buffer, START_DELIMITER);
141
+
142
+ // 2. Add x, y, width, height (4 bytes each)
143
+ const int32_t x = detection.box .x ;
144
+ const int32_t y = detection.box .y ;
145
+ const int32_t width = detection.box .width ;
146
+ const int32_t height = detection.box .height ;
147
+
148
+ append (udp_data_buffer, x);
149
+ append (udp_data_buffer, y);
150
+ append (udp_data_buffer, width);
151
+ append (udp_data_buffer, height);
152
+
153
+ // 3. Add name length and name string
154
+ constexpr uint8_t name_length = 255 ;
155
+ udp_data_buffer.push_back (static_cast <char >(name_length));
156
+
157
+ uint8_t name[name_length];
158
+ memcpy (name, detection.name .c_str (), name_length - 2 );
159
+ name[name_length - 1 ] = ' \0 ' ;
160
+ append (udp_data_buffer, name);
161
+
162
+ // 4. Add confidence (4 bytes)
163
+ const float confidence = detection.confidence ;
164
+ append (udp_data_buffer, confidence);
165
+
166
+ // Send data via UDP
167
+ const ssize_t bytes_sent = sendto (sockfd_, udp_data_buffer.data (), udp_data_buffer.size (), 0 ,
168
+ (const struct sockaddr *)&servaddr_, sizeof (servaddr_));
169
+ if (bytes_sent < 0 )
170
+ perror (" Failed to send UDP message" );
166
171
}
167
172
168
173
return false ;
0 commit comments