|
| 1 | +import os |
| 2 | +import json |
| 3 | +import csv |
| 4 | +import numpy as np |
| 5 | +import random |
| 6 | + |
| 7 | + |
| 8 | +Work_Path = "F:\\PlantarPressurePredictExperiment" |
| 9 | +os.chdir(Work_Path) |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +def getDic(): |
| 14 | + with open("subjectConfig.json", "r",encoding= "utf-8") as f: |
| 15 | + subjectConfigDict = json.load(f) |
| 16 | + return subjectConfigDict |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +def GetKeyPoint(dic,subject,item,step): |
| 23 | + HC = dic[subject]["ResultData"][item][step].split(",")[0] |
| 24 | + SC = dic[subject]["ResultData"][item]["{0}SCValue".format(step)].split("_")[0] |
| 25 | + HL = dic[subject]["ResultData"][item]["{0}HLValue".format(step)].split("_")[0] |
| 26 | + TO = dic[subject]["ResultData"][item][step].split(",")[1] |
| 27 | + return int(HC),int(SC),int(HL),int(TO) |
| 28 | + |
| 29 | + |
| 30 | +def GetArr(subject,detailItemName,eachStep,HC,SC,HL,TO): |
| 31 | + file = "1" |
| 32 | + if(eachStep == "BTS" or eachStep == "TS"): |
| 33 | + file = open("ProcessedData\\subject{0}\\{1}-L.csv".format(subject,detailItemName),encoding="utf-8") |
| 34 | + elif(eachStep == "BAP" or eachStep == "AP" or eachStep == "DS"): |
| 35 | + file = open("ProcessedData\\subject{0}\\{1}-R.csv".format(subject,detailItemName),encoding="utf-8") |
| 36 | + else: |
| 37 | + print("Error",subject,detailItemName) |
| 38 | + |
| 39 | + Arr = np.loadtxt(file,delimiter = ",") |
| 40 | + HCArr = Arr[HC-1:SC-1,:] |
| 41 | + MSArr = Arr[SC:HL-1,:] |
| 42 | + TOArr = Arr[HL:TO-1,:] |
| 43 | + file.close() |
| 44 | + return HCArr,MSArr,TOArr |
| 45 | + |
| 46 | + |
| 47 | +def GetLabel(dic,subjectName,detailItemName): |
| 48 | + angle = dic[subjectName]["ResultData"][detailItemName]["angle"] |
| 49 | + strategy = dic[subjectName]["ResultData"][detailItemName]["strategy"] |
| 50 | + return angle,strategy |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +def GetFileIterator(dic): |
| 56 | + randomList = [1,2,3,4,5] |
| 57 | + isTest = False |
| 58 | + for subject in subjects: |
| 59 | + for item in items: |
| 60 | + for sub_item in sub_items: |
| 61 | + isTest = True if(random.choice(randomList) == 1) else False |
| 62 | + detailItemName = "{0}-{1}".format(item, sub_item) |
| 63 | + subjectName = "subject{}".format(subject) |
| 64 | + if (dic[subjectName]["ResultData"].__contains__(detailItemName)): |
| 65 | + for eachStep in Step:#每一个特征步 |
| 66 | + HC, SC, HL, TO = GetKeyPoint(dic, subjectName, detailItemName, eachStep) |
| 67 | + HCArr, MSArr, TOArr = GetArr(subject, detailItemName, eachStep, HC, SC, HL, TO) |
| 68 | + angle,strategy = GetLabel(dic,subjectName,detailItemName) |
| 69 | + #if(angle != "0°") : continue #不要30°和120°变化 |
| 70 | + for k in range(len(TOArr)-(SeleSequence-1)): #每两帧取一个图像 |
| 71 | + #yield subjectName,detailItemName,angle,strategy,eachStep,HCArr,MSArr,TOArr |
| 72 | + yield subjectName, detailItemName, angle, strategy, eachStep,TOArr[k]+TOArr[k+1],isTest |
| 73 | + print(subjectName,"---",detailItemName) |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +def switchLabelClass(label): |
| 79 | + if label == 0: |
| 80 | + return 0 |
| 81 | + if label ==30: |
| 82 | + return 1 |
| 83 | + if label ==60: |
| 84 | + return 2 |
| 85 | + if label == 90: |
| 86 | + return 3 |
| 87 | + if label == 120: |
| 88 | + return 4 |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + subjects = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"] |
| 94 | + items = ["1", "2", "3", "4", "5"] |
| 95 | + sub_items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] |
| 96 | + #Step = ["BAP", "BTS", "AP", "TS", "DS"] |
| 97 | + Step = ["AP"] |
| 98 | + |
| 99 | + SaveTrainDataFilePath = "Pytorch\\data\\angle\\TrainData.csv" |
| 100 | + SaveTrainLabelFilePath = "Pytorch\\data\\angle\\TrainLabel.csv" |
| 101 | + SaveTestDataFilePath = "Pytorch\\data\\angle\\TestData.csv" |
| 102 | + SaveTestLabelFilePath = "Pytorch\\data\\angle\\TestLabel.csv" |
| 103 | + |
| 104 | + SeleSequence = 2 #每两帧取一个图像 |
| 105 | + |
| 106 | + dic = getDic() |
| 107 | + TrainArr = [] |
| 108 | + TrainAngle = [] |
| 109 | + TestArr = [] |
| 110 | + TestAngle = [] |
| 111 | + |
| 112 | + for eachItem in GetFileIterator(dic): |
| 113 | + Arr = eachItem[5] |
| 114 | + for eachItem in GetFileIterator(dic): |
| 115 | + Arr = eachItem[5] |
| 116 | + label = int(eachItem[2].split("°")[0]) |
| 117 | + label = switchLabelClass(label) |
| 118 | + |
| 119 | + if(eachItem[6]): |
| 120 | + TestArr.append(Arr) |
| 121 | + TestAngle.append(label) |
| 122 | + else: |
| 123 | + TrainArr.append(Arr) |
| 124 | + TrainAngle.append(label) |
| 125 | + |
| 126 | + TrainArr = np.array(TrainArr) |
| 127 | + TrainAngle = np.array(TrainAngle) |
| 128 | + TestArr = np.array(TestArr) |
| 129 | + TestAngle = np.array(TestAngle) |
| 130 | + |
| 131 | + np.savetxt(SaveTrainDataFilePath,TrainArr,delimiter=",") |
| 132 | + np.savetxt(SaveTrainLabelFilePath, TrainAngle,fmt="%d", delimiter=",") |
| 133 | + |
| 134 | + np.savetxt(SaveTestDataFilePath, TestArr, delimiter=",") |
| 135 | + np.savetxt(SaveTestLabelFilePath, TestAngle,fmt="%d", delimiter=",") |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
0 commit comments