Upload 3 files
Browse files- extract.m +49 -0
- test.json +0 -0
- train.json +0 -0
extract.m
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
% Define the directory where your images are stored
|
| 2 |
+
for dirName = {'train', 'test'}
|
| 3 |
+
imageDir = dirName{1};
|
| 4 |
+
|
| 5 |
+
% Initialize a dictionary (containers.Map) to store file comments
|
| 6 |
+
fileComments = containers.Map();
|
| 7 |
+
|
| 8 |
+
% List all image files in the directory
|
| 9 |
+
imageFiles = dir(fullfile(imageDir, '*.jpg')); % Update the file extension as needed
|
| 10 |
+
|
| 11 |
+
disp(['Total number of image files: ', num2str(length(imageFiles))]);
|
| 12 |
+
|
| 13 |
+
% Iterate over each image file
|
| 14 |
+
for i = 1:length(imageFiles)
|
| 15 |
+
% Get the full file path
|
| 16 |
+
imagePath = fullfile(imageDir, imageFiles(i).name);
|
| 17 |
+
|
| 18 |
+
% Use imfinfo to extract metadata
|
| 19 |
+
try
|
| 20 |
+
info = imfinfo(imagePath);
|
| 21 |
+
catch
|
| 22 |
+
disp(['Image not recognized or corrupted: ', imagePath]);
|
| 23 |
+
continue;
|
| 24 |
+
end
|
| 25 |
+
info = imfinfo(imagePath);
|
| 26 |
+
commentParts = info.Comment;
|
| 27 |
+
% Store the comment in the dictionary with the file name as the key
|
| 28 |
+
commentDict = containers.Map();
|
| 29 |
+
for j = 1:length(commentParts)
|
| 30 |
+
keyValue = strsplit(strtrim(commentParts{j}), '=');
|
| 31 |
+
if length(keyValue) == 2
|
| 32 |
+
commentDict(keyValue{1}) = keyValue{2};
|
| 33 |
+
end
|
| 34 |
+
end
|
| 35 |
+
fileComments(imageFiles(i).name) = commentDict;
|
| 36 |
+
end
|
| 37 |
+
% Define the output JSON file path
|
| 38 |
+
outputJsonFile = strcat(imageDir, '.json');
|
| 39 |
+
|
| 40 |
+
% Write the JSON string to the output JSON file
|
| 41 |
+
fid = fopen(outputJsonFile, 'w');
|
| 42 |
+
if fid ~= -1
|
| 43 |
+
fwrite(fid, unicode2native(jsonencode(fileComments, 'PrettyPrint', true), 'UTF-8'));
|
| 44 |
+
fclose(fid);
|
| 45 |
+
else
|
| 46 |
+
disp('Error opening file.');
|
| 47 |
+
end
|
| 48 |
+
end
|
| 49 |
+
disp('Comments saved to JSON file.');
|
test.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
train.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|