refactoring, renaming and commenting

pull/3471/head
kallaballa 2 years ago
parent c8bc3af0c7
commit 614b39c21c
  1. 4
      modules/v4d/include/opencv2/v4d/nvg.hpp
  2. 34
      modules/v4d/include/opencv2/v4d/v4d.hpp
  3. 26
      modules/v4d/samples/beauty-demo.cpp
  4. 6
      modules/v4d/samples/optflow-demo.cpp
  5. 6
      modules/v4d/src/detail/framebuffercontext.cpp
  6. 1
      modules/v4d/src/detail/nanoguicontext.hpp
  7. 17
      modules/v4d/src/v4d.cpp
  8. 9
      modules/v4d/tutorials/00-intro.markdown

@ -6,8 +6,6 @@
#ifndef SRC_OPENCV_V4D_NVG_HPP_ #ifndef SRC_OPENCV_V4D_NVG_HPP_
#define SRC_OPENCV_V4D_NVG_HPP_ #define SRC_OPENCV_V4D_NVG_HPP_
//#include "v4d.hpp"
#include <stdio.h> #include <stdio.h>
#include <opencv2/core.hpp> #include <opencv2/core.hpp>
#include "nanovg.h" #include "nanovg.h"
@ -17,7 +15,7 @@ struct NVGcontext;
namespace cv { namespace cv {
namespace v4d { namespace v4d {
/*! /*!
* In general please refere to https://github.com/memononen/nanovg/blob/master/src/nanovg.h for reference. * In general please refer to https://github.com/memononen/nanovg/blob/master/src/nanovg.h for reference.
*/ */
namespace nvg { namespace nvg {
/*! /*!

@ -25,14 +25,18 @@
#include "source.hpp" #include "source.hpp"
#include "sink.hpp" #include "sink.hpp"
#include "util.hpp" #include "util.hpp"
#include "formhelper.hpp"
#include "nvg.hpp"
#include "detail/threadpool.hpp"
#include <iostream> #include <iostream>
#include <future> #include <future>
#include <set> #include <set>
#include <string> #include <string>
#include <opencv2/imgproc.hpp> #include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp> #include <opencv2/videoio.hpp>
#include "formhelper.hpp"
#include "detail/threadpool.hpp"
using std::cout; using std::cout;
using std::cerr; using std::cerr;
@ -49,7 +53,7 @@ namespace nanogui {
*/ */
namespace cv { namespace cv {
/*! /*!
* Visualization namespace * V4D namespace
*/ */
namespace v4d { namespace v4d {
class FormHelper; class FormHelper;
@ -63,12 +67,6 @@ class GLContext;
class NanoVGContext; class NanoVGContext;
class NanoguiContext; class NanoguiContext;
/*!
* The GFLW error callback.
* @param error Error number
* @param description Error description
*/
void glfw_error_callback(int error, const char* description);
/*! /*!
* Find widgets that are of type T. * Find widgets that are of type T.
* @tparam T The type of widget to find * @tparam T The type of widget to find
@ -96,10 +94,7 @@ CV_EXPORTS cv::Scalar colorConvert(const cv::Scalar& src, cv::ColorConversionCod
using namespace cv::v4d::detail; using namespace cv::v4d::detail;
class NVG;
class CV_EXPORTS V4D { class CV_EXPORTS V4D {
friend class detail::NanoVGContext;
friend class detail::FrameBufferContext; friend class detail::FrameBufferContext;
friend class HTML5Capture; friend class HTML5Capture;
cv::Size initialSize_; cv::Size initialSize_;
@ -347,12 +342,12 @@ public:
* Enable/Disable scaling the framebuffer during blitting. * Enable/Disable scaling the framebuffer during blitting.
* @param s if true enable scaling * @param s if true enable scaling
*/ */
CV_EXPORTS void setFrameBufferScaling(bool s); CV_EXPORTS void setScaling(bool s);
/*! /*!
* Determine if framebuffer is scaled during blitting. * Determine if framebuffer is scaled during blitting.
* @return true if framebuffer is scaled during blitting. * @return true if framebuffer is scaled during blitting.
*/ */
CV_EXPORTS bool isFrameBufferScaling(); CV_EXPORTS bool isScaling();
/*! /*!
* Everytime a frame is displayed this count is incremented * Everytime a frame is displayed this count is incremented
* @return the current frame count * @return the current frame count
@ -378,15 +373,16 @@ public:
CV_EXPORTS void printSystemInfo(); CV_EXPORTS void printSystemInfo();
CV_EXPORTS void makeCurrent(); CV_EXPORTS void makeCurrent();
NanoguiContext& nguiCtx();
void setDefaultKeyboardEventCallback();
void setKeyboardEventCallback(
std::function<bool(int key, int scancode, int action, int modifiers)> fn);
private: private:
V4D(const cv::Size& size, const cv::Size& fbsize, V4D(const cv::Size& size, const cv::Size& fbsize,
const string& title, bool offscreen, bool debug, bool compat, int samples); const string& title, bool offscreen, bool debug, bool compat, int samples);
void init(); void init();
void setDefaultKeyboardEventCallback();
void setKeyboardEventCallback(
std::function<bool(int key, int scancode, int action, int modifiers)> fn);
void setMouseDrag(bool d); void setMouseDrag(bool d);
bool isMouseDrag(); bool isMouseDrag();
cv::Vec2f getMousePosition(); cv::Vec2f getMousePosition();
@ -397,6 +393,7 @@ private:
CLVAContext& clvaCtx(); CLVAContext& clvaCtx();
NanoVGContext& nvgCtx(); NanoVGContext& nvgCtx();
GLContext& glCtx(); GLContext& glCtx();
NanoguiContext& nguiCtx();
bool hasFbCtx(); bool hasFbCtx();
bool hasClvaCtx(); bool hasClvaCtx();
@ -410,6 +407,5 @@ private:
} }
} /* namespace kb */ } /* namespace kb */
#include <opencv2/v4d/nvg.hpp>
#endif /* SRC_OPENCV_V4D_V4D_HPP_ */ #endif /* SRC_OPENCV_V4D_V4D_HPP_ */

@ -26,8 +26,8 @@ constexpr long unsigned int HEIGHT = 720;
constexpr long unsigned int WIDTH = 960; constexpr long unsigned int WIDTH = 960;
constexpr long unsigned int HEIGHT = 540; constexpr long unsigned int HEIGHT = 540;
#endif #endif
constexpr unsigned int DOWNSIZE_WIDTH = 320; constexpr unsigned int DOWNSIZE_WIDTH = 960;
constexpr unsigned int DOWNSIZE_HEIGHT = 180; constexpr unsigned int DOWNSIZE_HEIGHT = 540;
constexpr bool OFFSCREEN = false; constexpr bool OFFSCREEN = false;
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
constexpr const char *OUTPUT_FILENAME = "beauty-demo.mkv"; constexpr const char *OUTPUT_FILENAME = "beauty-demo.mkv";
@ -35,10 +35,10 @@ constexpr const char *OUTPUT_FILENAME = "beauty-demo.mkv";
const unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT)); const unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT));
/* Visualization parameters */ /* Visualization parameters */
constexpr int BLUR_DIV = 1000; constexpr int BLUR_DIV = 500;
int blur_skin_kernel_size = std::max(int(DIAG / BLUR_DIV % 2 == 0 ? DIAG / BLUR_DIV + 1 : DIAG / BLUR_DIV), 1); int blur_skin_kernel_size = std::max(int(DIAG / BLUR_DIV % 2 == 0 ? DIAG / BLUR_DIV + 1 : DIAG / BLUR_DIV), 1);
//Saturation boost factor for eyes and lips //Saturation boost factor for eyes and lips
float eyes_and_lips_saturation = 1.6f; float eyes_and_lips_saturation = 2.0f;
//Saturation boost factor for skin //Saturation boost factor for skin
float skin_saturation = 1.7f; float skin_saturation = 1.7f;
//Contrast factor skin //Contrast factor skin
@ -46,11 +46,11 @@ float skin_contrast = 0.7f;
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
//Show input and output side by side //Show input and output side by side
bool side_by_side = true; bool side_by_side = true;
//Stretch the video to the window size //Scale the video to the window size
bool stretch = true; bool scale = true;
#else #else
bool side_by_side = false; bool side_by_side = false;
bool stretch = false; bool scale = false;
#endif #endif
cv::Ptr<cv::v4d::V4D> window; cv::Ptr<cv::v4d::V4D> window;
@ -126,7 +126,7 @@ struct FaceFeatures {
return allPoints; return allPoints;
} }
//Returns all face features points in fixed order //Returns all feature points in fixed order
vector<vector<cv::Point2f>> features() const { vector<vector<cv::Point2f>> features() const {
return {chin_, return {chin_,
top_nose_, top_nose_,
@ -206,10 +206,10 @@ static void setup_gui(cv::Ptr<cv::v4d::V4D> v) {
form.makeGroup("Display"); form.makeGroup("Display");
form.makeFormVariable("Side by side", side_by_side, "Enable or disable side by side view"); form.makeFormVariable("Side by side", side_by_side, "Enable or disable side by side view");
auto* stretchVar = form.makeFormVariable("Stretch", stretch, "Enable or disable stetching to the window size"); auto* scaleVar = form.makeFormVariable("Stretch", scale, "Enable or disable stetching to the window size");
stretchVar->set_callback([=](const bool& b) { scaleVar->set_callback([=](const bool& b) {
v->setFrameBufferScaling(b); v->setScaling(b);
stretch = b; scale = b;
}); });
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
@ -398,7 +398,7 @@ int main() {
window = V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(), "Beauty Demo", OFFSCREEN); window = V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(), "Beauty Demo", OFFSCREEN);
facemark->loadModel("assets/lbfmodel.yaml"); facemark->loadModel("assets/lbfmodel.yaml");
window->setFrameBufferScaling(stretch); window->setScaling(scale);
if (!OFFSCREEN) { if (!OFFSCREEN) {
setup_gui(window); setup_gui(window);

@ -100,7 +100,7 @@ nanogui::Color effect_color(1.0f, 0.75f, 0.4f, 1.0f);
//display on-screen FPS //display on-screen FPS
bool show_fps = true; bool show_fps = true;
//Stretch frame buffer to window size //Stretch frame buffer to window size
bool stretch = false; bool scale = false;
//Use OpenCL or not //Use OpenCL or not
bool use_acceleration = true; bool use_acceleration = true;
//The post processing mode //The post processing mode
@ -379,8 +379,8 @@ static void setup_gui(cv::Ptr<cv::v4d::V4D> main, cv::Ptr<cv::v4d::V4D> menu) {
form.makeGroup("Display"); form.makeGroup("Display");
form.makeFormVariable("Show FPS", show_fps, "Enable or disable the On-screen FPS display"); form.makeFormVariable("Show FPS", show_fps, "Enable or disable the On-screen FPS display");
form.makeFormVariable("Stetch", stretch, "Stretch the frame buffer to the window size")->set_callback([=](const bool &s) { form.makeFormVariable("Stetch", scale, "Stretch the frame buffer to the window size")->set_callback([=](const bool &s) {
main->setFrameBufferScaling(s); main->setScaling(s);
}); });
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__

@ -22,6 +22,10 @@ namespace cv {
namespace v4d { namespace v4d {
namespace detail { namespace detail {
void glfw_error_callback(int error, const char* description) {
fprintf(stderr, "GLFW Error: (%d) %s\n", error, description);
}
static bool contains_absolute(nanogui::Widget* w, const nanogui::Vector2i& p) { static bool contains_absolute(nanogui::Widget* w, const nanogui::Vector2i& p) {
nanogui::Vector2i d = p - w->absolute_position(); nanogui::Vector2i d = p - w->absolute_position();
return d.x() >= 0 && d.y() >= 0 && d.x() < w->size().x() && d.y() < w->size().y(); return d.x() >= 0 && d.y() >= 0 && d.x() < w->size().x() && d.y() < w->size().y();
@ -211,7 +215,7 @@ void FrameBufferContext::init() {
#endif #endif
if (glfwInit() != GLFW_TRUE) if (glfwInit() != GLFW_TRUE)
assert(false); assert(false);
glfwSetErrorCallback(cv::v4d::glfw_error_callback); glfwSetErrorCallback(cv::v4d::detail::glfw_error_callback);
if (debug_) if (debug_)
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);

@ -6,6 +6,7 @@
#ifndef SRC_OPENCV_NANOGUICONTEXT_HPP_ #ifndef SRC_OPENCV_NANOGUICONTEXT_HPP_
#define SRC_OPENCV_NANOGUICONTEXT_HPP_ #define SRC_OPENCV_NANOGUICONTEXT_HPP_
#include "opencv2/v4d/nvg.hpp"
#include "framebuffercontext.hpp" #include "framebuffercontext.hpp"

@ -15,11 +15,6 @@
namespace cv { namespace cv {
namespace v4d { namespace v4d {
namespace detail {
void glfw_error_callback(int error, const char* description) {
fprintf(stderr, "GLFW Error: (%d) %s\n", error, description);
}
}
cv::Scalar colorConvert(const cv::Scalar& src, cv::ColorConversionCodes code) { cv::Scalar colorConvert(const cv::Scalar& src, cv::ColorConversionCodes code) {
cv::Mat tmpIn(1, 1, CV_8UC3); cv::Mat tmpIn(1, 1, CV_8UC3);
@ -464,11 +459,11 @@ void V4D::setVisible(bool v) {
nguiCtx().screen().perform_layout(); nguiCtx().screen().perform_layout();
} }
void V4D::setFrameBufferScaling(bool s) { void V4D::setScaling(bool s) {
stretch_ = s; stretch_ = s;
} }
bool V4D::isFrameBufferScaling() { bool V4D::isScaling() {
return stretch_; return stretch_;
} }
@ -494,7 +489,7 @@ void V4D::setDefaultKeyboardEventCallback() {
void V4D::swapContextBuffers() { void V4D::swapContextBuffers() {
run_sync_on_main<10>([this]() { run_sync_on_main<10>([this]() {
FrameBufferContext::GLScope glScope(glCtx().fbCtx(), GL_READ_FRAMEBUFFER); FrameBufferContext::GLScope glScope(glCtx().fbCtx(), GL_READ_FRAMEBUFFER);
glCtx().fbCtx().blitFrameBufferToScreen(viewport(), glCtx().fbCtx().getWindowSize(), isFrameBufferScaling()); glCtx().fbCtx().blitFrameBufferToScreen(viewport(), glCtx().fbCtx().getWindowSize(), isScaling());
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
glfwSwapBuffers(glCtx().fbCtx().getGLFWWindow()); glfwSwapBuffers(glCtx().fbCtx().getGLFWWindow());
#else #else
@ -504,7 +499,7 @@ void V4D::swapContextBuffers() {
run_sync_on_main<11>([this]() { run_sync_on_main<11>([this]() {
FrameBufferContext::GLScope glScope(nvgCtx().fbCtx(), GL_READ_FRAMEBUFFER); FrameBufferContext::GLScope glScope(nvgCtx().fbCtx(), GL_READ_FRAMEBUFFER);
nvgCtx().fbCtx().blitFrameBufferToScreen(viewport(), nvgCtx().fbCtx().getWindowSize(), isFrameBufferScaling()); nvgCtx().fbCtx().blitFrameBufferToScreen(viewport(), nvgCtx().fbCtx().getWindowSize(), isScaling());
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
glfwSwapBuffers(nvgCtx().fbCtx().getGLFWWindow()); glfwSwapBuffers(nvgCtx().fbCtx().getGLFWWindow());
#else #else
@ -514,7 +509,7 @@ void V4D::swapContextBuffers() {
run_sync_on_main<12>([this]() { run_sync_on_main<12>([this]() {
FrameBufferContext::GLScope glScope(nguiCtx().fbCtx(), GL_READ_FRAMEBUFFER); FrameBufferContext::GLScope glScope(nguiCtx().fbCtx(), GL_READ_FRAMEBUFFER);
nguiCtx().fbCtx().blitFrameBufferToScreen(viewport(), nguiCtx().fbCtx().getWindowSize(), isFrameBufferScaling()); nguiCtx().fbCtx().blitFrameBufferToScreen(viewport(), nguiCtx().fbCtx().getWindowSize(), isScaling());
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
glfwSwapBuffers(nguiCtx().fbCtx().getGLFWWindow()); glfwSwapBuffers(nguiCtx().fbCtx().getGLFWWindow());
#else #else
@ -539,7 +534,7 @@ bool V4D::display() {
run_sync_on_main<6>([&, this](){ run_sync_on_main<6>([&, this](){
{ {
FrameBufferContext::GLScope glScope(fbCtx(), GL_READ_FRAMEBUFFER); FrameBufferContext::GLScope glScope(fbCtx(), GL_READ_FRAMEBUFFER);
fbCtx().blitFrameBufferToScreen(viewport(), fbCtx().getWindowSize(), isFrameBufferScaling()); fbCtx().blitFrameBufferToScreen(viewport(), fbCtx().getWindowSize(), isScaling());
} }
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
nguiCtx().render(printFPS_, showFPS_); nguiCtx().render(printFPS_, showFPS_);

@ -114,6 +114,15 @@ make -j8
sudo make install sudo make install
``` ```
## Download models
```bash
mkdir assets
cd assets
wget https://github.com/kurnianggoro/GSOC2017/raw/master/data/lbfmodel.yaml
wget https://github.com/opencv/opencv_zoo/raw/main/models/face_detection_yunet/face_detection_yunet_2023mar.onnx
cd ..
```
## Download the example videos ## Download the example videos
```bash ```bash
# big buck bunny video # big buck bunny video

Loading…
Cancel
Save