Skip to content

Commit

Permalink
Remove grab thread
Browse files Browse the repository at this point in the history
  • Loading branch information
peckto committed May 30, 2021
1 parent 81e8b21 commit a3e1b0e
Showing 1 changed file with 18 additions and 69 deletions.
87 changes: 18 additions & 69 deletions deepseg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -165,16 +164,6 @@ long diffnanosecs(timestamp_t t1, timestamp_t t2) {
return std::chrono::duration_cast<std::chrono::nanoseconds>(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,
Expand Down Expand Up @@ -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<std::mutex> 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;
Expand Down Expand Up @@ -576,35 +542,25 @@ 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));

bool filterActive = true;

// 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<std::mutex> 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) {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -688,12 +643,6 @@ int main(int argc, char* argv[]) {
}
}

{
std::lock_guard<std::mutex> hold(capinfo.lock);
capinfo.grab = NULL;
}
grabber.join();

printf("\n");
return 0;
}

0 comments on commit a3e1b0e

Please sign in to comment.