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

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

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()




  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值