Skip to content

Commit 52a7921

Browse files
committed
Add logging
1 parent fa12144 commit 52a7921

File tree

1 file changed

+69
-32
lines changed

1 file changed

+69
-32
lines changed

app/deepseg.cc

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,14 @@ typedef std::chrono::high_resolution_clock::time_point timestamp_t;
105105
typedef struct {
106106
timestamp_t bootns;
107107
timestamp_t lastns;
108-
timestamp_t waitns;
109108
timestamp_t lockns;
110109
timestamp_t copyns;
111110
timestamp_t prepns;
112-
timestamp_t tfltns;
113111
timestamp_t maskns;
114112
timestamp_t postns;
115113
timestamp_t v4l2ns;
116-
// these are already converted to ns
117-
long grabns;
118-
long retrns;
114+
timestamp_t grabns;
115+
timestamp_t retrns;
119116
} timinginfo_t;
120117

121118
timestamp_t timestamp() {
@@ -135,11 +132,6 @@ typedef struct {
135132
std::mutex lock;
136133
} capinfo_t;
137134

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-
143135
// let's do this!
144136
static bool is_number(const std::string &s) {
145137
return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
@@ -170,11 +162,13 @@ class CalcMask final {
170162

171163
void run() {
172164
cv::Mat *raw_tmp;
165+
timestamp_t t1;
173166

174167
while(thread_state::INIT == this->state)
175168
usleep(1000); // Wait for constructor to complete initialization
176169

177170
while(thread_state::RUNNING == this->state) {
171+
t0 = timestamp();
178172
/* actual handling */
179173
{
180174
std::unique_lock<std::mutex> hold(lock_frame);
@@ -188,6 +182,9 @@ class CalcMask final {
188182
frame_next = frame_current;
189183
frame_current = raw_tmp;
190184
}
185+
waitns=diffnanosecs(timestamp(), t0);
186+
t0 = timestamp();
187+
t1 = timestamp();
191188
if(!bs_maskgen_process(maskctx, *frame_current, *mask_current)) {
192189
fprintf(stderr, "failed to process video frame\n");
193190
exit(1);
@@ -199,15 +196,56 @@ class CalcMask final {
199196
mask_current = raw_tmp;
200197
new_mask = true;
201198
}
199+
loopns = diffnanosecs(timestamp(), t1);
202200
}
203201
}
204202

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+
205220
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) :
207232
state{thread_state::INIT},
208233
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+
209248
// Do all other initialization …
210-
this->maskctx = maskctx;
211249
frame_next = &frame1;
212250
frame_current = &frame2;
213251
mask_current = &mask1;
@@ -220,6 +258,7 @@ class CalcMask final {
220258
~CalcMask() {
221259
state = thread_state::DONE;
222260
thread.join();
261+
bs_maskgen_delete(maskctx);
223262
}
224263

225264
void set_input_frame(cv::Mat &frame) {
@@ -423,46 +462,41 @@ int main(int argc, char* argv[]) {
423462
cap.set(CV_CAP_PROP_FOURCC, fourcc);
424463
cap.set(CV_CAP_PROP_CONVERT_RGB, true);
425464

426-
void *maskctx = bs_maskgen_new(modelname, threads, width, height, nullptr, onprep, oninfer, onmask, &ti);
427-
if (!maskctx)
428-
exit(1);
429-
430465
cv::Mat mask(height, width, CV_8U);
431466
cv::Mat raw;
432-
CalcMask ai(maskctx);
467+
CalcMask ai(modelname, threads, width, height);
433468
ti.lastns = timestamp();
434469
printf("Startup: %ldns\n", diffnanosecs(ti.lastns,ti.bootns));
435470

436471
bool filterActive = true;
437472

438473
// mainloop
439474
for(bool running = true; running; ) {
440-
ti.waitns=timestamp();
441-
442475
// grab new frame from cam
443476
cap.grab();
477+
ti.grabns=timestamp();
444478
// copy new frame to buffer
445479
cap.retrieve(raw);
480+
ti.retrns=timestamp();
446481
ai.set_input_frame(raw);
447-
448482
ti.copyns=timestamp();
483+
449484
if (raw.rows == 0 || raw.cols == 0) continue; // sanity check
450485

451486
if (blur_strength) {
452487
raw.copyTo(bg);
453488
cv::GaussianBlur(bg,bg,cv::Size(blur_strength,blur_strength),0);
454489
}
490+
ti.prepns = timestamp();
455491

456492
if (filterActive) {
457493
// do background detection magic
458494
ai.get_output_mask(mask);
459495

460496
// alpha blend background over foreground using mask
461497
raw = alpha_blend(bg, raw, mask);
462-
} else {
463-
// fix up timing values
464-
ti.maskns=ti.tfltns=ti.prepns=ti.copyns;
465498
}
499+
ti.maskns = timestamp();
466500

467501
if (flipHorizontal && flipVertical) {
468502
cv::flip(raw,raw,-1);
@@ -495,18 +529,21 @@ int main(int argc, char* argv[]) {
495529
}
496530

497531
// 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),
504536
diffnanosecs(ti.prepns,ti.copyns),
505-
diffnanosecs(ti.tfltns,ti.prepns),
506-
diffnanosecs(ti.maskns,ti.tfltns),
537+
diffnanosecs(ti.maskns,ti.prepns),
507538
diffnanosecs(ti.postns,ti.maskns),
508539
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+
);
510547
fflush(stdout);
511548
ti.lastns = timestamp();
512549
if (debug < 2) continue;

0 commit comments

Comments
 (0)