How can I read text files and compile a joint XY Position history
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello eveyone,
I am struggling with finding a solution to my problem. I have numerous a json text files with XY position co-ordinates obtained from a 2D human pose estimation. I would like to read and use particular data keypoints from the XY coordinates for each JSON file in order to plot joint angle history for example. Is there a way to convert the data into an array? I have been struggling with numerous Matlab functions (fgets, fscanf) albeit with no luck and I am a bit lost.
The below data is what is contained on the numerous JSON files I have (a json file for each frame captured during the pose estimation video). The XY data below indicates the XY coordinate for each pose keypoint with a confidence reading/percentage for how accurate the pose estimation software believes that XY coordinate is. For example for the first keypoint for the Nose, the XY coordinate (with its confidence reading) is 314.19, 94.238, 0.934092 (both pieces of info are in BOLD in the data below.
{"version":1.3,"people":[{"person_id":[-1],"pose_keypoints_2d":[314.19,94.238,0.934092,305.601,109.546,0.891856,288.369,111.457,0.862389,268.27,130.557,0.860192,280.744,125.806,0.860478,322.797,106.684,0.84053,353.393,92.3152,0.859181,375.415,77.0249,0.859859,310.392,186.998,0.760913,296.043,186.045,0.705134,277.88,246.273,0.866712,234.798,229.088,0.834373,325.659,187.963,0.724311,354.392,233.851,0.880398,343.864,294.145,0.808459,309.387,88.5102,0.968337,317.993,89.4436,0.927254,295.042,87.5593,0.901007,0,0,0,364.873,300.81,0.505551,362.036,297.926,0.442485,338.115,300.781,0.724548,213.795,238.663,0.719225,212.839,233.886,0.699098,232.92,223.355,0.603927],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]}]}
{"version":1.3,"people":[{"person_id":[-1],"pose_keypoints_2d":[314.19,94.238,0.934092,305.601,109.546,0.891856,288.369,111.457,0.862389,268.27,130.557,0.860192,280.744,125.806,0.860478,322.797,106.684,0.84053,353.393,92.3152,0.859181,375.415,77.0249,0.859859,310.392,186.998,0.760913,296.043,186.045,0.705134,277.88,246.273,0.866712,234.798,229.088,0.834373,325.659,187.963,0.724311,354.392,233.851,0.880398,343.864,294.145,0.808459,309.387,88.5102,0.968337,317.993,89.4436,0.927254,295.042,87.5593,0.901007,0,0,0,364.873,300.81,0.505551,362.036,297.926,0.442485,338.115,300.781,0.724548,213.795,238.663,0.719225,212.839,233.886,0.699098,232.92,223.355,0.603927],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]}]}
[dpb edit -- added copy as code so the select button will work to get the text if folks want to try their hand at it...]
description of each XY coordinate: (not in json file)
# {0, "Nose"},
# {1, "Neck"},
# {2, "RShoulder"},
# {3, "RElbow"},
# {4, "RWrist"},
# {5, "LShoulder"},
# {6, "LElbow"},
# {7, "LWrist"},
# {8, "MidHip"},
# {9, "RHip"},
# {10, "RKnee"},
# {11, "RAnkle"},
# {12, "LHip"},
# {13, "LKnee"},
# {14, "LAnkle"},
# {15, "REye"},
# {16, "LEye"},
# {17, "REar"},
# {18, "LEar"},
# {19, "LBigToe"},
# {20, "LSmallToe"},
# {21, "LHeel"},
# {22, "RBigToe"},
# {23, "RSmallToe"},
# {24, "RHeel"},
# {25, "Background"}
1 Commento
dpb
il 28 Feb 2021
Modificato: dpb
il 28 Feb 2021
Attach a file, not the representation of a file...
But, in the meantime, you can try
fname = 'youFile.json';
val = jsondecode(fileread(fname));
That may be excessively optimistic; I've yet to learn anything about JSON so have no idea about how clever the TMW decode function is having never used it. The example given is so trivially simple in content as to be ludicrous -- cellstr() would return the same result as does it.
Risposta accettata
dpb
il 28 Feb 2021
Well, it does seem to "just work" -- almost. jsondecode doesn't know about the 2D array bit, but it did decode the JSON info block --
I put the data block into a file named 'pose.json', and then:
>> vals=jsondecode(fileread('pose.json'))
vals =
struct with fields:
version: 1.3000
people: [1×1 struct]
>> vals.people
ans =
struct with fields:
person_id: -1
pose_keypoints_2d: [75×1 double]
face_keypoints_2d: []
hand_left_keypoints_2d: []
hand_right_keypoints_2d: []
pose_keypoints_3d: []
face_keypoints_3d: []
hand_left_keypoints_3d: []
hand_right_keypoints_3d: []
>>
Don't see anything inside the JSON itself that identifies the 3-vector for each point, but given that outside information you provided, then:
>> keypoints=reshape(vals.people.pose_keypoints_2d,3,[]).';
>> keypoints(1:10,:)
ans =
314.1900 94.2380 0.9341
305.6010 109.5460 0.8919
288.3690 111.4570 0.8624
268.2700 130.5570 0.8602
280.7440 125.8060 0.8605
322.7970 106.6840 0.8405
353.3930 92.3152 0.8592
375.4150 77.0249 0.8599
310.3920 186.9980 0.7609
296.0430 186.0450 0.7051
>>
Looks like it works as advertised. Amazing!!! :)
1 Commento
dpb
il 28 Feb 2021
Or, turn it into a table--
>> keypoints=array2table(reshape(vals.people.pose_keypoints_2d,3,[]).','VariableNames',{'PoseX','PoseY','PoseErr'});
>> head(keypoints)
ans =
8×3 table
PoseX PoseY PoseErr
______ ______ _______
314.19 94.238 0.93409
305.6 109.55 0.89186
288.37 111.46 0.86239
268.27 130.56 0.86019
280.74 125.81 0.86048
322.8 106.68 0.84053
353.39 92.315 0.85918
375.42 77.025 0.85986
>>
Più risposte (1)
Ross Norman
il 2 Mar 2021
Modificato: Ross Norman
il 3 Mar 2021
1 Commento
dpb
il 3 Mar 2021
You can either use a timetable or timesries where each row is the time and column is the node or leave organized by row by node and make each column of the table a time.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!