19
24

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

OpenPoseでの関節の座標

Last updated at Posted at 2018-11-30

OpenPoseでの関節の座標はどこで取得するのだろうと試行錯誤していたらようやく見つけました。

#関節の座標取得
tf_pose/estimator.pyに以下のような記述があります。

estimator.py
    @staticmethod
    def draw_humans(npimg, humans, imgcopy=False):
        if imgcopy:
            npimg = np.copy(npimg)
        image_h, image_w = npimg.shape[:2]
        centers = {}
        for human in humans:
            # draw point
            for i in range(common.CocoPart.Background.value):
                if i not in human.body_parts.keys():
                    continue

                body_part = human.body_parts[i]
                center = (int(body_part.x * image_w + 0.5), int(body_part.y * image_h + 0.5))
                centers[i] = center
                cv2.circle(npimg, center, 3, common.CocoColors[i], thickness=3, lineType=8, shift=0)

            # draw line
            for pair_order, pair in enumerate(common.CocoPairsRender):
                if pair[0] not in human.body_parts.keys() or pair[1] not in human.body_parts.keys():
                    continue

                # npimg = cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)
                cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)

        return npimg

#draw point #draw line とあるようにここで体の骨格を描いています。
ここに、

estimator.py
    @staticmethod
    def draw_humans(npimg, humans, imgcopy=False):
        if imgcopy:
            npimg = np.copy(npimg)
        image_h, image_w = npimg.shape[:2]
        centers = {}
        for human in humans:
            # draw point
            for i in range(common.CocoPart.Background.value):
                if i not in human.body_parts.keys():
                    continue

                body_part = human.body_parts[i]
                center = (int(body_part.x * image_w + 0.5), int(body_part.y * image_h + 0.5))
                centers[i] = center
                print(center)        #追加
                cv2.circle(npimg, center, 3, common.CocoColors[i], thickness=3, lineType=8, shift=0)

            # draw line
            for pair_order, pair in enumerate(common.CocoPairsRender):
                if pair[0] not in human.body_parts.keys() or pair[1] not in human.body_parts.keys():
                    continue

                # npimg = cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)
                cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)

        return npimg

と追加すると、体の関節位置の座標を取得することができます。

出力結果
{0: (1378, 176), 1: (1351, 352), 2: (1218, 307), 3: (1111, 470), 4: (1280, 385), 5: (1476, 398), 6: (1529, 554), 7: (1449, 443), 8: (1209, 743), 9: (1173, 1011), 11: (1387, 750), 12: (1324, 1057), 14: (1351, 150), 15: (1413, 163), 16: (1289, 170), 17: (1422, 333)}

0〜17までの数字はどの関節の座標であるかを指しています。調べたところ、
0:鼻、1:心臓、2:右肩、3:右肘、4:右手首、5:左肩、6:左肘、7:左手首、8:右腰、9:右膝、10:右足首、11:左腰、12:左膝、13:左足首、14:右目、15:左目、16:右耳、17:左耳
となっています。

また、x座標y座標をそれぞれ別に扱うことも可能です。
例として、6:左肘のx座標のみを出力するときは以下のようにします。

estimator.py
    @staticmethod
    def draw_humans(npimg, humans, imgcopy=False):
        if imgcopy:
            npimg = np.copy(npimg)
        image_h, image_w = npimg.shape[:2]
        x = {}     #追加
        y = {}     #追加
        centers = {}
        for human in humans:
            # draw point
            for i in range(common.CocoPart.Background.value):
                if i not in human.body_parts.keys():
                    continue

                body_part = human.body_parts[i]
                center = (int(body_part.x * image_w + 0.5), int(body_part.y * image_h + 0.5))
                x[i] = int(body_part.x * image_w + 0.5)     #追加
                y[i] = int(body_part.y * image_h + 0.5)     #追加
                centers[i] = center
                cv2.circle(npimg, center, 3, common.CocoColors[i], thickness=3, lineType=8, shift=0)

             print(x[6])   #追加

            # draw line
            for pair_order, pair in enumerate(common.CocoPairsRender):
                if pair[0] not in human.body_parts.keys() or pair[1] not in human.body_parts.keys():
                    continue

                # npimg = cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)
                cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)

        return npimg

print文はfor文の中に入れないように注意してください。
関節の座標が分かれば色々と応用できそうですね!

19
24
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
19
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?