Instantly share code, notes, and snippets.

Embed
What would you like to do?
openpose data conversion function
function openpose2csv(sheetname,flg){
//sheetname = openpose data sheet, flg = "body" or "face"
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheet=ss.getSheetByName(sheetname);
var endrow=sheet.getLastRow();
logarray=sheet.getRange(1,1,endrow,2).getValues();
var jsonData;
var resultarray = [];
var fieldarray = [];
var resultarrayNum = 0;
var m;
var i;
var b;
var s;
var test=0;
//read openpose data row by row
for(i=0; i<endrow; i++) {
jsonData=JSON.parse(logarray[i][1]);
if(flg=="body"){
for(m=0;m<jsonData["people"].length;m++){
scorecheck=0;
resultarray[resultarrayNum]=[];//0:datetime,1:bodynum,2-55:openposebodykeypoints(18pointsXYscore):
//resultarray definition
// {0, "Nose"},2,3,4
// {1, "Neck"},5,6,7
// {2, "RShoulder"},8,9,10
// {3, "RElbow"},11,12,13
// {4, "RWrist"},14,15,16
// {5, "LShoulder"},17,18,19
// {6, "LElbow"},20,21,22
// {7, "LWrist"},23,24,25
// {8, "RHip"},26,27,28
// {9, "RKnee"},29,30,31
// {10, "RAnkle"},32,33,34
// {11, "LHip"},35,36,37
// {12, "LKnee"},38,39,40
// {13, "LAnkle"},41,42,43
// {14, "REye"},44,45,46
// {15, "LEye"},47,48,49
// {16, "REar"},50,51,52
// {17, "LEar"},53,54,55
//DateTime humanNum Nose X Nose Y Nose score Neck X Neck Y Neck score RShoulder X RShoulder Y RShoulder score RElbow X RElbow Y RElbow score RWrist X RWrist Y RWrist score LShoulder X LShoulder Y LShoulder score LElbow X LElbow Y LElbow score
//LWrist X LWrist Y LWrist score RHip X RHip Y RHip score RKnee X RKnee Y RKnee score RAnkle X RAnkle Y RAnkle score LHip X LHip Y LHip score LKnee X LKnee Y LKnee score LAnkle X LAnkle Y LAnkle score REye X REye Y REye score LEye X LEye Y LEye score
//initialize..
for(b=0;b<60;b++){
if(b%3==2 && b>1)fieldarray[b]=(b-b%3)/3+"_X";
if(b%3==0 && b>1)fieldarray[b]=(b-b%3-3)/3+"_Y";
if(b%3==1 && b>1)fieldarray[b]=(b-b%3-3)/3+"_score";
resultarray[resultarrayNum][b]=0;
}
fieldarray[0]="DateTime";
fieldarray[1]="humanNum";
fieldarray[56]="confidencemax";
fieldarray[57]="minX";
fieldarray[58]="maxX";
fieldarray[59]="minY";
fieldarray[60]="maxY";
resultarray[resultarrayNum][0]=logarray[i][0].substr(0,23);
resultarray[resultarrayNum][1]=m;
boundingminX=10000;
boundingminY=10000;
boundingmaxX=0;
boundingmaxY=0;
confidencemax=0;
//read body parts data(X, Y, confidence score)
for(s=0;s<jsonData["people"][m]["pose_keypoints"].length;s++){
resultarray[resultarrayNum][s+2]=jsonData["people"][m]["pose_keypoints"][s];
if(s%3==0 && jsonData["people"][m]["pose_keypoints"][s]>0){
if(boundingminX>jsonData["people"][m]["pose_keypoints"][s])boundingminX=jsonData["people"][m]["pose_keypoints"][s];
if(boundingmaxX<jsonData["people"][m]["pose_keypoints"][s])boundingmaxX=jsonData["people"][m]["pose_keypoints"][s];
}
if(s%3==1 && jsonData["people"][m]["pose_keypoints"][s]>0){
if(boundingminY>jsonData["people"][m]["pose_keypoints"][s])boundingminY=jsonData["people"][m]["pose_keypoints"][s];
if(boundingmaxY<jsonData["people"][m]["pose_keypoints"][s])boundingmaxY=jsonData["people"][m]["pose_keypoints"][s];
}
if(s%3==2 && jsonData["people"][m]["pose_keypoints"][s]>0){
if(confidencemax<jsonData["people"][m]["pose_keypoints"][s])confidencemax=jsonData["people"][m]["pose_keypoints"][s];
}
}
resultarray[resultarrayNum][56]=confidencemax;//confidencemax
resultarray[resultarrayNum][57]=boundingminX;//boundingbox minX
resultarray[resultarrayNum][58]=boundingmaxX;//boundingbox maxX
resultarray[resultarrayNum][59]=boundingminY;//boundingbox mimY
resultarray[resultarrayNum][60]=boundingmaxY;//boundingbox maxY
resultarrayNum++;
}
}
if(flg=="face"){
for(m=0;m<jsonData["people"].length;m++){
scorecheck=0;
resultarray[resultarrayNum]=[];//0:datetime,1:bodynum,2-212:openposebodykeypoints(18pointsXYscore):
for(b=0;b<216;b++){
if(b%3==2 && b>1)fieldarray[b]=(b-b%3)/3+"_X";
if(b%3==0 && b>1)fieldarray[b]=(b-b%3-3)/3+"_Y";
if(b%3==1 && b>1)fieldarray[b]=(b-b%3-3)/3+"_score";
resultarray[resultarrayNum][b]=0;
}
fieldarray[0]="DateTime";
fieldarray[1]="humanNum";
fieldarray[212]="confidencemax";
fieldarray[213]="minX";
fieldarray[214]="maxX";
fieldarray[215]="minY";
fieldarray[216]="maxY";
resultarray[resultarrayNum][0]=logarray[i][0].substr(0,23);
resultarray[resultarrayNum][1]=m;
boundingminX=10000;
boundingminY=10000;
boundingmaxX=0;
boundingmaxY=0;
confidencemax=0;
for(s=0;s<jsonData["people"][m]["face_keypoints"].length;s++){
resultarray[resultarrayNum][s+2]=jsonData["people"][m]["face_keypoints"][s];
if(s%3==0 && jsonData["people"][m]["face_keypoints"][s]>0){
if(boundingminX>jsonData["people"][m]["face_keypoints"][s])boundingminX=jsonData["people"][m]["face_keypoints"][s];
if(boundingmaxX<jsonData["people"][m]["face_keypoints"][s])boundingmaxX=jsonData["people"][m]["face_keypoints"][s];
}
if(s%3==1 && jsonData["people"][m]["face_keypoints"][s]>0){
if(boundingminY>jsonData["people"][m]["face_keypoints"][s])boundingminY=jsonData["people"][m]["face_keypoints"][s];
if(boundingmaxY<jsonData["people"][m]["face_keypoints"][s])boundingmaxY=jsonData["people"][m]["face_keypoints"][s];
}
if(s%3==2 && jsonData["people"][m]["face_keypoints"][s]>0){
if(confidencemax<jsonData["people"][m]["face_keypoints"][s])confidencemax=jsonData["people"][m]["face_keypoints"][s];
}
}
resultarray[resultarrayNum][212]=confidencemax;//confidencemax
resultarray[resultarrayNum][213]=boundingminX;//boundingbox minX
resultarray[resultarrayNum][214]=boundingmaxX;//boundingbox maxX
resultarray[resultarrayNum][215]=boundingminY;//boundingbox mimY
resultarray[resultarrayNum][216]=boundingmaxY;//boundingbox maxY
resultarrayNum++;
}
}
}
//sort by date
resultarray = resultarray.sort();
//add field
resultarray2 = resultarray.unshift(fieldarray)
return resultarray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment