Skip to content

Commit

Permalink
Add double buffer structure for frame Mat
Browse files Browse the repository at this point in the history
  • Loading branch information
peckto committed May 26, 2021
1 parent 5fb3bc1 commit 82a5f00
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions deepseg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ typedef struct {
timestamp_t retrns;
// values form ai thread are already converted to ns
long ai_condns;
long ai_copyns;
long ai_prens;
long ai_tfltns;
long ai_postns;
Expand Down Expand Up @@ -203,7 +202,10 @@ typedef struct {
cv::Rect roidim;
cv::Mat mask;
cv::Mat mroi;
cv::Mat raw;
cv::Mat buf1;
cv::Mat buf2;
cv::Mat *raw_current;
cv::Mat *raw_next;
cv::Mat ofinal;
cv::Size blur;
float ratio;
Expand Down Expand Up @@ -284,25 +286,28 @@ void init_tensorflow(calcinfo_t &info) {
info.lock_mask = PTHREAD_MUTEX_INITIALIZER;
info.condition_new_frame = PTHREAD_COND_INITIALIZER;
info.running = false;

info.raw_next = &info.buf1;
info.raw_current = &info.buf2;
}

void calc_mask(calcinfo_t &info) {
timestamp_t t = timestamp();
cv::Mat *raw_tmp;

// copy frame from shared buffer
cv::Mat raw;
// change frame buffer pointer
pthread_mutex_lock(&info.lock_raw);
timestamp_t t0 = timestamp();
pthread_cond_wait(&info.condition_new_frame, &info.lock_raw); // wait for new frame to arrive
info.pti->ai_condns = diffnanosecs(timestamp(), t0);
t0 = timestamp();
raw = info.raw.clone();
raw_tmp = info.raw_next;
info.raw_next = info.raw_current;
info.raw_current = raw_tmp;
pthread_mutex_unlock(&info.lock_raw);
info.pti->ai_copyns = diffnanosecs(timestamp(), t0);
t0 = timestamp();

// map ROI
cv::Mat roi = raw(info.roidim);
cv::Mat roi = (*info.raw_current)(info.roidim);

// resize ROI to input size
cv::Mat in_u8_bgr, in_u8_rgb;
Expand Down Expand Up @@ -389,7 +394,7 @@ void calc_mask(calcinfo_t &info) {

// scale up into full-sized mask
pthread_mutex_lock(&info.lock_mask);
cv::resize(info.ofinal,info.mroi,cv::Size(raw.rows/info.ratio,raw.rows));
cv::resize(info.ofinal,info.mroi,cv::Size(info.raw_current->rows/info.ratio,info.raw_current->rows));
pthread_mutex_unlock(&info.lock_mask);
info.pti->ai_maskns = diffnanosecs(timestamp(), t0);

Expand Down Expand Up @@ -635,7 +640,7 @@ int main(int argc, char* argv[]) {
// copy frame to ai thread local buffer
pthread_mutex_lock(&calcinfo.lock_raw);
ti.lockns=timestamp();
calcinfo.raw = raw.clone();
*calcinfo.raw_next = raw.clone();
pthread_cond_signal(&calcinfo.condition_new_frame);
pthread_mutex_unlock(&calcinfo.lock_raw);
ti.copyns=timestamp();
Expand Down Expand Up @@ -688,15 +693,14 @@ int main(int argc, char* argv[]) {
}

// timing details..
printf("video [grab:%9ld retr:%9ld lock:%9ld copy:%9ld post:%9ld v4l2:%9ld] ai [cond:%9ld copy:%9ld pre:%9ld tflt:%9ld post:%9ld mask:%9ld] ",
printf("video [grab:%9ld retr:%9ld lock:%9ld copy:%9ld post:%9ld v4l2:%9ld] ai [cond:%9ld pre:%9ld tflt:%9ld post:%9ld mask:%9ld] ",
diffnanosecs(ti.grabns, ti.lastns),
diffnanosecs(ti.retrns,ti.grabns),
diffnanosecs(ti.lockns,ti.retrns),
diffnanosecs(ti.copyns,ti.lockns),
diffnanosecs(ti.postns,ti.copyns),
diffnanosecs(ti.v4l2ns,ti.postns),
ti.ai_condns,
ti.ai_copyns,
ti.ai_prens,
ti.ai_tfltns,
ti.ai_postns,
Expand Down

0 comments on commit 82a5f00

Please sign in to comment.