0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kurento用OpenCV Filter Moduleの作成

Last updated at Posted at 2018-10-16

概要

Kurento Media Serverで動作するFilter Moduleサンプルの作成とインストールを行う。(参考URL: http://doc-kurento.readthedocs.io/en/stable/user/writing_modules.html)
Kurento Media Serverのインストール方法はこちら

kurento-media-server-dev packageのインストール

Filter Moduleのscaffold作成アプリを使うので、kurento-media-server-dev
をインストールする。

$ sudo apt-get install -y kurento-media-server-dev

Custom Filter Moduleの作成

モジュール作成スクリプトを使ってOpenCV Filter Moduleのソースコードツリーを生成するには、次のコマンドを用いる。

$ kurento-module-scaffold.sh <module_name> <output_directory> opencv_filter

ここでは以下のようにコマンドを実行した。

$ cd ~
$ kurento-module-scaffold.sh CustomFilter ~/kms opencv_filter

CMakeLists.txt の編集

$ cd kms/custom-filter
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_INSTALL_PREFIX=/usr
$ vim ../CMakeLists.txt

CMakeLists.txt に以下の1行を追加。(これがないとエラーになる。)
# 追加する場所は 下から8行目あたりの、set(CMAKE_CXX_FLAGS …)の次の行。一番下はNG。
----------------- CMakeLists.txt -------------
set (CMAKE_POSITION_INDEPENDENT_CODE ON)
----------------------------------------------

Filterプログラムの本体の場所

フィルタの本体のソースコードは、~/kms/custom-filter/src/server/implementation/objectsにある4つのファイル。

  • CustomFilterImpl.cpp
  • CustomFilterImpl.hpp
  • CustomFilterOpenCVImpl.cpp
  • CustomFilterOpenCVImpl.hpp

このうち、CustomFilterImpl.cppCustomFilterImpl.hppは編集しない。
書き換えるのはCustomFilterOpenCVImpl.cppCustomFilterOpenCVImpl.hpp
スクリプトで作成されたソースコードは、フィルタの中身がなく実行すると例外を発生させるのでそのままでは動作しない。
ここでは、https://github.com/Kurento/kms-opencv-plugin-sample ので実装を参考に
CustomFilterOpenCVImpl.cppCustomFilterOpenCVImpl.hpp を作成する。

CustomrFilterOpenCVImpl.hpp
/* Autogenerated with kurento-module-creator */

# ifndef __CUSTOM_FILTER_OPENCV_IMPL_HPP__
# define __CUSTOM_FILTER_OPENCV_IMPL_HPP__

# include <OpenCVProcess.hpp>
# include "CustomFilter.hpp"
# include <EventHandler.hpp>

namespace kurento
{
namespace module
{
namespace customfilter
{

class CustomFilterOpenCVImpl : public virtual OpenCVProcess
{

public:

    CustomFilterOpenCVImpl ();

    virtual ~CustomFilterOpenCVImpl () {};

    virtual void process (cv::Mat &mat);

    void setFilterType (int filterType);
    void setEdgeThreshold (int edgeValue);

  private:

    int filterType;
    int edgeValue;
};

} /* customfilter */
} /* module */
} /* kurento */

# endif /*  __CUSTOM_FILTER_OPENCV_IMPL_HPP__ */
CustomFilterOpenCVImpl.cpp
/* Autogenerated with kurento-module-creator */

# include "CustomFilterOpenCVImpl.hpp"
# include <KurentoException.hpp>

using namespace cv;

namespace kurento
{
namespace module
{
namespace customfilter
{

CustomFilterOpenCVImpl::CustomFilterOpenCVImpl ()
{
  this->filterType = 0;
  this->edgeValue = 125;
}

/*
 * This function will be called with each new frame. mat variable
 * contains the current frame. You should insert your image processing code
 * here. Any changes in mat, will be sent through the Media Pipeline.
 */
void CustomFilterOpenCVImpl::process (cv::Mat &mat)
{
  cv::Mat matBN (mat.rows, mat.cols, CV_8UC1);
  cv::cvtColor(mat, matBN, COLOR_BGRA2GRAY);

  if (filterType == 0) {
    Canny (matBN, matBN, edgeValue, 125);
  }
  cvtColor (matBN, mat, COLOR_GRAY2BGRA);
}

void CustomFilterOpenCVImpl::setFilterType (int filterType)
{
  this->filterType = filterType;
}

void CustomFilterOpenCVImpl::setEdgeThreshold (int edgeValue)
{
  this->edgeValue = edgeValue;
}

} /* customfilter */
} /* module */
} /* kurento */

Custom Filterのビルドとインストール

Filter Moduleのビルドとインストール

$ cd ~/kms/custom_filter/build
$ cmake .. -DCMAKE_INSTALL_PREFIX=/usr
$ make clean
$ make
$ sudo make install

filter moduleのインストールの確認

$ kurento-media-server -v
Kurento Media Server version: 6.7.1
Found modules:
	'core' version 6.7.1
	'customfilter' version 0.0.1~0.gf7bf8f6
	'elements' version 6.7.1
	'filters' version 6.7.1

JAVAライブラリのビルドとmavenリポジトリへのインストール

$ cd ~/kms/custom-filter/build
$ cmake .. -DGENERATE_JAVA_CLIENT_PROJECT=TRUE
$ make
$ make java_install

JAVAライブラリのリポジトリへの登録されていることを確認する。jarファイルがあればOK。

$ cd ~/.m2/repository/org/kurento/module/customfilter
$ ls
0.0.1-SNAPSHOT
$ cd 0.0.1-SNAPSHOT
$ ls
customfilter-0.0.1-SNAPSHOT.jar  maven-metadata-local.xml
customfilter-0.0.1-SNAPSHOT.pom  _remote.repositories

サーバーを再起動

$ sudo service kurento-media-server restart

フィルターの雛形の作成は、以上。
続いて、[作成したフィルタをKurento Media Serverのアプリから呼び出しの設定]を行う。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?