ported all demos and examples to the new api

pull/3471/head
kallaballa 2 years ago
parent 834ba6e891
commit cbc6082fcf
  1. 23
      modules/v4d/include/opencv2/v4d/v4d.hpp
  2. 2
      modules/v4d/samples/beauty-demo.cpp
  3. 2
      modules/v4d/samples/cube-demo.cpp
  4. 2
      modules/v4d/samples/custom_source_and_sink.cpp
  5. 2
      modules/v4d/samples/display_image.cpp
  6. 2
      modules/v4d/samples/display_image_fb.cpp
  7. 2
      modules/v4d/samples/display_image_nvg.cpp
  8. 2
      modules/v4d/samples/font-demo.cpp
  9. 2
      modules/v4d/samples/font_rendering.cpp
  10. 2
      modules/v4d/samples/font_with_gui.cpp
  11. 2
      modules/v4d/samples/many_cubes-demo.cpp
  12. 2
      modules/v4d/samples/nanovg-demo.cpp
  13. 2
      modules/v4d/samples/optflow-demo.cpp
  14. 32
      modules/v4d/samples/pedestrian-demo.cpp
  15. 2
      modules/v4d/samples/render_opengl.cpp
  16. 2
      modules/v4d/samples/shader-demo.cpp
  17. 2
      modules/v4d/samples/vector_graphics.cpp
  18. 2
      modules/v4d/samples/vector_graphics_and_fb.cpp
  19. 2
      modules/v4d/samples/video-demo.cpp
  20. 2
      modules/v4d/samples/video_editing.cpp

