😊

ビルド済み OpenPose の Python ライブラリを利用する

に公開

概要

OpenPose でリアルタイムな姿勢推定を行いたい場合、OpenPose のプロジェクトをビルドして、Python ライブラリを取得しなければならない。しかし、このビルドは非常に複雑で、ビルド環境を用意するだけでも非常に大変なので、公式サイトに公開されている Windows 用のビルド済パッケージを使用して、ライブラリを使用する。

環境

  • Windows 11 x64
  • Python 3.7.9(Python のインストール手順は省略)
  • CUDA 13.0(CUDA を使用して、GPU による処理を行う)
  • cuDNN 9.14.0
  • OpenPose 1.7.0

事前準備

1. Python のインストール

省略

2. CUDA のインストール

このリンクを使用して、ダウンロードページに移動。適切なプラットフォームを選択して、CUDA Toolkit Installer を取得する。インストーラを起動したら、インストールオプション「高速」を選択して、CUDA の最新バージョンをインストール。
以下のコマンドでインストールを確認できる。

nvcc --version

3. cuDNN のインストール

このリンクを使用して、ダウンロードページに移動。適切なプラットフォームを選択して Base Installer を取得する。インストーラを起動したら、インストールオプション「高速」を選択して、cuDNN の最新バージョンをインストール。インストールが完了したら、念のために PC を再起動。インストールをコマンドで確認する方法はない。C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\{version}というディレクトリ内にバージョン情報が記録されるようだ。
https://ai.desktop.creative.resanaplaza.com/【すぐできる】cuda-とcudnnのversion確認方法(windows)/

4. ビルド済み OpenPose のダウンロード

公式GitHub上の Release から、openpose-1.7.0-binaries-win64-gpu-python3.7-flir-3d_recommended.zipをクリックしてダウンロードする。Zip を解凍して、LICENSE などが入っているopenposeディレクトリを~/openposeにコピー。ディレクトリ構成は以下の通り。

~/openpose/
    ├ 3rdparty/
    ├ bin/
    ├ examples/
    ├ include/
    ├ lib/
    ├ models/
    ├ python/
    ├ Instructions.txt
    ├ LICENSE
    └ Version.txt

5. 必要なモジュールをインポート

pip install opencv-python

コード

~/openpose/pythonディレクトリに、サンプルコードが入っている。

openpose_python.py
# From Python
# It requires OpenCV installed for Python
import sys
import os
from sys import platform
import argparse

try:
    # Import Openpose (Windows/Ubuntu/OSX)
    dir_path = os.path.dirname(os.path.realpath(__file__))
    try:
        # Change these variables to point to the correct folder (Release/x64 etc.)
        sys.path.append(dir_path + '/../bin/python/openpose/Release')
        os.environ['PATH']  = os.environ['PATH'] + ';' + dir_path + '/../x64/Release;' +  dir_path + '/../bin;'
        import pyopenpose as op
    except ImportError as e:
        print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
        raise e

    # Flags
    parser = argparse.ArgumentParser()
    args = parser.parse_known_args()

    # Custom Params (refer to include/openpose/flags.hpp for more parameters)
    params = dict()
    params["model_folder"] = "../models/"

    # Add others in path?
    for i in range(0, len(args[1])):
        curr_item = args[1][i]
        if i != len(args[1])-1: next_item = args[1][i+1]
        else: next_item = "1"
        if "--" in curr_item and "--" in next_item:
            key = curr_item.replace('-','')
            if key not in params:  params[key] = "1"
        elif "--" in curr_item and "--" not in next_item:
            key = curr_item.replace('-','')
            if key not in params: params[key] = next_item

    # Construct it from system arguments
    # op.init_argv(args[1])
    # oppython = op.OpenposePython()

    # Starting OpenPose
    opWrapper = op.WrapperPython(op.ThreadManagerMode.Synchronous)
    opWrapper.configure(params)
    opWrapper.execute()
except Exception as e:
    print(e)
    sys.exit(-1)

実行してみる。

cd ~/openpose/python
python openpose_python.py

Discussion