You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将33个人体关键点数据合并为单行并按规则添加时间戳?

解决方案

问题说明

当前代码将33个人体关键点的每个单独存为Excel一行,不符合需求。需要实现:

  • 每组33个关键点的所有X/Y/Z数据合并为同一行
  • 按组生成递增的Number编号(0、1、2...)
  • 为每组添加统一的时间戳

当前代码

def save_data_to_excel(self):
    data_dict = {pose_type: [] for pose_type in ['pose', 'right_hand', 'left_hand']}
    for pose_type, pose_data in self.alldata:
        for idx, (key, value) in enumerate(pose_data.items()):
            # Append timestamp along with position data
            timestamp = datetime.now().strftime("%H:%M:%S.%f")
            key_suffix = ['X', 'Y', 'Z']
            data_dict[pose_type].append({**{'Number': idx, 'Timestamp': timestamp}, **{f'{key}_{key_suffix[i]}': val for i, val in enumerate(value)}})
            #data_row = [idx, timestamp] + [{f'{key}_{key_suffix[i]}': val for i, val in enumerate(value)}]
            #data_dict[pose_type].append(data_row)

    with pd.ExcelWriter('koordinat.xlsx') as writer:
        for pose_type, data in data_dict.items():
            df = pd.DataFrame(data)
            df.to_excel(writer, sheet_name=pose_type, index=False)

修改后的代码

from datetime import datetime
import pandas as pd

def save_data_to_excel(self):
    data_dict = {pose_type: [] for pose_type in ['pose', 'right_hand', 'left_hand']}
    # 遍历每个类型的姿态数据
    for pose_type, pose_data in self.alldata:
        key_list = list(pose_data.keys())
        # 计算完整组数
        group_count = len(key_list) // 33
        
        # 处理每组33个关键点
        for group_idx in range(group_count):
            # 整组使用同一个时间戳
            timestamp = datetime.now().strftime("%H:%M:%S.%f")
            row_data = {'Number': group_idx, 'Timestamp': timestamp}
            
            # 遍历当前组的所有关键点,将坐标整合到同一行
            for key in key_list[group_idx*33 : (group_idx+1)*33]:
                coords = pose_data[key]
                key_suffix = ['X', 'Y', 'Z']
                row_data.update({f'{key}_{key_suffix[i]}': val for i, val in enumerate(coords)})
            
            data_dict[pose_type].append(row_data)
        
        # 处理剩余不足33个的关键点(可选,按需保留)
        remaining_keys = key_list[group_count*33:]
        if remaining_keys:
            timestamp = datetime.now().strftime("%H:%M:%S.%f")
            row_data = {'Number': group_count, 'Timestamp': timestamp}
            for key in remaining_keys:
                coords = pose_data[key]
                key_suffix = ['X', 'Y', 'Z']
                row_data.update({f'{key}_{key_suffix[i]}': val for i, val in enumerate(coords)})
            data_dict[pose_type].append(row_data)

    # 写入Excel文件
    with pd.ExcelWriter('koordinat.xlsx') as writer:
        for pose_type, data in data_dict.items():
            df = pd.DataFrame(data)
            df.to_excel(writer, sheet_name=pose_type, index=False)

关键改动说明

  • 按组批量处理:不再逐个遍历关键点,改为以33个为一组批量处理,确保一组数据存入同一行
  • 统一时间戳:每组生成一个时间戳,避免单个关键点重复生成时间
  • 递增组编号:用group_idx作为每组的Number值,从0开始依次递增
  • 行数据构建:为每组创建一个字典,逐步添加所有关键点的X/Y/Z字段,最终整行加入数据列表

内容的提问来源于stack exchange,提问作者Johanes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 07:31:05