Summary of Kurento Custom OpenCV Module Development Method (3)

Summary of Kurento Custom OpenCV Module Development Method (3)

(2016-02-23 10:09:07)
      In the previous two blog posts, we introduced the development method of Opencv type modules in Kurento and implemented a simple edge detection module. Looking back at the previous code, we defined two private member variables in the class OpencvPluginSampleOpenCVImpl: filterType and edgeValue, where filterType is 0 to perform edge detection, and edgeValue is a small threshold used to control edge connections in the Canny algorithm. In the previous code, we set the default values ​​of these two members directly in the constructor of OpencvPluginSampleOpenCVImpl, which is hard-coded in the code. So the question is, some people may want to set the default values ​​of these two parameters in the configuration file to control the module more flexibly, how to achieve this? In this blog post, we will go a step further and see how to configure parameters for custom modules through configuration files.

      Before introducing the implementation, let's take a look at how Kurento's built-in modules implement parameter configuration. The configuration files of the Kurento module are stored in the /etc/kurento/modules directory by default. After the Kurento installation is completed, there will be a subdirectory named kurento, which stores the configuration files of the existing built-in modules. The file format for ini. When the Kurento server starts, it will automatically scan and load the contents of these configuration files. You can see the relevant information in the Kurento log file, so if you want to configure the custom module, you also need to place a corresponding configuration in this directory. document.

# ls -l / etc / current / modules / current /
-rw-r--r-- 1 root root  29 Nov 25 19:19 BaseRtpEndpoint.conf.ini
-rw-r--r-- 1 root root 350 Nov 25 19:41 HttpEndpoint.conf.ini
-rw-r--r-- 1 root root  24 Nov 25 19:19 MediaElement.conf.ini
-rw-r--r-- 1 root root 618 Nov 25 19:19 SdpEndpoint.conf.json
-rw-r--r-- 1 root root  36 Nov 25 19:19 UriEndpoint.conf.ini
-rw-r - r-- 1 root root 511 Nov 25 19:41 WebRtcEndpoint.conf.ini

2016-02-23 10:50:56,227714 20082 [0x00007f9d4d7dd8c0] info KurentoLoadConfig loadConfig.cpp:164 loadModulesConfigFromDir() Looking for config files in /etc/kurento/modules
2016-02-23 10:50:56,227774 20082 [0x00007f9d4d7dd8c0] info KurentoLoadConfig loadConfig.cpp:164 loadModulesConfigFromDir() Looking for config files in /etc/kurento/modules/kurento
2016-02-23 10:50:56,227910 20082 [0x00007f9d4d7dd8c0] info KurentoLoadConfig loadConfig.cpp:189 loadModulesConfigFromDir() Loaded module config from: /etc/kurento/modules/kurento/MediaElement.conf.ini
2016-02-23 10:50:56,228021 20082 [0x00007f9d4d7dd8c0] info KurentoLoadConfig loadConfig.cpp:189 loadModulesConfigFromDir() Loaded module config from: /etc/kurento/modules/kurento/BaseRtpEndpoint.conf.ini

      First, create a configuration file to add parameter content. We add a subdirectory opencvpluginsample for custom modules in the /etc/kurento/modules directory , and then create a file OpencvPluginSample.conf.ini in this subdirectory. The subdirectory name and configuration file name here must be consistent with the module name, otherwise the custom module cannot obtain the configuration in it. After the configuration file is created, add the configuration parameters and parameter values ​​we want to it, as follows:

# sudo mkdir /etc/kurento/modules/opencvpluginsample
# sudo touch /etc/kurento/modules/opencvpluginsample/OpencvPluginSample.conf.ini

# cat /etc/kurento/modules/opencvpluginsample/OpencvPluginSample.conf.ini 
defaultFilterType=0
defaultEdgeValue=25

      设置完配置参数之后,重启一下Kurento服务器,在启动日志中可以看到新添加的配置文件已经被正常加载了。

