0
点赞
收藏
分享

微信扫一扫

python mediapipe 读取视频画出每帧的手部骨骼图并保存

兽怪海北 2022-03-11 阅读 61

提取出每帧骨骼并画在空白图上

import cv2
import mediapipe as mp
import numpy as np

cap = cv2.VideoCapture("./dataset/accident_3.mp4")
# cap = cv2.VideoCapture("./Hands/video.mp4")
# cap = cv2.VideoCapture(0)

mpHands = mp.solutions.hands
hands = mpHands.Hands(static_image_mode=False,
                      max_num_hands=2,
                      min_detection_confidence=0.75,
                      min_tracking_confidence=0.5
                      )
mpDraw = mp.solutions.drawing_utils

#创建一个500*500,3颜色通道图片的numpy矩阵
img = np.zeros((500, 500, 3), np.uint8)

# 白色背景
img.fill(255)

#统计帧数
i = 0

while True:

    sussess, frame = cap.read()
    if not sussess:
        break
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = hands.process(frame)

    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            # print('hand_landmarks:',hand_landmarks)
            print("\n")
            m = hand_landmarks.landmark
            for id, lm in enumerate(m):
                print("x = {}, y = {}, z = {}".format(lm.x, lm.y, lm.z))
            # 关键点可视化
            mpDraw.draw_landmarks(
                img, hand_landmarks, mpHands.HAND_CONNECTIONS)

    cv2.imwrite('./test/' + str(i) + '.png', img)

    #清除之前线条
    img = np.zeros((500, 500, 3), np.uint8)
    img.fill(255)

    i = i + 1
    if cv2.waitKey(1) & 0xFF == 27:
        break
    print("共{}帧,第{}帧".format(cap.get(7), i))

cap.release()




举报

相关推荐

0 条评论