% Define the directory where your images are stored for dirName = {'train', 'test'} imageDir = dirName{1}; % Initialize a dictionary (containers.Map) to store file comments fileComments = containers.Map(); % List all image files in the directory imageFiles = dir(fullfile(imageDir, '*.jpg')); % Update the file extension as needed disp(['Total number of image files: ', num2str(length(imageFiles))]); % Iterate over each image file for i = 1:length(imageFiles) % Get the full file path imagePath = fullfile(imageDir, imageFiles(i).name); % Use imfinfo to extract metadata try info = imfinfo(imagePath); catch disp(['Image not recognized or corrupted: ', imagePath]); continue; end info = imfinfo(imagePath); commentParts = info.Comment; % Store the comment in the dictionary with the file name as the key commentDict = containers.Map(); for j = 1:length(commentParts) keyValue = strsplit(strtrim(commentParts{j}), '='); if length(keyValue) == 2 commentDict(keyValue{1}) = keyValue{2}; end end fileComments(imageFiles(i).name) = commentDict; end % Define the output JSON file path outputJsonFile = strcat(imageDir, '.json'); % Write the JSON string to the output JSON file fid = fopen(outputJsonFile, 'w'); if fid ~= -1 fwrite(fid, unicode2native(jsonencode(fileComments, 'PrettyPrint', true), 'UTF-8')); fclose(fid); else disp('Error opening file.'); end end disp('Comments saved to JSON file.');