2016-02-23 10:50:56,228927 20082 [0x00007f9d4d7dd8c0] info KurentoLoadConfig loadConfig.cpp:189 loadModulesConfigFromDir() Loaded module config from: /etc/kurento/modules/opencvpluginsample/OpencvPluginSample.conf.ini"
...
{
  {
    opencvpluginsample":
    {
       "OpencvPluginSample":
       {
         "defaultFilterType": "0", 
         "defaultEdgeValue": "25", 
         "configPath": "\/etc\/kurento\/modules\/opencvpluginsample"
        }
    }
 }
}

      其次,修改自定义模块代码读取参数配置。配置参数已经由Kurento加载完成了,那么我们的自定义模块如何才能读取到这些参数值呢?在之前的博文中我们自动生成的代码涉及两个类:OpencvPluginSampleImpl和OpencvPluginSampleOpenCVImpl,图像处理的功能主要放在 OpencvPluginSampleOpenCVImpl这个类中,而 OpencvPluginSampleImpl这个类基本上没怎么修改,现在就是 OpencvPluginSampleImpl这个类发挥作用的时候了。在 OpencvPluginSampleImpl.hpp这个文件中,我们看到这个类的构造函数声明如下,第一个参数的类型是boost::property_tree::ptree,而property_tree就是boost库中专门为配置文件而写的,支持xml,ini和json格式文件。任何配置文件中的参数值均是config这个参数传进来的,通过它即可获取所有参数值。

OpencvPluginSampleImpl (const boost::property_tree::ptree& config, std::shared_ptr mediaPipeline);

      以下是为了读取配置参数做的所有代码改动,我们先把 OpencvPluginSampleOpenCVImpl类中的两个私有成员变量重新声明为protected,这样在 OpencvPluginSampleImpl类的构造函数中就可以直接修改这两个成员变量的值了。

class OpencvPluginSampleOpenCVImpl : public virtual OpenCVProcess
{
public:
  OpencvPluginSampleOpenCVImpl ();
  virtual ~OpencvPluginSampleOpenCVImpl () {};
  virtual void process (cv::Mat& mat);
  void setFilterType (int filterType);
  void setEdgeThreshold (int edgeValue);
protected:
  int filterType;
  int edgeValue;
};

      接下来修改OpencvPluginSampleImpl.cpp文件中的内容,主要就是在构造函数中增加对两个参数值的获取,使用到的函数为getConfigValue,该函数接收两个参数:配置参数名称和参数默认值,即如果要读取的参数没有在文件中配置的话就使用该默认值。完成这些代码修改后,重新编译自定义模块并重启Kurento服务器,此时我们的自定义模块就可以正常使用配置文件中的配置参数了。

static const std::string DEFAULT_FILTER_TYPE = "defaultFilterType";
static const std::string DEFAULT_EDGE_VALUE = "defaultEdgeValue";

OpencvPluginSampleImpl::OpencvPluginSampleImpl (const boost::property_tree::ptree &config, std::shared_ptr mediaPipeline) : OpenCVFilterImpl (config, std::dynamic_pointer_cast (mediaPipeline) )
{
    filterType = getConfigValue (DEFAULT_FILTER_TYPE, 0);
    edgeValue = getConfigValue (DEFAULT_EDGE_VALUE, 125);
}

      Finally, modify the parameter value in the configuration file to verify the effect. Now that the custom module can control the default value of the small , we might as well modify the value in the configuration file to verify whether the custom module really uses the configuration parameters from the file. The following figure is a comparison of the revisions when the value of defaultEdgeValue is set to 25 and 125. It is not difficult to see that the difference is quite large, which indicates that the custom module does use the configuration parameters from the file.

Summary of Kurento Custom OpenCV Module Development Method (3)
The edge detection effect when the defaultEdgeValue value is set to 25

Summary of Kurento Custom OpenCV Module Development Method (3)
The edge detection effect when the defaultEdgeValue value is set to 125

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324632513&siteId=291194637