diff --git a/deepseg.cc b/deepseg.cc index 714e5de..70d06ac 100644 --- a/deepseg.cc +++ b/deepseg.cc @@ -153,9 +153,8 @@ typedef struct { timestamp_t maskns; timestamp_t postns; timestamp_t v4l2ns; - // these are already converted to ns - long grabns; - long retrns; + timestamp_t grabns; + timestamp_t retrns; } timinginfo_t; timestamp_t timestamp() { @@ -165,16 +164,6 @@ long diffnanosecs(timestamp_t t1, timestamp_t t2) { return std::chrono::duration_cast(t1-t2).count(); } -// threaded capture shared state -typedef struct { - cv::VideoCapture *cap; - cv::Mat *grab; - cv::Mat *raw; - int64 cnt; - timinginfo_t *pti; - std::mutex lock; -} capinfo_t; - enum class modeltype_t { Unknown, BodyPix, @@ -208,29 +197,6 @@ typedef struct { float ratio; } calcinfo_t; -// capture thread function -void grab_thread(capinfo_t *ci) { - bool done = false; - // while we have a grab frame.. grab frames - while (!done) { - timestamp_t ts = timestamp(); - ci->cap->grab(); - long ns = diffnanosecs(timestamp(),ts); - { - std::lock_guard hold(ci->lock); - ci->pti->grabns = ns; - if (ci->grab!=NULL) { - ts = timestamp(); - ci->cap->retrieve(*ci->grab); - ci->pti->retrns = diffnanosecs(timestamp(),ts); - } else { - done = true; - } - ci->cnt++; - } - } -} - modeltype_t get_modeltype(const char* modelname) { if (strstr(modelname, "body-pix")) { return modeltype_t::BodyPix; @@ -576,12 +542,6 @@ int main(int argc, char* argv[]) { calcinfo_t calcinfo = { modelname, modeltype, norm, threads, width, height, debug }; init_tensorflow(calcinfo); - // kick off separate grabber thread to keep OpenCV/FFMpeg happy (or it lags badly) - cv::Mat buf1; - cv::Mat buf2; - int64 oldcnt = 0; - capinfo_t capinfo = { &cap, &buf1, &buf2, 0, &ti }; - std::thread grabber(grab_thread, &capinfo); ti.lastns = timestamp(); printf("Startup: %ldns\n", diffnanosecs(ti.lastns,ti.bootns)); @@ -589,22 +549,18 @@ int main(int argc, char* argv[]) { // mainloop for(bool running = true; running; ) { - // wait for next frame - while (capinfo.cnt == oldcnt) usleep(10000); - oldcnt = capinfo.cnt; - ti.waitns=timestamp(); - - // switch buffer pointers in capture thread - { - std::lock_guard hold(capinfo.lock); - ti.lockns=timestamp(); - cv::Mat *tmat = capinfo.grab; - capinfo.grab = capinfo.raw; - capinfo.raw = tmat; - } - // we can now guarantee capinfo.raw will remain unchanged while we process it.. - calcinfo.raw = *capinfo.raw; - ti.copyns=timestamp(); + int e1 = cv::getTickCount(); + + // grab new frame from cam + cap.grab(); + ti.grabns = timestamp(); + + // copy new frame to buffer + cap.retrieve(calcinfo.raw); + ti.retrns = timestamp(); + ti.copyns = timestamp(); + ti.lockns=timestamp(); + if (calcinfo.raw.rows == 0 || calcinfo.raw.cols == 0) continue; // sanity check if (blur_strength) { @@ -651,11 +607,10 @@ int main(int argc, char* argv[]) { } // timing details.. - 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", - diffnanosecs(ti.waitns,ti.lastns), - diffnanosecs(ti.lockns,ti.waitns), - ti.grabns, - ti.retrns, + printf("lock:%9ld [grab:%9ld retr:%9ld] copy:%9ld open:%9ld tflt:%9ld mask:%9ld post:%9ld v4l2:%9ld FPS: %5.2f\e[K\r", + diffnanosecs(ti.lockns,ti.retrns), + diffnanosecs(ti.grabns, ti.lastns), + diffnanosecs(ti.retrns,ti.grabns), diffnanosecs(ti.copyns,ti.lockns), diffnanosecs(ti.prepns,ti.copyns), diffnanosecs(ti.tfltns,ti.prepns), @@ -688,12 +643,6 @@ int main(int argc, char* argv[]) { } } - { - std::lock_guard hold(capinfo.lock); - capinfo.grab = NULL; - } - grabber.join(); - printf("\n"); return 0; }