@ -71,7 +71,7 @@ class Plan {
public: public:
virtual ~Plan() {}; virtual ~Plan() {};
virtual void setup(cv::Ptr<V4D> window) {}; virtual void setup(cv::Ptr<V4D> window) {};
virtual void infere(cv::Ptr<V4D> window) = 0; virtual void infer(cv::Ptr<V4D> window) = 0;
virtual void teardown(cv::Ptr<V4D> window) {}; virtual void teardown(cv::Ptr<V4D> window) {};
}; };
/*! /*!
@ -487,7 +487,7 @@ public:
} }
void capture() { void capture() {
cv::UMat tmp; static thread_local cv::UMat tmp(fbSize(), CV_8UC3);
capture(tmp); capture(tmp);
} }
@ -507,10 +507,10 @@ public:
} }
void write() { void write() {
cv::UMat frame; static thread_local cv::UMat frame(fbSize(), CV_8UC3);
fb([](cv::UMat& frameBuffer, const cv::UMat& f) { fb([](const cv::UMat& frameBuffer, cv::UMat& f) {
f.copyTo(frameBuffer); frameBuffer.copyTo(f);
}, frame); }, frame);
write([](cv::UMat& outputFrame, const cv::UMat& f){ write([](cv::UMat& outputFrame, const cv::UMat& f){
@ -540,13 +540,6 @@ public:
}); });
} }
/*!
* Execute function object fn inside a nanovg context.
* The context takes care of setting up opengl and nanovg states.
* A function object passed like that can use the functions in cv::viz::nvg.
* @param fn A function that is passed the size of the framebuffer
* and performs drawing using cv::v4d::nvg
*/
template <typename Tfn, typename ... Args> template <typename Tfn, typename ... Args>
void nvg(Tfn fn, Args&&... args) { void nvg(Tfn fn, Args&&... args) {
CV_Assert(detail::is_stateless<std::remove_cv_t<std::remove_reference_t<decltype(fn)>>>::value); CV_Assert(detail::is_stateless<std::remove_cv_t<std::remove_reference_t<decltype(fn)>>>::value);
@ -578,11 +571,9 @@ public:
TimeTracker::getInstance()->execute(id, [this, fn, id, &args...](){ TimeTracker::getInstance()->execute(id, [this, fn, id, &args...](){
(emit_access<std::true_type, std::remove_reference_t<Args>, Args...>(id, std::is_const_v<std::remove_reference_t<Args>>, &args),...); (emit_access<std::true_type, std::remove_reference_t<Args>, Args...>(id, std::is_const_v<std::remove_reference_t<Args>>, &args),...);
std::function functor(fn); std::function functor(fn);
typename detail::function_traits<decltype(fn)>::argument_types t = std::make_tuple(args...); add_transaction<decltype(functor)>(parallelCtx(), id, std::forward<decltype(functor)>(fn), std::forward<Args>(args)...);
std::apply([=](auto &&... Targs) { add_transaction<decltype(functor)>(parallelCtx(), id, std::forward<decltype(functor)>(fn), std::forward<std::add_lvalue_reference_t<decltype(Targs)>>(Targs)...); }, t);
}); });
} }
CV_EXPORTS void imgui(std::function<void(ImGuiContext* ctx)> fn); CV_EXPORTS void imgui(std::function<void(ImGuiContext* ctx)> fn);
/*! /*!
@ -688,7 +679,7 @@ public:
this->runPlan(); this->runPlan();
this->display(); this->display();
this->clearPlan(); this->clearPlan();
plan->infere(self()); plan->infer(self());
this->makePlan(); this->makePlan();
do { do {
this->runPlan(); this->runPlan();

@ -215,7 +215,7 @@ class BeautyDemoPlan : public Plan {
bool faceFound_ = false; bool faceFound_ = false;
FaceFeatures features_; FaceFeatures features_;
public: public:
void infere(cv::Ptr<V4D> window) override { void infer(cv::Ptr<V4D> window) override {
auto always = [](){ return true; }; auto always = [](){ return true; };
auto isTrue = [](bool& ff){ return ff; }; auto isTrue = [](bool& ff){ return ff; };
auto isFalse = [](bool& ff){ return !ff; }; auto isFalse = [](bool& ff){ return !ff; };

@ -219,7 +219,7 @@ public:
init_scene(sz, v, sp, ut); init_scene(sz, v, sp, ut);
}, window->fbSize(), vao, shaderProgram, uniformTransform); }, window->fbSize(), vao, shaderProgram, uniformTransform);
} }
void infere(cv::Ptr<V4D> window) { void infer(cv::Ptr<V4D> window) {
window->gl([](){ window->gl([](){
//Clear the background //Clear the background
glClearColor(0.2, 0.24, 0.4, 1); glClearColor(0.2, 0.24, 0.4, 1);

@ -41,7 +41,7 @@ int main() {
class CustomSourceAndSinkPlan : public Plan { class CustomSourceAndSinkPlan : public Plan {
string hr_ = "Hello Rainbow!"; string hr_ = "Hello Rainbow!";
void infere(cv::Ptr<V4D> win) override { void infer(cv::Ptr<V4D> win) override {
win->capture(); win->capture();
//Render "Hello Rainbow!" over the video //Render "Hello Rainbow!" over the video

@ -21,7 +21,7 @@ int main() {
#endif #endif
} }
//Display the framebuffer in the native window in an endless loop. //Display the framebuffer in the native window in an endless loop.
void infere(Ptr<V4D> win) override { void infer(Ptr<V4D> win) override {
//Feeds the image to the video pipeline //Feeds the image to the video pipeline
win->feed(image_); win->feed(image_);
} }

@ -27,7 +27,7 @@ int main() {
}, image_, resized_, converted_, win->fbSize()); }, image_, resized_, converted_, win->fbSize());
} }
void infere(Ptr<V4D> win) override { void infer(Ptr<V4D> win) override {
//Create a fb context and copy the prepared image to the framebuffer. The fb context //Create a fb context and copy the prepared image to the framebuffer. The fb context
//takes care of retrieving and storing the data on the graphics card (using CL-GL //takes care of retrieving and storing the data on the graphics card (using CL-GL
//interop if available), ready for other contexts to use //interop if available), ready for other contexts to use

@ -40,7 +40,7 @@ public:
}, image_); }, image_);
} }
void infere(Ptr<V4D> win) override{ void infer(Ptr<V4D> win) override{
//Creates a NanoVG context to draw the loaded image_ over again to the screen. //Creates a NanoVG context to draw the loaded image_ over again to the screen.
win->nvg([](const Image_t& img, const cv::Size& sz) { win->nvg([](const Image_t& img, const cv::Size& sz) {
using namespace cv::v4d::nvg; using namespace cv::v4d::nvg;

@ -95,7 +95,7 @@ class FontDemoPlan : public Plan {
int32_t translateY_; int32_t translateY_;
public: public:
void infere(cv::Ptr<V4D> window) override { void infer(cv::Ptr<V4D> window) override {
auto always = []() { return true; }; auto always = []() { return true; };
auto isTrue = [](const bool& b) { return b; }; auto isTrue = [](const bool& b) { return b; };

@ -10,7 +10,7 @@ int main() {
//The text to render //The text to render
string hw_ = "Hello World"; string hw_ = "Hello World";
public: public:
void infere(Ptr<V4D> win) override { void infer(Ptr<V4D> win) override {
//Render the text at the center of the screen. Note that you can load you own fonts. //Render the text at the center of the screen. Note that you can load you own fonts.
win->nvg([](const Size &sz, const string &str) { win->nvg([](const Size &sz, const string &str) {
using namespace cv::v4d::nvg; using namespace cv::v4d::nvg;

@ -25,7 +25,7 @@ int main() {
//The text //The text
string hw_ = "hello world"; string hw_ = "hello world";
public: public:
void infere(Ptr<V4D> win) override { void infer(Ptr<V4D> win) override {
//Render the text at the center of the screen using parameters from the GUI. //Render the text at the center of the screen using parameters from the GUI.
win->nvg([](const Size& sz, const string& str, const float& s, const std::vector<float>& c) { win->nvg([](const Size& sz, const string& str, const float& s, const std::vector<float>& c) {
using namespace cv::v4d::nvg; using namespace cv::v4d::nvg;

@ -232,7 +232,7 @@ public:
}, window->fbSize(), vao[i], shaderProgram[i], uniformTransform[i]); }, window->fbSize(), vao[i], shaderProgram[i], uniformTransform[i]);
} }
} }
void infere(cv::Ptr<V4D> window) { void infer(cv::Ptr<V4D> window) {
window->gl([](){ window->gl([](){
//Clear the background //Clear the background
glClearColor(0.2, 0.24, 0.4, 1); glClearColor(0.2, 0.24, 0.4, 1);

@ -133,7 +133,7 @@ class NanoVGDemoPlan : public Plan {
cv::UMat hueChannel_; cv::UMat hueChannel_;
double hue_; double hue_;
public: public:
void infere(cv::Ptr<V4D> window) override { void infer(cv::Ptr<V4D> window) override {
window->parallel([](const uint64_t& frameCount, double& hue){ window->parallel([](const uint64_t& frameCount, double& hue){
//we use frame count to calculate the current hue //we use frame count to calculate the current hue

@ -403,7 +403,7 @@ class OptflowPlan : public Plan {
vector<cv::Point2f> detectedPoints; vector<cv::Point2f> detectedPoints;
public: public:
virtual ~OptflowPlan() override {}; virtual ~OptflowPlan() override {};
virtual void infere(cv::Ptr<V4D> window) override { virtual void infer(cv::Ptr<V4D> window) override {
window->capture([](const cv::UMat& videoFrame, cv::UMat& d, cv::UMat& b) { window->capture([](const cv::UMat& videoFrame, cv::UMat& d, cv::UMat& b) {
//resize to foreground scale //resize to foreground scale
cv::resize(videoFrame, d, cv::Size(videoFrame.size().width * fg_scale, videoFrame.size().height * fg_scale)); cv::resize(videoFrame, d, cv::Size(videoFrame.size().width * fg_scale, videoFrame.size().height * fg_scale));

@ -33,8 +33,6 @@ constexpr const char* OUTPUT_FILENAME = "pedestrian-demo.mkv";
#endif #endif
const int BLUR_KERNEL_SIZE = std::max(int(DIAG / 200 % 2 == 0 ? DIAG / 200 + 1 : DIAG / 200), 1); const int BLUR_KERNEL_SIZE = std::max(int(DIAG / 200 % 2 == 0 ? DIAG / 200 + 1 : DIAG / 200), 1);
//Descriptor used for pedestrian detection
static thread_local cv::HOGDescriptor hog;
//adapted from cv::dnn_objdetect::InferBbox //adapted from cv::dnn_objdetect::InferBbox
static inline bool pair_comparator(std::pair<double, size_t> l1, std::pair<double, size_t> l2) { static inline bool pair_comparator(std::pair<double, size_t> l1, std::pair<double, size_t> l2) {
@ -138,16 +136,20 @@ class PedestrianDemoPlan : public Plan {
bool trackerInitialized_ = false; bool trackerInitialized_ = false;
//If tracking fails re-detect //If tracking fails re-detect
bool redetect_ = true; bool redetect_ = true;
//Descriptor used for pedestrian detection
cv::HOGDescriptor hog_;
public: public:
void setup(cv::Ptr<V4D> window) override { void setup(cv::Ptr<V4D> window) override {
params_.desc_pca = cv::TrackerKCF::GRAY; window->parallel([](cv::TrackerKCF::Params& params, cv::Ptr<cv::Tracker>& tracker, cv::HOGDescriptor& hog){
params_.compress_feature = false; params.desc_pca = cv::TrackerKCF::GRAY;
params_.compressed_size = 1; params.compress_feature = false;
tracker_ = cv::TrackerKCF::create(params_); params.compressed_size = 1;
tracker = cv::TrackerKCF::create(params);
hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector()); hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
}, params_, tracker_, hog_);
} }
void infere(cv::Ptr<V4D> window) override { void infer(cv::Ptr<V4D> window) override {
static auto always = [](){ return true; }; static auto always = [](){ return true; };
static auto doRedect = [](const bool& trackerInit, const bool& redetect){ return !trackerInit || redetect; }; static auto doRedect = [](const bool& trackerInit, const bool& redetect){ return !trackerInit || redetect; };
static auto dontRedect = [](const bool& trackerInit, const bool& redetect){ return trackerInit && !redetect; }; static auto dontRedect = [](const bool& trackerInit, const bool& redetect){ return trackerInit && !redetect; };
@ -156,11 +158,11 @@ public:
{ {
window->capture(); window->capture();
window->fb([&](const cv::UMat& frameBuffer){ window->fb([](const cv::UMat& frameBuffer, cv::UMat& videoFrame){
//copy video frame //copy video frame
cvtColor(frameBuffer,videoFrame_,cv::COLOR_BGRA2RGB); cvtColor(frameBuffer,videoFrame,cv::COLOR_BGRA2RGB);
//downsample video frame for hog detection //downsample video frame for hog_ detection
}); }, videoFrame_);
window->parallel([](const cv::UMat& videoFrame, cv::UMat& videoFrameDown, cv::UMat& videoFrameDownGrey, cv::UMat& background){ window->parallel([](const cv::UMat& videoFrame, cv::UMat& videoFrameDown, cv::UMat& videoFrameDownGrey, cv::UMat& background){
cv::resize(videoFrame, videoFrameDown, cv::Size(DOWNSIZE_WIDTH, DOWNSIZE_HEIGHT)); cv::resize(videoFrame, videoFrameDown, cv::Size(DOWNSIZE_WIDTH, DOWNSIZE_HEIGHT));
@ -173,7 +175,7 @@ public:
//Try to track the pedestrian (if we currently are tracking one), else re-detect using HOG descriptor //Try to track the pedestrian (if we currently are tracking one), else re-detect using HOG descriptor
window->graph(doRedect, trackerInitialized_, redetect_); window->graph(doRedect, trackerInitialized_, redetect_);
{ {
window->parallel([](bool& redetect, cv::UMat& videoFrameDownGrey, std::vector<cv::Rect>& locations, vector<vector<double>>& boxes, vector<double>& probs, cv::Ptr<cv::TrackerKCF>& tracker, cv::Rect& tracked, bool& trackerInitialized){ window->parallel([](cv::HOGDescriptor& hog, bool& redetect, cv::UMat& videoFrameDownGrey, std::vector<cv::Rect>& locations, vector<vector<double>>& boxes, vector<double>& probs, cv::Ptr<cv::Tracker>& tracker, cv::Rect& tracked, bool& trackerInitialized){
redetect = false; redetect = false;
//Detect pedestrians //Detect pedestrians
hog.detectMultiScale(videoFrameDownGrey, locations, 0, cv::Size(), cv::Size(), 1.15, 2.0, false); hog.detectMultiScale(videoFrameDownGrey, locations, 0, cv::Size(), cv::Size(), 1.15, 2.0, false);
@ -203,12 +205,12 @@ public:
trackerInitialized = true; trackerInitialized = true;
} }
} }
}, redetect_, videoFrameDownGrey_, locations_, boxes_, probs_, tracker_, tracked_, trackerInitialized_); }, hog_, redetect_, videoFrameDownGrey_, locations_, boxes_, probs_, tracker_, tracked_, trackerInitialized_);
} }
window->endgraph(doRedect, trackerInitialized_, redetect_); window->endgraph(doRedect, trackerInitialized_, redetect_);
window->graph(dontRedect, trackerInitialized_, redetect_); window->graph(dontRedect, trackerInitialized_, redetect_);
{ {
window->parallel([](bool& redetect, const cv::UMat& videoFrameDownGrey, cv::Ptr<cv::TrackerKCF>& tracker, cv::Rect& tracked){ window->parallel([](bool& redetect, cv::UMat& videoFrameDownGrey, cv::Ptr<cv::Tracker>& tracker, cv::Rect& tracked){
if(!tracker->update(videoFrameDownGrey, tracked)) { if(!tracker->update(videoFrameDownGrey, tracked)) {
//detection failed - re-detect //detection failed - re-detect
redetect = true; redetect = true;
@ -269,7 +271,7 @@ int main(int argc, char **argv) {
window->setSource(src); window->setSource(src);
#endif #endif
window->run<PedestrianDemoPlan>(0); window->run<PedestrianDemoPlan>(2);
return 0; return 0;
} }

@ -15,7 +15,7 @@ int main() {
glClearColor(0.0f, 0.0f, 1.0f, 1.0f); glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}); });
} }
void infere(Ptr<V4D> win) override { void infer(Ptr<V4D> win) override {
win->gl([]() { win->gl([]() {
//Clears the screen. The clear color and other GL-states are preserved between context-calls. //Clears the screen. The clear color and other GL-states are preserved between context-calls.
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);

@ -276,7 +276,7 @@ void setup(cv::Ptr<V4D> window) override {
}, window->fbSize()); }, window->fbSize());
} }
void infere(cv::Ptr<V4D> window) override { void infer(cv::Ptr<V4D> window) override {
window->capture(frame_); window->capture(frame_);
window->gl([](const cv::Size &sz) { window->gl([](const cv::Size &sz) {

@ -5,7 +5,7 @@ using namespace cv::v4d;
class VectorGraphicsPlan: public Plan { class VectorGraphicsPlan: public Plan {
public: public:
void infere(Ptr<V4D> win) override { void infer(Ptr<V4D> win) override {
//Creates a NanoVG context and draws googly eyes that occasionally blink. //Creates a NanoVG context and draws googly eyes that occasionally blink.
win->nvg([](const Size &sz) { win->nvg([](const Size &sz) {
//Calls from this namespace may only be used inside a nvg context. //Calls from this namespace may only be used inside a nvg context.

@ -9,7 +9,7 @@ int main() {
class VectorGraphicsAndFBPlan : public Plan { class VectorGraphicsAndFBPlan : public Plan {
public: public:
void infere(Ptr<V4D> win) override { void infer(Ptr<V4D> win) override {
//Again creates a NanoVG context and draws googly eyes //Again creates a NanoVG context and draws googly eyes
win->nvg([](const Size& sz) { win->nvg([](const Size& sz) {
//Calls from this namespace may only be used inside a nvg context //Calls from this namespace may only be used inside a nvg context

@ -197,7 +197,7 @@ public:
init_scene(); init_scene();
}); });
} }
void infere(cv::Ptr<V4D> window) override { void infer(cv::Ptr<V4D> window) override {
window->capture(frame_); window->capture(frame_);
window->gl([]() { window->gl([]() {

@ -14,7 +14,7 @@ int main(int argc, char** argv) {
cv::UMat frame_; cv::UMat frame_;
const string hv_ = "Hello Video!"; const string hv_ = "Hello Video!";
public: public:
void infere(Ptr<V4D> win) override { void infer(Ptr<V4D> win) override {
//Capture video from the source //Capture video from the source
win->capture(); win->capture();

Loading…
Cancel
Save