在PIE数据集上用openpose提取的关键点特征保存到pkl。

# -*- codeing = utf-8 -*-
# @Time :2022/7/24 下午7:43
# @Author :刘泉恺
# @File : dgf.py
# @Software :PyCharm
# 全自动实现利用openpose提取PIE数据集上骨骼关键点数据包括坐标,置信度,标号,并进行bu零和归一化处理

import xml.etree.ElementTree as ET
import cv2
import math
import copy
import numpy as np
from src import util
from src.body import Body
from src.hand import Hand
import pickle
def loop_root(num):
    set_path = 'set03'
    if len(str(num)) >1:
        video_path = 'video_00'+ str(num)
    else:
        video_path = 'video_000' + str(num)
    print(video_path)
    f = '/media/lqk/数据集/PIE/PIE_dataset/annotations/'+set_path +'/' + video_path + '_annt.xml'
    img_path = '/media/lqk/数据集/PIE/PIE_dataset/images/'+set_path +'/' + video_path
    save_path = '/mnt/code/datasets/PIE/images/'+set_path +'/' + video_path
    body_estimation = Body('model/body_pose_model.pth')
    hand_estimation = Hand('model/hand_pose_model.pth')
    f_pkl = open(set_path+'_' +video_path+'.pkl', 'wb')
    tree = ET.parse(f)
    root = tree.getroot()
    print('root.tag:', root.tag)
    pose_data = []
    id = 0
    id_test = ''
    kp_txt = []
    for i in root: # 计算根下面的标签数
        a = i.attrib
        for key, values in a.items():  # 判断是否为行人标签
            if values == 'pedestrian':
                # print('i.tag:',i.tag)
                # print('i.attrib', i.attrib) # attrib属性是标签下面的键值对
                for j in i :
                    b = j.attrib
                    #
                    # print('  j.tag:', j.tag )
                    # print('  j.attrib',j.attrib)
                    # print('  j.text:',j.text)    # 遍历行人下的box
                    for k in j:
                        c = k.attrib
                        for key, values in c.items():
                            if values == 'id':
                                if k.text != id_test:
                                    id = id + 1
                                # print('    k.attrib', k.attrib)
                                # print('    k.text:', k.text ) # 遍历行人id
                                id_test = k.text
                                kp_txt.insert(0, id_test)
                                # print(id_test)
                    # a = int(id_test[4:])
                    # print(int(id_test[5:]))
                ##################### 测试用
                                # print(int(id_test[4:]))
                                # if int(id_test[4:]) > 115:
                                #     flag = 1
                                # else:
                                #     flag = 0
                    # if flag == 1:
                    #     continue
                    try:

                        for key, values in b.items():
                            if key == 'frame':
                            # print('frame:',values ) # 获得片段数
                                frame = values
                            # sheet.append(values)
                            if key == 'xbr':
                                xbr = float(values)
                            if key == 'xtl':
                                xtl = float(values)
                            if key == 'ybr':  #### 遇到线索不料savepath,切换注释
                                ybr = float(values)
                            if key == 'ytl':
                                ytl = float(values)
                            if key == 'ybr':#### 遇到线索不料savepath,切换注释
                                ybr = float(values)
                                if len(frame) != 5:
                                    frame = '0' + frame
                                frame_path = img_path + '/' + frame + '.png'
                                img = cv2.imread(frame_path)
                            # print(img, type(img))
                            # cv2.imshow("M8", img)
                            # cv2.waitKey(0)
                            #     print('frame_path 为:',frame_path)
                            # print('ybr'in dir()
                            #     print(ytl,ybr,xtl,xbr)
                                if img is None: # 图片有缺失,跳过截图
                                    # print(frame, '此图像没有人')
                                    continue

                                img_people = img[math.floor(ytl):math.ceil(ybr),math.floor(xtl):math.ceil(xbr)]
                                save_img_path = save_path + '/'+id_test +'_'+ frame +'.png'
                                print('save_image_path为 : ', save_img_path )
                            #出现异常跳过的文件,大部分为宽度像素小于十
                            #############################################################################
                            # if save_img_path =='/mnt/code/datasets/PIE/images/set01/video_0002/06212.png':
                            #     continue
                            # if save_img_path =='/mnt/code/datasets/PIE/images/set01/video_0002/14091.png':
                            #     continue
                            # if save_img_path =='/mnt/code/datasets/PIE/images/set01/video_0002/14092.png':
                            #     continue
                            # if save_img_path =='/mnt/code/datasets/PIE/images/set01/video_0002/06865.png':
                            #     continue
                            # if save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0002/06865.png':
                            #     continue
                            # if save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0002/06866.png':
                            #     continue
                            # if save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0002/06737.png':
                            #     continue
                            # if save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0002/07296.png':
                            #     continue
                            # if save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0002/07297.png':
                            #     continue
                            # if save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0002/07908.png':
                            #     continue
                            # if save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0002/08754.png':
                            #     continue
                            # if save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0002/12569.png':
                            #     continue
                            # if save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0002/12570.png':
                            #     continue
                            ###############################################################################
                            # if id_test == '1_3_64' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_64_03821.png':
                            #     continue
                            # if id_test == '1_3_64' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_64_03822.png':
                            #     continue
                            # if id_test == '1_3_64' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_64_03823.png':
                            #     continue
                            # if id_test == '1_3_65' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_65_03726.png':
                            #     continue
                            # if id_test == '1_3_66' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_66_03764.png':
                            #     continue
                            # if id_test == '1_3_66' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_66_03765.png':
                            #     continue
                            # if id_test == '1_3_67' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_67_03786.png':
                            #     continue
                            # if id_test == '1_3_67' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_67_03787.png':
                            #     continue
                            # if id_test == '1_3_67' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_67_03788.png':
                            #     continue
                            # if id_test == '1_3_72' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_72_10158.png':
                            #     continue
                            # if id_test == '1_3_72' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_72_10159.png':
                            #     continue
                            # if id_test == '1_3_73' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_73_10144.png':
                            #     continue
                            # if id_test == '1_3_73' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_73_10145.png':
                            #     continue
                            # if id_test == '1_3_73' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_73_10146.png':
                            #     continue
                            # if id_test == '1_3_74' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_74_10196.png':
                            #     continue
                            # if id_test == '1_3_76' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_76_09421.png':
                            #     continue
                            # if id_test == '1_3_76' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_76_09422.png':
                            #     continue
                            # if id_test == '1_3_76' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_76_09423.png':
                            #     continue
                            # if id_test == '1_3_76' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_76_10164.png':
                            #     continue
                            # if id_test == '1_3_76' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_76_10165.png':
                            #     continue
                            # if id_test == '1_3_76' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_76_10166.png':
                            #     continue
                            # if id_test == '1_3_77' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_77_10152.png':
                            #     continue
                            # if id_test == '1_3_77' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_77_10154.png':
                            #     continue
                            # if id_test == '1_3_77' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_77_10153.png':
                            #     continue
                            # if id_test == '1_3_81' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_81_12520.png':
                            #     continue
                            # if id_test == '1_3_82' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_82_12905.png':
                            #     continue
                            # if id_test == '1_3_85' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_85_13211.png':
                            #     continue
                            # if id_test == '1_3_85' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_85_13212.png':
                            #     continue
                            # if id_test == '1_3_88' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_88_15057.png':
                            #     continue
                            # if id_test == '1_3_104' and save_img_path =='/mnt/code/datasets/PIE/images/set01/video_0003/1_3_104_13366.png':
                            #     continue
                            #
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09860.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09861.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09862.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09863.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09864.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09865.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09866.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09867.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09868.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09869.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09870.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09871.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09872.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09873.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_09966.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_10194.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_10195.png':
                            #     continue
                            # if id_test == '1_3_70' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_70_10196.png':
                            #     continue
                            # if id_test == '1_3_93' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_93_16651.png':
                            #     continue
                            # if id_test == '1_3_93' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_93_16652.png':
                            #     continue
                            # if id_test == '1_3_95' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_95_16810.png':
                            #     continue
                            # if id_test == '1_3_98' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_98_17557.png':
                            #     continue
                            # if id_test == '1_3_98' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_98_17558.png':
                            #     continue
                            # if id_test == '1_3_98' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_98_17817.png':
                            #     continue
                            # if id_test == '1_3_98' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_98_17818.png':
                            #     continue
                            # if id_test == '1_3_99' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_99_17821.png':
                            #     continue
                            # if id_test == '1_3_99' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_99_17822.png':
                            #     continue
                            # if id_test == '1_3_79' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_79_11605.png':
                            #     continue
                            # if id_test == '1_3_79' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_79_11606.png':
                            #     continue
                            # if id_test == '1_3_79' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_79_11607.png':
                            #     continue
                            # if id_test == '1_3_79' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_79_11608.png':
                            #     continue
                            # if id_test == '1_3_79' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_79_11609.png':
                            #     continue
                            # if id_test == '1_3_75' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_75_10177.png':
                            #     continue
                            # if id_test == '1_3_75' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_75_10178.png':
                            #     continue
                            # if id_test == '1_3_75' and save_img_path == '/mnt/code/datasets/PIE/images/set01/video_0003/1_3_75_10179.png':
                            #     continue


                            # h, w, _ = img_people.shape
                            # if w < 12:
                            #     continue

                            ############################################   #调用mediapose

                                oriImg = img_people
                                # print(oriImg.shape)
                                candidate, subset = body_estimation(oriImg)
                                canvas = copy.deepcopy(oriImg)
                                canvas, kp = util.draw_bodypose(canvas, candidate, subset)
                            #######################################################提取关键点并进行补全,归一画处理
                                h, w, _ = oriImg.shape
                                if len(candidate) == 0:
                                    kp = np.delete(kp, 0, 0)
                                    for i in range(18):
                                        bc = np.array([0, 0, 0, i])
                                        kp = np.row_stack((kp, bc))
                                else:

                                    for i in range(18):
                                        if i < len(kp):
                                            if kp[i][3] != i:
                                                bc = np.array([0, 0, 0, i])
                                                np.insert(kp, i, bc, axis=0)
                                        else:
                                            bc = np.array([0, 0, 0, i])
                                            kp = np.row_stack((kp, bc))
                                guiyi = np.array([w, h, 1, 1])  # u is h
                                kp = kp / guiyi
                            # kp_txt =kp.reshape(1,72)
                                kp_txt= kp.tolist()
                                kp_txt.insert(0,frame)
                            # for i in kp_txt:
                                cv2.imwrite(save_img_path, canvas)
                                frame_path = img_path  # 为下次循环准备,复原路径
                                save_img_path = save_path # 为下次循环准备,复原路径
                    except Exception as e: # 异常处理
                        print(e)
                        print(f'error file:{e.__traceback__.tb_frame.f_globals["__file__"]}')
                        print(f"error line:{e.__traceback__.tb_lineno}")
                        print('发生错误')
                        continue

                    pose_data.append(kp_txt)
                    # print(pose_data)
    pickle.dump(pose_data,f_pkl,-1)
#关闭文件
    f_pkl.close()
    print(pose_data)

if __name__ == "__main__":
    num = 1
    for i in range(10):

        try:
            loop_root(num)

        except Exception as e:
            print(e)
            print(f'error file:{e.__traceback__.tb_frame.f_globals["__file__"]}')
            print(f"error line:{e.__traceback__.tb_lineno}")
            print('外部发生错误')
            continue
        num = num+1
     # loop_root(9)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值