-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-models-cdn.cjs
More file actions
140 lines (122 loc) · 4.31 KB
/
Copy pathfix-models-cdn.cjs
File metadata and controls
140 lines (122 loc) · 4.31 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const https = require('https');
const fs = require('fs');
const path = require('path');
// Define where models will be stored
const modelsDir = path.join(__dirname, 'public', 'models');
if (!fs.existsSync(modelsDir)) {
fs.mkdirSync(modelsDir, { recursive: true });
}
// Define the models to download from CDN
const modelFiles = [
// TinyFaceDetector model
{
url: 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api/model/tiny_face_detector_model-weights_manifest.json',
path: path.join(modelsDir, 'tiny_face_detector_model-weights_manifest.json'),
isManifest: true
},
{
url: 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api/model/tiny_face_detector_model-shard1',
path: path.join(modelsDir, 'tiny_face_detector_model-shard1.bin'),
isManifest: false
},
// FaceLandmark68 model
{
url: 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api/model/face_landmark_68_model-weights_manifest.json',
path: path.join(modelsDir, 'face_landmark_68_model-weights_manifest.json'),
isManifest: true
},
{
url: 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api/model/face_landmark_68_model-shard1',
path: path.join(modelsDir, 'face_landmark_68_model-shard1.bin'),
isManifest: false
},
// FaceRecognition model
{
url: 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api/model/face_recognition_model-weights_manifest.json',
path: path.join(modelsDir, 'face_recognition_model-weights_manifest.json'),
isManifest: true
},
{
url: 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api/model/face_recognition_model-shard1',
path: path.join(modelsDir, 'face_recognition_model-shard1.bin'),
isManifest: false
},
{
url: 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api/model/face_recognition_model-shard2',
path: path.join(modelsDir, 'face_recognition_model-shard2.bin'),
isManifest: false
}
];
// Download a file and save it to the specified path
function downloadFile(url, filePath) {
return new Promise((resolve, reject) => {
console.log(`Downloading ${url} to ${filePath}`);
const file = fs.createWriteStream(filePath);
https.get(url, (response) => {
// Check for redirects
if (response.statusCode === 301 || response.statusCode === 302) {
console.log(`Following redirect: ${response.headers.location}`);
return downloadFile(response.headers.location, filePath)
.then(resolve)
.catch(reject);
}
if (response.statusCode !== 200) {
reject(new Error(`Failed to download: ${response.statusCode}`));
return;
}
response.pipe(file);
file.on('finish', () => {
file.close();
console.log(`Downloaded ${filePath}`);
resolve();
});
file.on('error', (err) => {
fs.unlink(filePath, () => {});
reject(err);
});
}).on('error', (err) => {
fs.unlink(filePath, () => {});
reject(err);
});
});
}
// Update the manifest file to fix model references
function updateManifest(manifestPath) {
try {
console.log(`Updating manifest: ${manifestPath}`);
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
// Add .bin extension to shard files and update paths
if (manifest && manifest.length > 0 && manifest[0].paths) {
manifest[0].paths = manifest[0].paths.map(p => {
if (p.includes('shard') && !p.endsWith('.bin')) {
return `${p}.bin`;
}
return p;
});
// Write the updated manifest
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
console.log(`Updated manifest: ${manifestPath}`);
}
} catch (error) {
console.error(`Error updating manifest ${manifestPath}:`, error);
}
}
// Main function to download all models
async function downloadModels() {
console.log('Starting to download face-api.js models...');
for (const model of modelFiles) {
try {
// Download the file
await downloadFile(model.url, model.path);
// Update manifest files
if (model.isManifest) {
updateManifest(model.path);
}
} catch (error) {
console.error(`Error downloading ${model.url}:`, error);
}
}
console.log('✅ All models downloaded and processed successfully!');
}
// Run the download
downloadModels().catch(console.error);