Skip to content

Instantly share code, notes, and snippets.

@farrajota
Last active December 27, 2023 13:45
Show Gist options
  • Save farrajota/95a34a7a15452b32949c6b79c458d437 to your computer and use it in GitHub Desktop.
Save farrajota/95a34a7a15452b32949c6b79c458d437 to your computer and use it in GitHub Desktop.
Convert mpii annotations from .mat to .json format
function mpii_convert_json( )
% convert mpii annotations .mat file to .json
%% load annotation file
fprintf('Load annotations... ')
data = load('/media/HDD2/Datasets/Human_Pose/mpii/mpii_human_pose_v1_u12_2/mpii_human_pose_v1_u12_1.mat');
fprintf('Done.\n')
%% open file
fprintf('Open file mpii_human_pose_annotations.json\n')
path = '/media/HDD2/Datasets/Human_Pose/mpii/mpii_human_pose_v1_u12_2/';
fileID = fopen(strcat(path, 'mpii_human_pose_annotations.json'),'w');
%% cycle all data and save to file (.json)
fprintf(fileID, '"{');
% annolist
fprintf(fileID, '\\"annolist\\":[');
for i=1:1:size(data.RELEASE.annolist,2)
fprintf(fileID, '{');
% image
fprintf(fileID, '\\"image\\": {');
fprintf(fileID, '\\"name\\": \\"%s\\"', data.RELEASE.annolist(1,i).image.name);
fprintf(fileID, '},');
% annorect
fprintf(fileID, '\\"annorect\\": [');
for j=1:1:size(data.RELEASE.annolist(1,i).annorect,2)
fprintf(fileID, '{');
flag_comma = 0;
% x1
if isfield(data.RELEASE.annolist(1,i).annorect(j),'x1')
fprintf(fileID, '\\"x1\\": %d', data.RELEASE.annolist(1,i).annorect(j).x1);
flag_comma = 1;
end
% y1
if isfield(data.RELEASE.annolist(1,i).annorect(j),'y1')
if flag_comma, fprintf(fileID, ','); end
flag_comma = 1;
fprintf(fileID, '\\"y1\\": %d', data.RELEASE.annolist(1,i).annorect(j).y1);
end
% x2
if isfield(data.RELEASE.annolist(1,i).annorect(j),'x2')
if flag_comma, fprintf(fileID, ','); end
flag_comma = 1;
fprintf(fileID, '\\"x2\\": %d', data.RELEASE.annolist(1,i).annorect(j).x2);
end
% y2
if isfield(data.RELEASE.annolist(1,i).annorect(j),'y2')
if flag_comma, fprintf(fileID, ','); end
flag_comma = 1;
fprintf(fileID, '\\"y2\\": %d', data.RELEASE.annolist(1,i).annorect(j).y2);
end
% annopoints
if isfield(data.RELEASE.annolist(1,i).annorect(j),'annopoints')
if flag_comma, fprintf(fileID, ','); end
flag_comma = 1;
if isempty(data.RELEASE.annolist(1,i).annorect(j).annopoints)
fprintf(fileID, '\\"annopoints\\": []');
else
%disp(i)
fprintf(fileID, '\\"annopoints\\": {\\"point\\": [');
for d=1:1:size(data.RELEASE.annolist(1,i).annorect(j).annopoints.point,2)
fprintf(fileID, '{');
fprintf(fileID, '\\"x\\": %d, ', data.RELEASE.annolist(1,i).annorect(j).annopoints.point(1,d).x);
fprintf(fileID, '\\"y\\": %d, ', data.RELEASE.annolist(1,i).annorect(j).annopoints.point(1,d).y);
fprintf(fileID, '\\"id\\": %d ', data.RELEASE.annolist(1,i).annorect(j).annopoints.point(1,d).id);
if isfield(data.RELEASE.annolist(1,i).annorect(j).annopoints.point(1,d),'is_visible')
fprintf(fileID, ',');
if isempty(data.RELEASE.annolist(1,i).annorect(j).annopoints.point(1,d).is_visible)
fprintf(fileID, '\\"is_visible\\": []');
else
fprintf(fileID, '\\"is_visible\\": %d', data.RELEASE.annolist(1,i).annorect(j).annopoints.point(1,d).is_visible);
end
end
fprintf(fileID, '}');
if d < size(data.RELEASE.annolist(1,i).annorect(j).annopoints.point,2)
fprintf(fileID, ',');
end
end
fprintf(fileID, ']}');
end
end
% scale
if isfield(data.RELEASE.annolist(1,i).annorect(j),'scale')
if flag_comma, fprintf(fileID, ','); end
flag_comma = 1;
if isempty(data.RELEASE.annolist(1,i).annorect(j).scale)
fprintf(fileID, '\\"scale\\": []');
else
fprintf(fileID, '\\"scale\\": %d', data.RELEASE.annolist(1,i).annorect(j).scale);
end
end
% objpos
if isfield(data.RELEASE.annolist(1,i).annorect(j),'objpos')
if flag_comma, fprintf(fileID, ','); end
flag_comma = 1;
if isempty(data.RELEASE.annolist(1,i).annorect(j).objpos)
fprintf(fileID, '\\"objpos\\": []');
else
fprintf(fileID, '\\"objpos\\": {');
fprintf(fileID, '\\"x\\": %d, ', data.RELEASE.annolist(1,i).annorect(j).objpos.x);
fprintf(fileID, '"y\\": %d', data.RELEASE.annolist(1,i).annorect(j).objpos.y);
fprintf(fileID, '}');
end
end
fprintf(fileID, '}');
if j < size(data.RELEASE.annolist(1,i).annorect,2)
fprintf(fileID, ',');
end
end
fprintf(fileID, '],');
% frame_sec
if isempty(data.RELEASE.annolist(1,i).frame_sec)
fprintf(fileID, '\\"frame_sec\\": [], ');
else
fprintf(fileID, '\\"frame_sec\\": %d, ', data.RELEASE.annolist(1,i).frame_sec);
end
% vididx
if isempty(data.RELEASE.annolist(1,i).vididx)
fprintf(fileID, '\\"vididx\\": [] ');
else
fprintf(fileID, '\\"vididx\\": %d ', data.RELEASE.annolist(1,i).vididx);
end
fprintf(fileID, '}');
if i<size(data.RELEASE.annolist,2)
fprintf(fileID, ',');
end
end
fprintf(fileID, '],');
% img_train
fprintf(fileID, '\\"img_train\\":[');
for i=1:1:size(data.RELEASE.img_train,2)
fprintf(fileID, '%d', data.RELEASE.img_train(1,i));
if i<size(data.RELEASE.img_train,2)
fprintf(fileID, ',');
end
end
fprintf(fileID, '],');
% version
fprintf(fileID, '\\"version\\": %s,', data.RELEASE.version);
% single_person
fprintf(fileID, '\\"single_person\\":[');
for i=1:1:size(data.RELEASE.single_person,1)
if isempty( data.RELEASE.single_person{i,1})
fprintf(fileID, '[]');
elseif size( data.RELEASE.single_person{i,1},1) == 1
fprintf(fileID, '%d', data.RELEASE.single_person{i,1});
else
fprintf(fileID, '[');
for j=1:1:size( data.RELEASE.single_person{i,1},1)
fprintf(fileID, '%d', data.RELEASE.single_person{i,1}(j));
if j < size( data.RELEASE.single_person{i,1},1)
fprintf(fileID, ',');
end
end
fprintf(fileID, ']');
end
if i<size(data.RELEASE.single_person,1)
fprintf(fileID, ',');
end
end
fprintf(fileID, '],');
% act
fprintf(fileID, '\\"act\\":[');
for i=1:1:size(data.RELEASE.act,1)
fprintf(fileID, '{');
% cat_name
if isempty(data.RELEASE.act(i,1).cat_name)
fprintf(fileID, '\\"cat_name\\": [], ');
else
fprintf(fileID, '\\"cat_name\\": \\"%s\\", ', data.RELEASE.act(i,1).cat_name);
end
% act_name
if isempty(data.RELEASE.act(i,1).act_name)
fprintf(fileID, '\\"act_name\\": [], ');
else
fprintf(fileID, '\\"act_name\\": \\"%s\\", ', data.RELEASE.act(i,1).act_name);
end
% act_id
fprintf(fileID, '\\"act_id\\": %d', data.RELEASE.act(i,1).act_id);
fprintf(fileID, '}');
if i<size(data.RELEASE.act,1)
fprintf(fileID, ',');
end
end
fprintf(fileID, '],');
% video_list
fprintf(fileID, '\\"video_list\\":[');
for i=1:1:size(data.RELEASE.video_list,2)
fprintf(fileID, '\\"%s\\"', data.RELEASE.video_list{1,i});
if i<size(data.RELEASE.video_list,2)
fprintf(fileID, ',');
end
end
fprintf(fileID, ']');
%% close file
fprintf(fileID, '}');
fclose(fileID);
fprintf('Script complete.\n')
end
@Jackie-Chou
Copy link

Excellent code! I am doing the same thing as well. One thing quite bothers me is that there are some null data as "[]" in the "is_visible" filed, instead of 0 or 1 as expected. I wonder what to do with such data and what does "[]" stands for. Very grateful if you could help me.

@farrajota
Copy link
Author

The idea to use empty lists instead of 0 and 1s is that you can do extra analysis with that. In cases where you want only samples with full annotated data in order to train a model, or you want to collect data on only one body joint, you can skip everything that is an empty list. Back when I used this dataset, these were some of the use cases that required me to not fill missing values with numbers (thus avoiding introducing bias into the data).

In essence, the goal of this script is to make you explicitly target what you need from the dataset and make you deal with the corner cases.

@Jackie-Chou
Copy link

Jackie-Chou commented Jun 9, 2019 via email

@Nitaant
Copy link

Nitaant commented Apr 14, 2022

JSONDecodeError: Extra data: im getting this error when Im trying to read the file

@philgatt
Copy link

JSONDecodeError: Extra data: im getting this error when Im trying to read the file

It's been a while but do you remember finding any solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment