rollback experiments

pull/3471/head
kallaballa 2 years ago
parent 5aad32a7f7
commit ecefe5ff18
  1. 13
      src/common/subsystems.hpp
  2. 131
      src/optflow/optflow-demo.cpp

@ -262,11 +262,11 @@ static void error_callback(int error, const char *description) {
class Window { class Window {
cv::Size size_; cv::Size size_;
bool offscreen_; bool offscreen_;
string title_;
int major_; int major_;
int minor_; int minor_;
int samples_; int samples_;
bool debug_; bool debug_;
string title_;
GLFWwindow *glfwWindow_ = nullptr; GLFWwindow *glfwWindow_ = nullptr;
CLGLContext* clglContext_ = nullptr; CLGLContext* clglContext_ = nullptr;
CLVAContext* clvaContext_ = nullptr; CLVAContext* clvaContext_ = nullptr;
@ -276,8 +276,6 @@ class Window {
nanogui::Screen* screen_ = nullptr; nanogui::Screen* screen_ = nullptr;
nanogui::FormHelper* form_ = nullptr; nanogui::FormHelper* form_ = nullptr;
cv::TickMeter tickMeter_; cv::TickMeter tickMeter_;
std::mutex pollMutex_;
bool startPolling_ = false;
bool closed_ = false; bool closed_ = false;
public: public:
@ -579,9 +577,9 @@ public:
} }
bool display() { bool display() {
std::scoped_lock<std::mutex> lock(pollMutex_);
bool result = true; bool result = true;
if (!offscreen_) { if (!offscreen_) {
glfwPollEvents();
screen_->draw_contents(); screen_->draw_contents();
clglContext_->blitFrameBufferToScreen(); clglContext_->blitFrameBufferToScreen();
screen_->draw_widgets(); screen_->draw_widgets();
@ -589,16 +587,9 @@ public:
result = !glfwWindowShouldClose(glfwWindow_); result = !glfwWindowShouldClose(glfwWindow_);
} }
startPolling_ = true;
return result; return result;
} }
void pollEvents() {
std::scoped_lock<std::mutex> lock(pollMutex_);
if(startPolling_)
glfwPollEvents();
}
bool isClosed() { bool isClosed() {
return closed_; return closed_;

@ -184,10 +184,9 @@ void composite_layers(const cv::UMat background, const cv::UMat foreground, cons
} }
void setup_gui(cv::Ptr<kb::Window> window) { void setup_gui(cv::Ptr<kb::Window> window) {
window->makeWindow(6, 45, "Settings"); window->makeWindow(5, 45, "Settings");
auto useOpenCL = window->makeFormVariable("Use OpenCL", use_opencl, "Enable or disable OpenCL acceleration");
window->makeFormVariable("Use OpenCL", use_opencl, "Enable or disable OpenCL acceleration");
window->makeGroup("Foreground"); window->makeGroup("Foreground");
window->makeFormVariable("Scale", fg_scale, 0.1f, 4.0f, true, "", "Generate the foreground at this scale"); window->makeFormVariable("Scale", fg_scale, 0.1f, 4.0f, true, "", "Generate the foreground at this scale");
window->makeFormVariable("Loss", fg_loss, 0.1f, 99.9f, true, "%", "On every frame the foreground loses on brightness"); window->makeFormVariable("Loss", fg_loss, 0.1f, 99.9f, true, "%", "On every frame the foreground loses on brightness");
@ -233,84 +232,74 @@ int main(int argc, char **argv) {
exit(1); exit(1);
} }
cv::Ptr<kb::Window> window = new kb::Window(cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Sparse Optical Flow Demo"); cv::Ptr<kb::Window> window = new kb::Window(cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Sparse Optical Flow Demo");
window->initialize();
std::thread worker([&]() { kb::print_system_info();
window->initialize(); setup_gui(window);
kb::print_system_info();
setup_gui(window);
auto capture = window->makeVACapture(argv[1], VA_HW_DEVICE_INDEX); auto capture = window->makeVACapture(argv[1], VA_HW_DEVICE_INDEX);
if (!capture.isOpened()) { if (!capture.isOpened()) {
cerr << "ERROR! Unable to open video input" << endl; cerr << "ERROR! Unable to open video input" << endl;
exit(-1); exit(-1);
} }
float fps = capture.get(cv::CAP_PROP_FPS); float fps = capture.get(cv::CAP_PROP_FPS);
window->makeVAWriter(OUTPUT_FILENAME, cv::VideoWriter::fourcc('V', 'P', '9', '0'), fps, window->getSize(), VA_HW_DEVICE_INDEX); window->makeVAWriter(OUTPUT_FILENAME, cv::VideoWriter::fourcc('V', 'P', '9', '0'), fps, window->getSize(), VA_HW_DEVICE_INDEX);
//BGRA //BGRA
cv::UMat background, foreground(window->getSize(), CV_8UC4, cv::Scalar::all(0)); cv::UMat background, foreground(window->getSize(), CV_8UC4, cv::Scalar::all(0));
//RGB //RGB
cv::UMat rgb, down; cv::UMat rgb, down;
//GREY //GREY
cv::UMat backgroundGrey, downPrevGrey, downNextGrey, downMotionMaskGrey; cv::UMat backgroundGrey, downPrevGrey, downNextGrey, downMotionMaskGrey;
vector<cv::Point2f> detectedPoints; vector<cv::Point2f> detectedPoints;
while (true) { while (true) {
if(cv::ocl::useOpenCL() != use_opencl) if(cv::ocl::useOpenCL() != use_opencl)
window->setUseOpenCL(use_opencl); window->setUseOpenCL(use_opencl);
if(!window->captureVA()) if(!window->captureVA())
break; break;
window->compute([&](cv::UMat& frameBuffer){ window->compute([&](cv::UMat& frameBuffer){
cv::resize(frameBuffer, down, cv::Size(window->getSize().width * fg_scale, window->getSize().height * fg_scale)); cv::resize(frameBuffer, down, cv::Size(window->getSize().width * fg_scale, window->getSize().height * fg_scale));
cv::cvtColor(frameBuffer, background, cv::COLOR_RGB2BGRA); cv::cvtColor(frameBuffer, background, cv::COLOR_RGB2BGRA);
}); });
cv::cvtColor(down, downNextGrey, cv::COLOR_RGB2GRAY); cv::cvtColor(down, downNextGrey, cv::COLOR_RGB2GRAY);
//Subtract the background to create a motion mask //Subtract the background to create a motion mask
prepare_motion_mask(downNextGrey, downMotionMaskGrey); prepare_motion_mask(downNextGrey, downMotionMaskGrey);
//Detect trackable points in the motion mask //Detect trackable points in the motion mask
detect_points(downMotionMaskGrey, detectedPoints); detect_points(downMotionMaskGrey, detectedPoints);
window->renderNVG([&](NVGcontext* vg, const cv::Size& sz) { window->renderNVG([&](NVGcontext* vg, const cv::Size& sz) {
window->clear(); window->clear();
if (!downPrevGrey.empty()) { if (!downPrevGrey.empty()) {
//We don't want the algorithm to get out of hand when there is a scene change, so we suppress it when we detect one. //We don't want the algorithm to get out of hand when there is a scene change, so we suppress it when we detect one.
if (!detect_scene_change(downMotionMaskGrey, scene_change_thresh, scene_change_thresh_diff)) { if (!detect_scene_change(downMotionMaskGrey, scene_change_thresh, scene_change_thresh_diff)) {
//Visualize the sparse optical flow using nanovg //Visualize the sparse optical flow using nanovg
cv::Scalar color = cv::Scalar(effect_color.r() * 255.0f, effect_color.g() * 255.0f, effect_color.b() * 255.0f, alpha * 255.0f); cv::Scalar color = cv::Scalar(effect_color.r() * 255.0f, effect_color.g() * 255.0f, effect_color.b() * 255.0f, alpha * 255.0f);
visualize_sparse_optical_flow(vg, downPrevGrey, downNextGrey, detectedPoints, fg_scale, max_stroke, color, max_points, point_loss); visualize_sparse_optical_flow(vg, downPrevGrey, downNextGrey, detectedPoints, fg_scale, max_stroke, color, max_points, point_loss);
}
} }
}); }
});
downPrevGrey = downNextGrey.clone();
window->compute([&](cv::UMat& frameBuffer){ downPrevGrey = downNextGrey.clone();
//Put it all together (OpenCL)
composite_layers(background, foreground, frameBuffer, frameBuffer, glow_kernel_size, fg_loss);
});
window->writeVA(); window->compute([&](cv::UMat& frameBuffer){
//Put it all together (OpenCL)
composite_layers(background, foreground, frameBuffer, frameBuffer, glow_kernel_size, fg_loss);
});
update_fps(window, show_fps); window->writeVA();
//If onscreen rendering is enabled it displays the framebuffer in the native window. Returns false if the window was closed.
if(!window->display())
break;
}
window->close();
});
while(!window->isClosed()) { update_fps(window, show_fps);
window->pollEvents(); //If onscreen rendering is enabled it displays the framebuffer in the native window. Returns false if the window was closed.
std::this_thread::sleep_for(10ms); if(!window->display())
break;
} }
worker.join();
return 0; return 0;
} }

Loading…
Cancel
Save