11
3

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 3 years have passed since last update.

macOSかつpyenvとpipenvの環境でOpenVinoを動かす(2020版)

Last updated at Posted at 2020-05-19

#コレは何?
macOS Catalina上で、pyenvをpipenvも使っているとう状況でopenvinoをうまく動かす方法についての覚書。
「macOS CatalinaでOpenVinoを動かす」の続編です。特に仮想環境とか使ってない&&わりと環境構築とか得意な人はそちらをご参照ください。
こちらは、上記環境利用中の初心者を対象として、内容をブラッシュアップしたものです。
※2021/5更新:2020.3LTSリリースに対応

#環境
macOS Catalina(10.15.7)
OpenVino(2020.3.341)
Python 3.7.7
cmake 3.19.1

#全般における注意事項
OpenVinoをインストールし、チュートリアル通りに動かすと、謹製のopencvにパスを通したり、勝手にpipを使ったりするので、うっかりするとpyenvやpipenvを使っている意味がなくなってしまいます。いわれたとおりにせずソースをまず見るべし。
多少面倒ですが、$ source /opt/intel/openvino/bin/setupvars.sh なぞは.bash_profileなどに書かずに、ターミナルを起動するたびにチクチク実行した方が他の開発環境とコンフリクトする可能性が低くなります。

#OpenVinoのダウンロードとインストール

ここからWEB & Localを選択し、macOS用のdmgをダウンロードしてください。
ここでは、2021バージョンではなく2020.3.1 LTSを用います。必ず2020.3.1 LTSをDLしてください。

通常はrootユーザーでインストールします。
完了するとwebページが表示されますが、ここではまだチュートリアルのデモを実行してはいけません!

#プロジェクト環境の構築
プロジェクトディレクトリを作成し、pyenvとpipenvの設定をします。
OpenVinoでのpythonの推奨バージョンは3.6か3.7ですので、ここではpyenvで3.7.7をインストールします。

$ pyenv install 3.7.7
$ mkdir ~/Documents/openvino-projects
$ cd ~/Documents/openvino-projects

チュートリアルのデモや、その他のデモで使ういくつかのライブラリをまとめました。
以下の内容をPipfileという名前で、プロジェクトディレクトリ(ここでは~/Documents/openvino-projects)に置きます。

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
numpy = "*"
pyyaml = "*"
requests = "*"
scipy = "*"

[requires]
python_version = "3.7"

仮想環境を構築し、シェルに入ります。

$ pyenv local 3.7.7
$ pipenv --python 3.7.7
........略
$ pipenv install
# ここでLocking..のまま永遠に終わらない場合は、(あまりオススメできないが)やむをえんので、強制終了したのち、
pipenv install --skip-lock とする。
........略
$ pipenv shell

# シェルに入ったらopenvinoの環境設定ファイルを実行
$ source /opt/intel/openvino/bin/setupvars.sh
[setupvars.sh] OpenVINO environment initialized

#チュートリアルを実行
インストール後に表示されるウェブページに書かれたチュートリアルデモでは、モデルのダウンロード、変換、実行までやってくれるスクリプトを実行するだけなのですが、これが余計なことをしてくれるので少し修正します。

$ cd /opt/intel/openvino/deployment_tools/demo/

demo_squeezenet_download_convert_run.shの146-150行目をコメントアウトして保存します(root権限が必要)。

demo_squeezenet_download_convert_run.sh
#if [[ "$OSTYPE" == "darwin"* ]]; then
#    $pip_binary install -r $ROOT_DIR/../open_model_zoo/tools/downloader/requirements.in
#else
#    sudo -E $pip_binary install -r $ROOT_DIR/../open_model_zoo/tools/downloader/requirements.in
#fi

実行。

$ ./demo_squeezenet_download_convert_run.sh
........略

同様にdemo_security_barrier_camera.shの119〜123行目もコメントアウトし、保存・実行します。

$ ./demo_security_barrier_camera.sh
........略

エラーが出ず、車両の画像が出ればOK

#C++のデモを実行する

デモファイルの入ったディレクトリをプロジェクトディレクトリにコピーします。

$ cp -r /opt/intel/openvino/deployment_tools/open_model_zoo/demos .

cmakeします。pythonで3D pose-estimationを使う場合はENABLE_PYTHONをONする必要がありますが、ここではやりません。

$ cd demos
$ cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_PYTHON=OFF .
......略

しかし、このままではコンパイルが通らないので、CmakeCache.txt(34行目)を編集します。

CMakeCache.txt
・・・・・略・・・・・
//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=-Wno-inconsistent-missing-override
・・・・・略・・・・・

makeします。

$ make -j4
・・・・・略・・・・・

intel64/Release以下に実行ファイルがビルドされています。
crossroad_camera_demoを実行してみましょう。
まずは、各サンプルの説明ページを見ましょう。

crossroad_camera_demoは、ここのページです。とりあえずよく読みましょう。
まずは、必要なモデルファイルのダウンロードが必要です。
以下のようにして、必要なモデルをダウンロードします。

