Datasets:

Modalities:
Image
Libraries:
Datasets
License:
File size: 1,754 Bytes
a5cd02f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
% 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.');