実現したいこと
https://zenn.dev/collabostyle/articles/21ebb9ac52c744#discuss
上記のサイトを参考にYolov8n-pose.ptのモデルを使用して姿勢検知をしています。
サイトではウェブカメラを使用していますが、今回は動画を使用して姿勢推定とキーポイントを出力しています。初心者でわからないことが多く、説明が下手なところがあるかもしれませんがアドバイスを頂けますと幸いです。
発生している問題・分からないこと
①出力結果は表示されるが、動画の保存ができない(エラーなし)
出力結果の動画が表示されるが、その動画の保存ができておらず、拡張子のないファイルができてしまいます。
②出力結果の内容が理解できない
出力結果が出るのですが、一部わからないところがあります。動画は高さ720×幅1280のサイズで、結果には384x640と出ているのがどういうことなのかわかりません。keypointのX、Yは720×1280の座標が出ているように見えます。
該当のソースコード
from ultralytics import YOLO import cv2 # モデル読み込み model = YOLO("yolov8n-pose.pt") # 入力画像 capture = cv2.VideoCapture('person.mp4') # 動画ファイル保存用の設定 fps = int(capture.get(cv2.CAP_PROP_FPS)) w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') video = cv2.VideoWriter('person_keypoints', fourcc, fps, (w, h),True) # keypointの位置毎の名称定義 KEYPOINTS_NAMES = [ "nose", # 0 "eye(L)", # 1 "eye(R)", # 2 "ear(L)", # 3 "ear(R)", # 4 "shoulder(L)", # 5 "shoulder(R)", # 6 "elbow(L)", # 7 "elbow(R)", # 8 "wrist(L)", # 9 "wrist(R)", # 10 "hip(L)", # 11 "hip(R)", # 12 "knee(L)", # 13 "knee(R)", # 14 "ankle(L)", # 15 "ankle(R)", # 16 ] while capture.isOpened(): success, frame = capture.read() if success: # 推論を実行 results = model(frame) annotatedFrame = results[0].plot() # 検出オブジェクトの名前、バウンディングボックス座標を取得 names = results[0].names classes = results[0].boxes.cls boxes = results[0].boxes for box, cls in zip(boxes, classes): name = names[int(cls)] x1, y1, x2, y2 = [int(i) for i in box.xyxy[0]] if len(results[0].keypoints) == 0: continue # 姿勢分析結果のキーポイントを取得する keypoints = results[0].keypoints confs = keypoints.conf[0].tolist() # 推論結果:1に近いほど信頼度が高い xys = keypoints.xy[0].tolist() # 座標 for index, keypoint in enumerate(zip(xys, confs)): score = keypoint[1] # スコアが0.5以下なら描画しない if score < 0.5: continue x = int(keypoint[0][0]) y = int(keypoint[0][1]) print( f"Keypoint Name={KEYPOINTS_NAMES[index]}, X={x}, Y={y}, Score={score:.4}" ) # 紫の四角を描画 annotatedFrame = cv2.rectangle( annotatedFrame, (x, y), (x + 3, y + 3), (255, 0, 255), cv2.FILLED, cv2.LINE_AA, ) # キーポイントの部位名称を描画 annotatedFrame = cv2.putText( annotatedFrame, KEYPOINTS_NAMES[index], (x + 5, y), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 0, 255), thickness=2, lineType=cv2.LINE_AA, ) print("------------------------------------------------------") cv2.imshow("YOLOv8 human pose estimation", annotatedFrame) if cv2.waitKey(1) & 0xFF == ord("q"): break else: break capture.release() cv2.destroyAllWindows
0: 384x640 1 person, 76.9ms Speed: 1.0ms preprocess, 76.9ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640) Keypoint Name=nose, X=643, Y=324, Score=0.9783 Keypoint Name=eye(L), X=688, Y=301, Score=0.9741 Keypoint Name=eye(R), X=622, Y=290, Score=0.9636 Keypoint Name=ear(L), X=750, Y=339, Score=0.8752 Keypoint Name=ear(R), X=590, Y=310, Score=0.7085 Keypoint Name=shoulder(L), X=812, Y=528, Score=0.9023 Keypoint Name=shoulder(R), X=494, Y=475, Score=0.9481 ------------------------------------------------------
試したこと・調べたこと
上記の詳細・結果
①自分で調べて追記したものです。自分で書き換えているうち設定が抜け落ちてしまっているかもしれません。
②調べましたがよくわかりませんでした。
補足
ultralytics 8.1.25
python 3.11.5
opencv-python 4.9.0.80

0 コメント