OpenCVをEmscriptenでwasmにビルドする!Webブラウザから呼び出す。OpenCV.js

OpenCVをEmscriptenでwasmにビルドする!Webブラウザから呼び出す。OpenCV.js

2018-09-164 min read

目次

  1. 概要
  2. opencvjsの紹介
  3. opencvjsのビルド
  4. 終わりに

概要

OpenCVをLLVM→EmscriptenでWebAssembly(以下、wasm)をビルドする。\n JavaScriptからOpenCVを呼び出す。

環境 使用するもの

OpenCV4.0.0(pre)

https://github.com/opencv/opencv

Emscripten

https://kripken.github.io/emscripten-site/index.html

LLVM

https://llvm.org/

OpenCV.jsの紹介

About

OpenCV.jsに関する詳細は公式でも詳しく書かれています。

https://docs.opencv.org/3.3.1/df/d0a/tutorial_js_intro.html

Introduction to OpenCV.js and Tutorials - 公式

LLVMとEmscripten

Emscripten is an LLVM-to-JavaScript compiler. It takes LLVM bitcode - which can be generated from C/C++ using clang, and compiles that into asm.js or WebAssembly that can execute directly inside the web browsers. . Asm.js is a highly optimizable, low-level subset of JavaScript. Asm.js enables ahead-of-time compilation and optimization in JavaScript engine that provide near-to-native execution speed. WebAssembly is a new portable, size- and load-time-efficient binary format suitable for compilation to the web. WebAssembly aims to execute at native speed. WebAssembly is currently being designed as an open standard by W3C.

Introduction to OpenCV.js and Tutorials\n OpenCV.js: OpenCV for the JavaScript programmer\n https://docs.opencv.org/3.3.1/df/d0a/tutorial_js_intro.html

公式から引用。

Emscriptenとは、任意のプログラミング言語に対してのコンパイラ基盤である「LLVM」から「JavaScript」へのコンパイラです。LLVMを用いて、C/C++から生成されるLLVMコードをWebブラウザから直接実行できるasm.jsまたはWasmにコンパイルします。

チュートリアル

OpenCV introduces a new set of tutorials that will guide you through various functions available in OpenCV.js. This guide is mainly focused on OpenCV 3.x version.

The purpose of OpenCV.js tutorials is to:

  1. Help with adaptability of OpenCV in web development\n
  2. Help the web community, developers and computer vision researchers to interactively access a variety of web-based OpenCV examples to help them understand specific vision algorithms.

Because OpenCV.js is able to run directly inside browser, the OpenCV.js tutorial web pages are intuitive and interactive. For example, using WebRTC API and evaluating JavaScript code would allow developers to change the parameters of CV functions and do live CV coding on web pages to see the results in real time.

Prior knowledge of JavaScript and web application development is recommended to understand this guide.

Introduction to OpenCV.js and Tutorials\n OpenCV.js Tutorials\n https://docs.opencv.org/3.3.1/df/d0a/tutorial_js_intro.html

OpenCV.jsはブラウザ内で直接実行できます。\n 例えば、WebRTCとOpenCVを組み合わせたアプリケーションを、リアルタイムで結果を見ながら実装することができるようなメリットがあるようです。

OpenCV.jsのビルド

ここではOpenCV.jsのビルド方法について紹介します。

環境のセットアップ

nodejs,python,cmakeがなどが必要となります。

# Install Python
sudo apt install python2.7

# Install node.js
sudo apt install nodejs

# Install CMake
sudo apt install cmake

Emscripten

Emscripten 導入手順

任意のディレクトリに移動しgitからcloneします。

git clone https://github.com/juj/emsdk.git
cd emsdk

emsdkコマンドを利用して最新のツールをインストールします。\n 完了したら、パスを通します。

git pull
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh

実行結果がこちら。

# result
Adding directories to PATH:
PATH += /path/to/emsdk
PATH += /path/to/emsdk/clang/e1.38.12_64bit
PATH += /path/to/emsdk/node/8.9.1_64bit/bin
PATH += /path/to/emsdk/emscripten/1.38.12

Setting environment variables:
EMSDK = /path/to/emsdk
EM_CONFIG = /Users/username/.emscripten
LLVM_ROOT = /path/to/emsdk/clang/e1.38.12_64bit
EMSCRIPTEN_NATIVE_OPTIMIZER = /path/to/emsdk/clang/e1.38.12_64bit/optimizer
BINARYEN_ROOT = /path/to/emsdk/clang/e1.38.12_64bit/binaryen
EMSDK_NODE = /path/to/emsdk/node/8.9.1_64bit/bin/node
EMSCRIPTEN = /path/to/emsdk/emscripten/1.38.12

これで完了です。\n パスを通すことでshell変数にパスが格納されます。

echo ${EMSCRIPTEN}
# /path/to/emsdk/emscripten/1.38.12

OpenCVの準備

gitでopencvをcloneします。

git clone https://github.com/opencv/opencv.git

クローンが完了したらpythonからビルドスクリプトを実行します。

公式では詳しく書かれていませんでしたが、\n 自分の環境ではPython3系で実行すると転びました。なので2.7で実行しました。

cd opencv/platforms/js/
ls ./
# README.md   build_js.py

build_jsディレクトリを構築する場合

python ./build_js.py build_js

上記のコマンドは、デフォルトでasm.jsをビルドします。\n wasmバージョンを作成するには、--build_wasmオプションを追加します。

python ./build_wasm --build_wasm

そのほかにも --build_doc や --build_testオプションなどが存在します。

python ./build_js.py build_js

正しくインストールされれば以下のようにファイルが生成されています。

ls build_wasm/bin
# opencv.js      opencv_js.js   opencv_js.wasm

テスト

上にも書いたように、--build_testオプションでテストを行うことができます。

build_js.py build_js --build_test
cd bin
npm install
node test.js

nodejsでコマンドラインからテストの結果を確認。

ブラウザからテスト結果を確認\n http://localhost:8080/tests.htmlでサーバをスタート

こんな形でビルド結果を確認することができます。\n 1件エラーがあるのが気になりますね。

終わりに

近いうちにJSでの実装に関する記事を書きたいと思います。\n 以上、よろしく

Tags
javascript(109)
linux(54)
node.js(53)
amazon%20aws(47)
typescript(44)
%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0(36)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86(30)
html5(29)
php(24)
centos(24)
python(22)
%E7%AB%B6%E6%8A%80%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0(21)
mac(21)
mysql(20)
canvas(19)
opencv(17)
%E9%9B%91%E8%AB%87(16)
docker(16)
wordpress(15)
atcoder(14)
apache(12)
%E6%A9%9F%E6%A2%B0%E5%AD%A6%E7%BF%92(12)
%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9(12)
amazon%20s3(12)
red%20hat(12)
prisma(12)
ubuntu(11)
github(10)
git(10)
vue.js(10)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86100%E6%9C%AC%E3%83%8E%E3%83%83%E3%82%AF(10)
mariadb(10)
react(9)
aws%20cdk(9)
css3(8)
%E5%8F%AF%E8%A6%96%E5%8C%96(8)
%E5%B0%8F%E3%83%8D%E3%82%BF(8)
nestjs(8)
amazon%20lightsail(7)
next.js(7)
%E3%83%96%E3%83%AD%E3%82%B0(6)
cms(6)
oracle(6)
perl(6)
gitlab(6)
iam(5)
amazon%20ec2(5)
%E8%B3%87%E6%A0%BC%E8%A9%A6%E9%A8%93(5)
aws%20amplify(5)
curl(4)
Author
githubzennqiita
ただの備忘録です。

※外部送信に関する公表事項