Skip to content

Commit

Permalink
Split files into digestable chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBE committed Jan 26, 2022
1 parent 7fa052e commit da36ec2
Show file tree
Hide file tree
Showing 7 changed files with 457 additions and 323 deletions.
14 changes: 10 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ set(CONFU_DEPENDENCIES_BINARY_DIR ${CMAKE_BINARY_DIR}/_deps)

# pull in Tensorflow Lite source build
add_subdirectory(${TENSORFLOW}/tensorflow/lite
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL)
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL
)

# build backscrub code
add_compile_definitions(DEEPSEG_VERSION=${DEEPSEG_VERSION} INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX})
Expand All @@ -62,7 +63,8 @@ set(CMAKE_CXX_STANDARD 17)

add_library(backscrub
lib/libbackscrub.cc
lib/transpose_conv_bias.cc)
lib/transpose_conv_bias.cc
)

target_link_libraries(backscrub
tensorflow-lite ${CMAKE_DL_LIBS}
Expand All @@ -73,13 +75,16 @@ target_link_libraries(backscrub
# We don't build the Linux-specific wrapper application on Windows
if(NOT WIN32)
add_library(videoio
videoio/loopback.cc)
videoio/loopback.cc
)

target_link_libraries(videoio)

add_executable(deepseg
app/deepseg.cc
app/background.cc
app/calcmask.cc
app/utils.cc
)

set_target_properties(deepseg PROPERTIES OUTPUT_NAME backscrub)
Expand Down Expand Up @@ -135,7 +140,8 @@ get_link_libraries(backscrub BACKSCRUB_DEPS)
export(TARGETS
backscrub
${BACKSCRUB_DEPS}
FILE BackscrubTargets.cmake)
FILE BackscrubTargets.cmake
)

# installation names for library, header, backgrounds and models
if(NOT WIN32)
Expand Down
8 changes: 4 additions & 4 deletions app/background.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* This is licensed software, @see LICENSE file.
* Authors - @see AUTHORS file. */

#ifndef _BACKGROUND_H_
#define _BACKGROUND_H_
#pragma once

#include <memory>
#include <string>

#include <opencv2/core/mat.hpp>

Expand All @@ -21,5 +23,3 @@ int grab_background(std::shared_ptr<background_t> handle, int width, int height,
// Grab current thumbnail image (if any) from background
// Returns <0 on error, 0 on success and copies thumbnail to out
int grab_thumbnail(std::shared_ptr<background_t> handle, cv::Mat &out);

#endif
122 changes: 122 additions & 0 deletions app/calcmask.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "calcmask.h"

#include <mutex>
#include <string>

#include "lib/libbackscrub.h"

CalcMask::CalcMask(
const std::string& modelname,
size_t threads,
size_t width,
size_t height
) {
maskctx = bs_maskgen_new(modelname.c_str(), threads, width, height, nullptr, onprep, oninfer, onmask, this);
if (!maskctx)
throw "Could not create mask context";

// Do all other initialization …
frame_next = &frame1;
frame_current = &frame2;
mask_current = &mask1;
mask_out = &mask2;
new_frame = false;
new_mask = false;

// Start the actual processing
state = thread_state_t::RUNNING;
thread = std::thread(&CalcMask::run, this);
}

CalcMask::~CalcMask() {
// mark as done
state = thread_state_t::DONE;

// wake up processing thread
new_frame = true;
condition_new_frame.notify_all();

// collect termination
thread.join();

// free resources
bs_maskgen_delete(maskctx);
}

void CalcMask::set_input_frame(cv::Mat &frame) {
std::lock_guard<std::mutex> hold(lock_frame);

*frame_next = frame.clone();
new_frame = true;
condition_new_frame.notify_all();
}

void CalcMask::get_output_mask(cv::Mat &out) {
if (new_mask) {
std::lock_guard<std::mutex> hold(lock_mask);

out = mask_out->clone();
new_mask = false;
}
}

void CalcMask::run() {
cv::Mat *raw_tmp;
timestamp_t tloop;

while(thread_state_t::RUNNING == this->state) {
tloop = t0 = timestamp();

/* actual handling */
{
std::unique_lock<std::mutex> hold(lock_frame);
while (!new_frame) {
condition_new_frame.wait(hold);
}

// change frame buffer pointer
new_frame = false;
raw_tmp = frame_next;
frame_next = frame_current;
frame_current = raw_tmp;
}

waitns = diffnanosecs(timestamp(), t0);
t0 = timestamp();

if(!bs_maskgen_process(maskctx, *frame_current, *mask_current)) {
fprintf(stderr, "failed to process video frame\n");
exit(1);
}

{
std::unique_lock<std::mutex> hold(lock_mask);

raw_tmp = mask_out;
mask_out = mask_current;
mask_current = raw_tmp;
new_mask = true;
}

loopns = diffnanosecs(timestamp(), tloop);
}
}

// timing callbacks
void CalcMask::onprep(void *ctx) {
CalcMask *cls = (CalcMask *)ctx;
cls->prepns = diffnanosecs(timestamp(), cls->t0);
cls->t0 = timestamp();
}

void CalcMask::oninfer(void *ctx) {
CalcMask *cls = (CalcMask *)ctx;
cls->tfltns = diffnanosecs(timestamp(), cls->t0);
cls->t0 = timestamp();
}

void CalcMask::onmask(void *ctx) {
CalcMask *cls = (CalcMask *)ctx;
cls->maskns = diffnanosecs(timestamp(), cls->t0);
cls->t0 = timestamp();
}
65 changes: 65 additions & 0 deletions app/calcmask.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <unistd.h>

#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <mutex>
#include <string>
#include <thread>

#include <opencv2/core/mat.hpp>

#include "utils.h"

enum class thread_state_t { RUNNING, DONE };

class CalcMask final {
protected:
volatile thread_state_t state;

void *maskctx;
timestamp_t t0;

// buffers
cv::Mat mask1;
cv::Mat mask2;
cv::Mat *mask_current;
cv::Mat *mask_out;
cv::Mat frame1;
cv::Mat frame2;
cv::Mat *frame_current;
cv::Mat *frame_next;

// thread synchronisation
std::mutex lock_frame;
std::mutex lock_mask;
std::condition_variable condition_new_frame;
bool new_frame;
bool new_mask;
std::thread thread;

// thread execution
void run();

// timing callbacks
static void onprep(void *ctx);
static void oninfer(void *ctx);
static void onmask(void *ctx);

public:
long waitns;
long prepns;
long tfltns;
long maskns;
long loopns;

CalcMask() = delete;
CalcMask(const CalcMask&) = delete;
CalcMask(const std::string& modelname, size_t threads, size_t width, size_t height);
~CalcMask();

void set_input_frame(cv::Mat &frame);
void get_output_mask(cv::Mat &out);
};
Loading

0 comments on commit da36ec2

Please sign in to comment.