-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitdata.js
More file actions
36 lines (28 loc) · 1.1 KB
/
splitdata.js
File metadata and controls
36 lines (28 loc) · 1.1 KB
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
const fs = require('fs');
const path = require('path');
const SOURCE_DIR = './video_frames';
const DEST_DIR = './dataset_new';
const SPLIT_RATIO = 0.8;
const CLASSES = ['real', 'fake'];
function shuffle(array) {
return array.sort(() => Math.random() - 0.5);
}
function copyFiles(files, fromDir, toDir) {
fs.mkdirSync(toDir, { recursive: true });
files.forEach(file => {
const src = path.join(fromDir, file);
const dest = path.join(toDir, file);
fs.copyFileSync(src, dest);
});
}
CLASSES.forEach(cls => {
const sourcePath = path.join(SOURCE_DIR, cls);
const files = fs.readdirSync(sourcePath).filter(f => f.toLowerCase().endsWith('.png') || f.toLowerCase().endsWith('.jpg'));
const shuffled = shuffle(files);
const splitIndex = Math.floor(SPLIT_RATIO * shuffled.length);
const trainFiles = shuffled.slice(0, splitIndex);
const valFiles = shuffled.slice(splitIndex);
copyFiles(trainFiles, sourcePath, path.join(DEST_DIR, 'train', cls));
copyFiles(valFiles, sourcePath, path.join(DEST_DIR, 'val', cls));
console.log(`✅ ${cls}: ${trainFiles.length} train, ${valFiles.length} val`);
});