This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashfoto.js
124 lines (113 loc) · 2.56 KB
/
flashfoto.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
119
120
121
122
123
124
var req = require('request'), fs = require('fs');
var base = 'http://flashfotoapi.com/api/';
var username = 'niusounds';
var apikey = 'ST8nRkjjPN8xESABCYOZlWkerCNhkwrI';
// http://flashfotoapi.com/api/get/2069373?partner_username=niusounds&partner_apikey=ST8nRkjjPN8xESABCYOZlWkerCNhkwrI&version=HardMasked
function generateFaceImage(filename, opts, cb) {
console.log('generateFaceImage', opts);
add(filename, opts, function(err, res, body) {
console.log(body);
if (err) {
cb(err);
return;
}
var id = body.Image.id;
console.log('image id is ' + id);
segment(id, function(err, res) {// '2069373' for sample
if (err) {
cb(err);
return;
}
segmentStatus(id, function loop(err, res, status) {
if (err) {
cb(err);
return;
}
console.log('segment status is ' + status.segmentation_status);
if (status.segmentation_status === 'finished') {
info(id, function(err, res, info) {
if (err) {
cb(err);
return;
}
info.ImageVersion.forEach(function(ver) {
if (ver.version === 'HardMasked') {
console.log('face croped image found');
get(ver, cb);
}
});
});
} else if (status.segmentation_status === 'failed') {
cb('segmentation failed');
} else {
setTimeout(segmentStatus, 3000, id, loop);
}
});
});
});
}
function add(filename, opts, callback) {
console.log('add', filename);
fs.createReadStream(filename).pipe(req.post({
url : base + 'add/',
qs : {
partner_username : username,
partner_apikey : apikey,
format : opts.format
},
json : true
}, callback));
}
function segment(id, callback) {
req.get({
url : base + 'segment/' + id,
json : true,
qs : {
partner_username : username,
partner_apikey : apikey
}
}, callback);
}
function info(id, callback) {
req.get({
url : base + 'info/' + id,
json : true,
qs : {
partner_username : username,
partner_apikey : apikey
}
}, callback);
}
function get(ver, callback) {
var id = ver.image_id || ver.id || ver;
var qs = {
partner_username : username,
partner_apikey : apikey
};
if (ver.version) {
qs.version = ver.version;
}
req.get({
url : base + 'get/' + id,
qs : qs,
encoding : null
}, callback);
}
function segmentStatus(id, callback) {
req.get({
url : base + 'segment_status/' + id,
json : true,
qs : {
partner_username : username,
partner_apikey : apikey
}
}, callback);
}
module.exports = {
add : add,
get : get,
info : info,
segment : segment,
segmentStatus : segmentStatus,
generateFaceImage : generateFaceImage
};