-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (65 loc) · 2.23 KB
/
index.js
File metadata and controls
76 lines (65 loc) · 2.23 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
// Sử dụng thư viện
var request = require('sync-request');
// Lấy danh sách idol từ file filtered-idols.json
var idols = require('./filtered-idols.json');
let key = '91bc85*******'; // Thay thế bằng key của bạn
let groupId = 'vav-idols';
// NodeJS không có thread.sleep nên ra dùng tạm function này
function sleep(time) {
console.log('Begin Sleep');
var stop = new Date().getTime();
while(new Date().getTime() < stop + time) {
;
}
console.log('End Sleep');
}
// Tạo idol trên hệ thống
function submitIdol(idol) {
let url = `https://api.projectoxford.ai/face/v1.0/persongroups/${groupId}/persons`;
console.log(`Begin submit idol: ${idol.id} - ${idol.name}`);
var res = request('POST', url, {
headers: {
'Ocp-Apim-Subscription-Key': key
},
json: {
name: idol.name,
userData: idol.id
}
});
if (res.statusCode == 200) {
var person = JSON.parse(res.getBody('utf8'));
console.log(`SUCCESS - Submit idol ${idol.id} - ${idol.name}. Person ID: ${person.personId}`);
// Bỏ 4 ảnh đầu
for (let i = 4; i < idol.images.length; i++) {
// Submit ảnh của idol lên hệ thống
try {
submitIdolFace(person.personId, idol.images[i].image);
sleep(4*1000); // Sleep 4 giây vì limit 20 call/phút
} catch (err) {
console.log('ERROR');
console.log(err);
}
}
} else {
console.log(res.getBody('utf8'));
}
}
// Submit ảnh của idol lên hệ thống
function submitIdolFace(personId, faceUrl) {
console.log(`Begin submit image ${faceUrl.substring(20,60)} for person id ${personId}`);
let url = `https://api.projectoxford.ai/face/v1.0/persongroups/${groupId}/persons/${personId}/persistedFaces`;
var res = request('POST', url, {
headers: {
'Ocp-Apim-Subscription-Key': key
},
json: {
url: faceUrl
}
});
if (res.statusCode == 200) {
console.log(`SUCCESS - Submit image ${faceUrl.substring(20,60)} for person id ${personId}.`);
}
}
for (let idol of idols) {
submitIdol(idol);
}