1
0

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 1 year has passed since last update.

mediapipeのpythonモジュールをビルドする

Posted at

概要

mediapipeのpythonモジュールをDocker環境でビルドし、別環境にpipを使ってインストールする手順を残しておく。

環境

Pythonのバージョンは3.8
インストール先はUbuntu22.04

手順

  1. mediapipe(https://github.com/google/mediapipe )をCloneする

    git clone https://github.com/google/mediapipe.git
    
  2. Cloneしてきたmediapipe内のDockerfileを使ってDockerイメージを作成する

    docker build --tag=mediapipe .
    
  3. 作成したDockerイメージを使って、mediapipeのサンプルプログラムが動くことを確認する

    docker run -it mediapipe:latest
    

    サンプルプログラム実行して、"Hello World!"が10回出力されたらOK

    GLOG_logtostderr=1 bazel run --define MEDIAPIPE_DISABLE_GPU=1 mediapipe/examples/desktop/hello_world
    

    (ここまでは、mediapipeのInstallationと同じ内容)
    (参考:https://developers.google.com/mediapipe/framework/getting_started/install#installing_using_docker)

  4. コンテナを抜けて、作成したモジュールを格納する用のディレクトリをマウントしつつ、再度起動する。

    docker run -it -v ./dist:/dist mediapipe:latest
    
  5. protbufをインストールする

    wget https://github.com/protocolbuffers/protobuf/releases/download/v23.1/protoc-23.1-linux-x86_64.zip  
    unzip protoc-23.1-linux-x86_64.zip -d protoc3  
    mv protoc3/bin/* /usr/local/bin/
    mv protoc3/include/* /usr/local/include/
    
  6. vimをインストールする

    apt update && apt install -y vim
    
  7. third_party/BUILDの中身を以下のように変更する
    とりあえず動くものができた設定なので、PyPIにあるものとは多分違うものができる

    cmake_external(
        name = "opencv_cmake",
        # Values to be passed as -Dkey=value on the CMake command line;
        # here are serving to provide some CMake script configuration options
        cache_entries = {
            "CMAKE_BUILD_TYPE": "Release",
            "BUILD_LIST": ",".join(sorted(OPENCV_MODULES)),
            "BUILD_TESTS": "OFF",
            "BUILD_PERF_TESTS": "OFF",
            "BUILD_EXAMPLES": "OFF",
            "BUILD_SHARED_LIBS": "ON" if OPENCV_SHARED_LIBS else "OFF",
            "WITH_ITT": "OFF",
            "WITH_JASPER": "OFF",
            "WITH_JPEG": "ON",
            "WITH_PNG": "ON",
            "WITH_TIFF": "OFF",
            "WITH_OPENCL": "OFF",
            "WITH_WEBP": "OFF",
            "WITH_IPP": "OFF",
            "WITH_FFMPEG": "OFF",
            "WITH_OPENEXR": "ON",
            # Optimization flags
            "CV_ENABLE_INTRINSICS": "ON",
            "WITH_EIGEN": "ON",
            "WITH_PTHREADS": "OFF",
            "WITH_PTHREADS_PF": "OFF",
            "OPENCV_SKIP_VISIBILITY_HIDDEN": "ON" if not OPENCV_SHARED_LIBS else "OFF",
            "OPENCV_SKIP_PYTHON_LOADER": "ON",
            "BUILD_opencv_python": "OFF",
            "ENABLE_CCACHE": "OFF",
        },    lib_source = "@opencv//:all",
        linkopts = [] if OPENCV_SHARED_LIBS else [
            "-lm",
            "-lpthread",
        ],
        shared_libraries = select({
            "@bazel_tools//src/conditions:darwin": ["libopencv_%s.%s.dylib" % (module, OPENCV_SO_VERSION) for module in OPENCV_MODULES],
            "//conditions:default": ["libopencv_%s.so.%s" % (module, OPENCV_SO_VERSION) for module in OPENCV_MODULES],
        }) if OPENCV_SHARED_LIBS else None,
        static_libraries = [
            "libopencv_%s.a" % module
            for module in OPENCV_MODULES
        ] if not OPENCV_SHARED_LIBS else None,
    )
    
  8. setup.pyの内の__version__を適当に書き換える

     __version__ = '1.11.1'
    
  9. ビルド実施。結構時間がかかる

    python3 setup.py bdist_wheel
    
  10. 完了したら、/mediapiepe/distにwheel ファイルがあるので、マウントしている/distにコピーし、

    cp /mediapipe/dist/*.whl /dist
    

    使用したい環境でインストールする。

    pip3 install mediapipe-xxx.whl
    

(参考)OpenCVのビルドオプション

手順7の設定に行き着くまでの流れとか、気が向いたら書く

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?