3
9

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.

OpenPoseのPython APIの使用方法

Last updated at Posted at 2020-08-09

OpenPoseのPython APIの簡単な使用方法を解説します

Python APIの導入手順(Windows)は以下の記事で解説しております
https://qiita.com/hac-chi/items/0e6f910a9b463438fa81

公式のPython APIサンプルコードはこちらにあります
サンプルコードを解説しているだけですので、ソース読んだ方が速い方はそちらを参照したほうが良いと思います
https://github.com/CMU-Perceptual-Computing-Lab/openpose/tree/master/examples/tutorial_api_python

#OpenPoseの開始

    # Starting OpenPose
    opWrapper = op.WrapperPython()
    opWrapper.configure(params)
    opWrapper.start()

のようにして、使用を開始します

ここで渡しているparamsは、辞書型です。
OpenPoseを使用するにあたってのさまざまな設定をparamsで渡します。

たとえばモデルのパスの指定は以下のように

    params = dict()
    params["model_folder"] = "../../../models/"

以下にモデルのパラメーター一覧と、デフォルト値が記載されております
https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/include/openpose/flags.hpp

個人的に重要そうなのをピックしました
(model_poseをBODY_25以外に変更する場合、modelsフォルダのバッチファイルやシェルスクリプトを実行し、COCOやMPI用のモデルを取得してください)


DEFINE_int32(number_people_max,         -1,             "This parameter will limit the maximum number of people detected, by keeping the people with"
                                                        " top scores. The score is based in person area over the image, body part score, as well as"
                                                        " joint score (between each pair of connected body parts). Useful if you know the exact"
                                                        " number of people in the scene, so it can remove false positives (if all the people have"
                                                        " been detected. However, it might also include false negatives by removing very small or"
                                                        " highly occluded people. -1 will keep them all.");

DEFINE_string(model_pose,               "BODY_25",      "Model to be used. E.g., `BODY_25` (fastest for CUDA version, most accurate, and includes"
                                                        " foot keypoints), `COCO` (18 keypoints), `MPI` (15 keypoints, least accurate model but"
                                                        " fastest on CPU), `MPI_4_layers` (15 keypoints, even faster but less accurate).");

DEFINE_bool(3d,                         false,          "Running OpenPose 3-D reconstruction demo: 1) Reading from a stereo camera system."
                                                        " 2) Performing 3-D reconstruction from the multiple views. 3) Displaying 3-D reconstruction"
                                                        " results. Note that it will only display 1 person. If multiple people is present, it will"
                                                        " fail.");

DEFINE_string(write_json,               "",             "Directory to write OpenPose output in JSON format. It includes body, hand, and face pose"
                                                        " keypoints (2-D and 3-D), as well as pose candidates (if `--part_candidates` enabled).");

DEFINE_string(udp_host,                 "",             "Experimental, not available yet. IP for UDP communication. E.g., `192.168.0.1`.");

DEFINE_string(udp_port,                 "8051",         "Experimental, not available yet. Port number for UDP communication.");

#画像の受け渡し

画像を読み込みます
注意なのですが、PILは使えず、OpenCVを使ってくださいとのことです

公式に以下のように記述があります

Do not use PIL
In order to read images in Python, make sure to use OpenCV (do not use PIL). We found that feeding a PIL image format to OpenPose results in the input image appearing in grey and duplicated 9 times (so the output skeleton appear 3 times smaller than they should be, and duplicated 9 times).
出典:OpenPose Python Module and Demo

読み込み方法ですが
まず、データの受け渡し用のオブジェクトををop.Datum()で作成し
OpenCVで読み込んだ画像をdatum.cvInputDataに格納します
そして、opWrapper.emplaceAndPopにリストとして渡しましょう
opWrapper.emplaceAndPopに返り値はありませんが、渡したdatum内に解析結果(出力画像、関節位置等々)が含まれています

    datum = op.Datum()
    imageToProcess = cv2.imread("image_path")
    datum.cvInputData = imageToProcess
    opWrapper.emplaceAndPop([datum]) 

#出力結果の表示

#関節座標    
print("Body keypoints:" + str(datum.poseKeypoints))    

#関節を表示した画像
cv2.imshow("Output Image", datum.cvOutputData)
cv2.waitKey(0)

他にも、datumの中には

datum.faceKeypoints   #顔の各パーツの座標
datum.handKeypoints[0]#左手の各パーツの座標
datum.handKeypoints[1]#右手の各パーツの座標

等々、色々と含まれています

詳しくは、以下を参照してください
https://cmu-perceptual-computing-lab.github.io/openpose/html/structop_1_1_datum.html

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?