-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizeImages.js
118 lines (109 loc) · 3.39 KB
/
organizeImages.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Requeriments:
- Node (https://nodejs.org).
- Packages: fs, fs.extra, path.
Instructions:
Put this in a directory with 'PH2 Dataset images' and execute it.
e.g.:
.
|____index.js
|____PH2 Dataset images
| |____IMD002
| | |____IMD002_Dermoscopic_Image
| | | |____IMD002.bmp <------ We need this
| | |____IMD002_lesion
| | | |____IMD002_lesion.bmp
| | |____IMD002_roi
| | | |____IMD002_R1_Label4.bmp
| | | |____IMD002_R2_Label3.bmp
| |____IMD003
| | |____IMD003_Dermoscopic_Image
| | | |____IMD003.bmp <------ We need this
| | |____IMD003_lesion
| | | |____IMD003_lesion.bmp
| | |____IMD003_roi
| | | |____IMD003_R1_Label4.bmp
| |____IMD004
| | |____IMD004_Dermoscopic_Image
| | | |____IMD004.bmp <------ We need this
| | |____IMD004_lesion
| | | |____IMD004_lesion.bmp
| | |____IMD004_roi
| | | |____IMD004_R1_Label4.bmp
| | | |____IMD004_R2_Label3.bmp
| |____IMD006
| | |____...
| |____IMD008
| | |____...
| |____IMD009
| | |____...
| |____IMD010
...
This algorithm arranges the files, sorting out the ones we need and putting them together in a directory.
e.g.:
.
|____IMD002.bmp
|____IMD003.bmp
|____IMD004.bmp
|____IMD006.bmp
|____IMD008.bmp
|____IMD009.bmp
|____IMD010.bmp
...
*/
'use strict';
const fs = require('fs')
, fsExtra = require('fs.extra')
, path = require('path')
, dest = 'imgs'
, destGT = 'imgsGT'
, src = 'PH2 Dataset images';
// Creates the directory if it doesn't exist
if (!fs.existsSync(dest)){
fs.mkdirSync(dest);
};
if (!fs.existsSync(destGT)){
fs.mkdirSync(destGT);
};
fs.readdir(path.normalize(src), (err, files) => {
// Iterate through files
files.forEach(file => {
fs.stat(path.normalize(src + '/' + file), (err, stats) => {
if (err) {
console.error(err);
return;
};
if (stats.isDirectory()) {
// Copy the photos to destination folder
// From 'PH2 Dataset images/IMD###/IMD###_Dermoscopic_Image/IMD###.bmp' to 'imgs/IMD###.bmp'
fsExtra.copy(path.normalize(src + '/' + file + '/' + file + '_Dermoscopic_Image' + '/' + file + '.bmp'), path.normalize(dest + '/' + file + '.bmp'), { replace: false }, function (err) {
if (err) {
console.error(err);
return;
};
});
};
});
});
});
fs.readdir(path.normalize(src), (err, files) => {
// Iterate through files
files.forEach(file => {
fs.stat(path.normalize(src + '/' + file), (err, stats) => {
if (err) {
console.error(err);
return;
};
if (stats.isDirectory()) {
// Copy the manual segmented images to destination folder
// From 'PH2 Dataset images/IMD###/IMD###_lesion/IMD###.bmp' to 'imgs/IMD###.bmp'
fsExtra.copy(path.normalize(src + '/' + file + '/' + file + '_lesion' + '/' + file + '_lesion.bmp'), path.normalize(destGT + '/' + file + '_lesion.bmp'), { replace: false }, function (err) {
if (err) {
console.error(err);
return;
};
});
};
});
});
});