Mediapipe:摄像头获取人体坐标并实时输出

前言:

最近初学Mediapipe,小小记录一下。
本篇介绍了基于Mediapipe的摄像头获取人体坐标并实时输出的代码和结果,要求结果为133个数据为一行,第一个为时间,2-133为33个关节点的xyz和置信度v的值。
代码暂时放上来,具体注释和细节之后再补充。


代码如下:

import cv2
import mediapipe as mp
from tqdm import tqdm
import time
import numpy as np

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_holistic = mp.solutions.holistic

cap = cv2.VideoCapture(0)
with mp_holistic.Holistic(
        min_detection_confidence=0.5,
        min_tracking_confidence=0.5) as holistic:
    while cap.isOpened():
        success, image = cap.read()
        if not success:
            print("false")
            # If loading a video, use 'break' instead of 'continue'.
            continue

        image.flags.writeable = False
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        results = holistic.process(image)

        h, w, c = image.shape
        start_time = time.time()

        # 画图
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        mp_drawing.draw_landmarks(
            image,
            results.pose_landmarks,
            mp_holistic.POSE_CONNECTIONS,
            landmark_drawing_spec=mp_drawing_styles
                .get_default_pose_landmarks_style())

        if results.pose_landmarks:
              for index, landmarks in enumerate(results.pose_landmarks.landmark):
                      cx = int(results.pose_landmarks.landmark[index].x*w)
                      cy = int(results.pose_landmarks.landmark[index].y*h)
                      cz = results.pose_landmarks.landmark[index].z
                      v = results.pose_landmarks.landmark[index].visibility

                      filename = 'mediapipe_results.txt'
                      test = open(filename, 'a')

                      if (index <= 0):
                          print('\n', file=test)
                          print(time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()), end=',', file=test)
                      print(cx, ',', cy, ',', cz, ',', v, end=',', file=test)


        cv2.imshow('MediaPipe Holistic', cv2.flip(image, 1))
        if cv2.waitKey(5) & 0xFF == 27:
            break
cap.release()


结果图:

需要输出的结果为133个数据为一行,第一个为时间,2-133为33个关节点的xyz和置信度v的值。

其中每个数据用逗号隔开,4个为一组,方便做实验。

Mediapipe:摄像头获取人体坐标并实时输出_第1张图片


你可能感兴趣的:(计算机视觉,python,opencv,图像处理)