$ cd ~/Documents/openvino-projects
$ /opt/intel/openvino/deployment_tools/open_model_zoo/tools/downloader/downloader.py --name person-vehicle-bike-detection-crossroad-0078 --precision FP16
・・・・・略・・・・・
$ /opt/intel/openvino/deployment_tools/open_model_zoo/tools/downloader/downloader.py --name person-attributes-recognition-crossroad-0230 --precision FP16
・・・・・略・・・・・
$ /opt/intel/openvino/deployment_tools/open_model_zoo/tools/downloader/downloader.py --name person-reidentification-retail-0031 --precision FP16
・・・・・略・・・・・

openvino-projects/intel/ 以下にモデルがダウンロードされます。
crossroad_camera_demoは入力にビデオファイルかカメラが使えますが、PCカメラだと通行人を撮るのに適していないので適当な映像ファイルを使いましょう。
デモに有用なリソースはここにあります

ここではpeople-detection.mp4を使ってみます。

$ mkdir sample-videos
# people-detection.mp4をダウンロードして、sample-videosフォルダに入れる。

$ ./demos/intel64/Release/crossroad_camera_demo \
-i ./sample-videos/people-detection.mp4 \
-m ./intel/person-vehicle-bike-detection-crossroad-0078/FP16/person-vehicle-bike-detection-crossroad-0078.xml \
-m_pa ./intel/person-attributes-recognition-crossroad-0230/FP16/person-attributes-recognition-crossroad-0230.xml \
-m_reid ./intel/person-reidentification-retail-0031/FP16/person-reidentification-retail-0031.xml \
-d CPU -d_pa CPU -d_reid CPU

このデモでは、人物の検知と属性情報の推定、そして、トラッキング(reidentification)を別々のモデルを使っておこないます。

もしうまくいかない場合

以下の内容をupdate_rpath.shという名前でプロジェクトディレクトリに保存します。

update_rpath.sh
install_name_tool -add_rpath '/opt/intel/openvino/opencv/lib' /opt/intel/openvino/python/python3/cv2.so
install_name_tool -add_rpath '/opt/intel/openvino/inference_engine/lib/intel64' /opt/intel/openvino/python/python3/cv2.so
install_name_tool -add_rpath '/opt/intel/openvino/deployment_tools/ngraph/lib' /opt/intel/openvino/python/python3/cv2.so
install_name_tool -add_rpath '/opt/intel/openvino/inference_engine/external/tbb/lib' /opt/intel/openvino/deployment_tools/inference_engine/lib/intel64/libinference_engine_legacy.dylib
install_name_tool -add_rpath '/opt/intel/openvino/inference_engine/lib/intel64' /opt/intel/openvino/deployment_tools/inference_engine/lib/intel64/libMKLDNNPlugin.dylib
install_name_tool -add_rpath '/opt/intel/openvino/inference_engine/lib/intel64' /opt/intel/openvino/python/python3.7/openvino/inference_engine/ie_api.so
install_name_tool -add_rpath '/opt/intel/openvino/inference_engine/external/tbb/lib' /opt/intel/openvino/python/python3.7/openvino/inference_engine/ie_api.so
install_name_tool -add_rpath '/opt/intel/openvino/deployment_tools/ngraph/lib' /opt/intel/openvino/deployment_tools/inference_engine/lib/intel64/libinference_engine.dylib
install_name_tool -add_rpath '/opt/intel/openvino/deployment_tools/ngraph/lib' /opt/intel/openvino/deployment_tools/inference_engine/lib/intel64/libinference_engine_legacy.dylib

上記のファイルを実行します(なぜこれをするかなど、詳しくは前投稿参照)。

$ cd ~/Documents/openvino-projects
$ chmod 755 ./update_rpath.sh
$ sudo ./update_rpath.sh

#Pythonデモを実行する

python用のデモは、demos/python_demos/ 以下にあります。
FaceRecogintionのデモを実行してみましょう。
FaceRecogintionの説明ページはこちら
同様に、必要なモデルファイルをダウンロードして実行します。今度はPCカメラを使います。
このデモでは--run_detectorオプションで、簡単な顔認証機能が使えます。対象となる顔写真を撮影しておきます。

$ cd ~/Documents/openvino-projects
$ mkdir face-data
#自分の写真を撮り、myname.jpgなどとし、face-dataに入れておく。

# modelのダウンロード(プロジェクトディレクトリ直下で実行)
$ /opt/intel/openvino/deployment_tools/open_model_zoo/tools/downloader/downloader.py --name face-detection-retail-0004 
$ /opt/intel/openvino/deployment_tools/open_model_zoo/tools/downloader/downloader.py --name landmarks-regression-retail-0009
$ /opt/intel/openvino/deployment_tools/open_model_zoo/tools/downloader/downloader.py --name face-reidentification-retail-0095

# デモの実行
$ python3 demos/python_demos/face_recognition_demo/face_recognition_demo.py \
-m_fd ./intel/face-detection-retail-0004/FP16/face-detection-retail-0004.xml \
-m_lm ./intel/landmarks-regression-retail-0009/FP16/landmarks-regression-retail-0009.xml \
-m_reid ./intel/face-reidentification-retail-0095/FP16/face-reidentification-retail-0095.xml \
--verbose --run_detector -fg ./face-data

起動すると、別画面で撮影した顔写真に名前を入れるよう求められます。適当に名前を入力します。
次に別の画像(もとから入っているもの?)が表示されますので、これにも適当に答えます。

カメラが起動し、自分の画像を認識します。

11
3
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
11
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?