@@ -105,17 +105,14 @@ typedef std::chrono::high_resolution_clock::time_point timestamp_t;
105
105
typedef struct {
106
106
timestamp_t bootns;
107
107
timestamp_t lastns;
108
- timestamp_t waitns;
109
108
timestamp_t lockns;
110
109
timestamp_t copyns;
111
110
timestamp_t prepns;
112
- timestamp_t tfltns;
113
111
timestamp_t maskns;
114
112
timestamp_t postns;
115
113
timestamp_t v4l2ns;
116
- // these are already converted to ns
117
- long grabns;
118
- long retrns;
114
+ timestamp_t grabns;
115
+ timestamp_t retrns;
119
116
} timinginfo_t ;
120
117
121
118
timestamp_t timestamp () {
@@ -135,11 +132,6 @@ typedef struct {
135
132
std::mutex lock;
136
133
} capinfo_t ;
137
134
138
- // timing callbacks
139
- void onprep (void *ctx) { ((timinginfo_t *)ctx)->prepns =timestamp (); }
140
- void oninfer (void *ctx) { ((timinginfo_t *)ctx)->tfltns =timestamp (); }
141
- void onmask (void *ctx) { ((timinginfo_t *)ctx)->maskns =timestamp (); }
142
-
143
135
// let's do this!
144
136
static bool is_number (const std::string &s) {
145
137
return !s.empty () && std::all_of (s.begin (), s.end (), ::isdigit);
@@ -170,11 +162,13 @@ class CalcMask final {
170
162
171
163
void run () {
172
164
cv::Mat *raw_tmp;
165
+ timestamp_t t1;
173
166
174
167
while (thread_state::INIT == this ->state )
175
168
usleep (1000 ); // Wait for constructor to complete initialization
176
169
177
170
while (thread_state::RUNNING == this ->state ) {
171
+ t0 = timestamp ();
178
172
/* actual handling */
179
173
{
180
174
std::unique_lock<std::mutex> hold (lock_frame);
@@ -188,6 +182,9 @@ class CalcMask final {
188
182
frame_next = frame_current;
189
183
frame_current = raw_tmp;
190
184
}
185
+ waitns=diffnanosecs (timestamp (), t0);
186
+ t0 = timestamp ();
187
+ t1 = timestamp ();
191
188
if (!bs_maskgen_process (maskctx, *frame_current, *mask_current)) {
192
189
fprintf (stderr, " failed to process video frame\n " );
193
190
exit (1 );
@@ -199,15 +196,56 @@ class CalcMask final {
199
196
mask_current = raw_tmp;
200
197
new_mask = true ;
201
198
}
199
+ loopns = diffnanosecs (timestamp (), t1);
202
200
}
203
201
}
204
202
203
+ // timing callbacks
204
+ static void onprep (void *ctx) {
205
+ CalcMask *cls = (CalcMask *)ctx;
206
+ cls->prepns =diffnanosecs (timestamp (), cls->t0 );
207
+ cls->t0 = timestamp ();
208
+ }
209
+ static void oninfer (void *ctx) {
210
+ CalcMask *cls = (CalcMask *)ctx;
211
+ cls->tfltns =diffnanosecs (timestamp (), cls->t0 );
212
+ cls->t0 = timestamp ();
213
+ }
214
+ static void onmask (void *ctx) {
215
+ CalcMask *cls = (CalcMask *)ctx;
216
+ cls->maskns =diffnanosecs (timestamp (), cls->t0 );
217
+ cls->t0 = timestamp ();
218
+ }
219
+
205
220
public:
206
- CalcMask (void *maskctx) :
221
+ timestamp_t t0;
222
+ long waitns;
223
+ long prepns;
224
+ long tfltns;
225
+ long maskns;
226
+ long loopns;
227
+
228
+ CalcMask (const char *modelname,
229
+ size_t threads,
230
+ size_t width,
231
+ size_t height) :
207
232
state{thread_state::INIT},
208
233
thread{&CalcMask::run, this } {
234
+ maskctx = bs_maskgen_new (
235
+ modelname,
236
+ threads,
237
+ width,
238
+ height,
239
+ nullptr ,
240
+ onprep,
241
+ oninfer,
242
+ onmask,
243
+ this
244
+ );
245
+ if (!maskctx)
246
+ exit (1 );
247
+
209
248
// Do all other initialization …
210
- this ->maskctx = maskctx;
211
249
frame_next = &frame1;
212
250
frame_current = &frame2;
213
251
mask_current = &mask1;
@@ -220,6 +258,7 @@ class CalcMask final {
220
258
~CalcMask () {
221
259
state = thread_state::DONE;
222
260
thread.join ();
261
+ bs_maskgen_delete (maskctx);
223
262
}
224
263
225
264
void set_input_frame (cv::Mat &frame) {
@@ -423,46 +462,41 @@ int main(int argc, char* argv[]) {
423
462
cap.set (CV_CAP_PROP_FOURCC, fourcc);
424
463
cap.set (CV_CAP_PROP_CONVERT_RGB, true );
425
464
426
- void *maskctx = bs_maskgen_new (modelname, threads, width, height, nullptr , onprep, oninfer, onmask, &ti);
427
- if (!maskctx)
428
- exit (1 );
429
-
430
465
cv::Mat mask (height, width, CV_8U);
431
466
cv::Mat raw;
432
- CalcMask ai (maskctx );
467
+ CalcMask ai (modelname, threads, width, height );
433
468
ti.lastns = timestamp ();
434
469
printf (" Startup: %ldns\n " , diffnanosecs (ti.lastns ,ti.bootns ));
435
470
436
471
bool filterActive = true ;
437
472
438
473
// mainloop
439
474
for (bool running = true ; running; ) {
440
- ti.waitns =timestamp ();
441
-
442
475
// grab new frame from cam
443
476
cap.grab ();
477
+ ti.grabns =timestamp ();
444
478
// copy new frame to buffer
445
479
cap.retrieve (raw);
480
+ ti.retrns =timestamp ();
446
481
ai.set_input_frame (raw);
447
-
448
482
ti.copyns =timestamp ();
483
+
449
484
if (raw.rows == 0 || raw.cols == 0 ) continue ; // sanity check
450
485
451
486
if (blur_strength) {
452
487
raw.copyTo (bg);
453
488
cv::GaussianBlur (bg,bg,cv::Size (blur_strength,blur_strength),0 );
454
489
}
490
+ ti.prepns = timestamp ();
455
491
456
492
if (filterActive) {
457
493
// do background detection magic
458
494
ai.get_output_mask (mask);
459
495
460
496
// alpha blend background over foreground using mask
461
497
raw = alpha_blend (bg, raw, mask);
462
- } else {
463
- // fix up timing values
464
- ti.maskns =ti.tfltns =ti.prepns =ti.copyns ;
465
498
}
499
+ ti.maskns = timestamp ();
466
500
467
501
if (flipHorizontal && flipVertical) {
468
502
cv::flip (raw,raw,-1 );
@@ -495,18 +529,21 @@ int main(int argc, char* argv[]) {
495
529
}
496
530
497
531
// timing details..
498
- printf (" wait:%9ld lock:%9ld [grab:%9ld retr:%9ld] copy:%9ld prep:%9ld tflt:%9ld mask:%9ld post:%9ld v4l2:%9ld FPS: %5.2f\e[K\r " ,
499
- diffnanosecs (ti.waitns ,ti.lastns ),
500
- diffnanosecs (ti.lockns ,ti.waitns ),
501
- ti.grabns ,
502
- ti.retrns ,
503
- diffnanosecs (ti.copyns ,ti.lockns ),
532
+ printf (" main [grab:%9ld retr:%9ld copy:%9ld prep:%9ld mask:%9ld post:%9ld v4l2:%9ld FPS: %5.2f] ai: [wait:%9ld prep:%9ld tflt:%9ld mask:%9ld FPS: %5.2f] \e[K\r " ,
533
+ diffnanosecs (ti.grabns , ti.lastns ),
534
+ diffnanosecs (ti.retrns ,ti.grabns ),
535
+ diffnanosecs (ti.copyns ,ti.retrns ),
504
536
diffnanosecs (ti.prepns ,ti.copyns ),
505
- diffnanosecs (ti.tfltns ,ti.prepns ),
506
- diffnanosecs (ti.maskns ,ti.tfltns ),
537
+ diffnanosecs (ti.maskns ,ti.prepns ),
507
538
diffnanosecs (ti.postns ,ti.maskns ),
508
539
diffnanosecs (ti.v4l2ns ,ti.postns ),
509
- 1e9 /diffnanosecs (ti.v4l2ns ,ti.lastns ));
540
+ 1e9 /diffnanosecs (ti.v4l2ns ,ti.lastns ),
541
+ ai.waitns ,
542
+ ai.prepns ,
543
+ ai.tfltns ,
544
+ ai.maskns ,
545
+ 1e9 /ai.loopns
546
+ );
510
547
fflush (stdout);
511
548
ti.lastns = timestamp ();
512
549
if (debug < 2 ) continue ;
0 commit comments