-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
119 lines (98 loc) · 3.33 KB
/
main.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
'use strict';
$(document).ready(function() {
let key;
let faces = [];
let imageParams = {
// Request parameters
"analyzesFaceLandmarks": "false",
"analyzesAge": "true",
"analyzesGender": "true",
"analyzesHeadPose": "true"
};
$('#pic1, #pic2').click(detectFaces);
function detectFaces() {
key = $('#key').val();
let url;
console.log('key:', key);
console.log('faces:', faces, ', faces.length:', faces.length);
if ($(this).attr('id') === 'pic1') {
console.log('detecting on pic 1');
url = $('#urlone').val();
// $('#pic1').prop('disabled', 'true');
} else {
console.log('detecting on pic 2');
url = $('#urltwo').val();
// $('#pic2').prop('disabled', 'true');
}
// display the picture currently being processed in the DOM
$('#pictures').append( $('<img>').attr('src', url) );
console.log('url:', url)
// oh dear, this is callback hell...
$.ajax( paramForDetect(url) )
.done(function(data) {
console.log("we've got face data:", data);
let newFaces = [];
let newFaceIds = [];
data.forEach((person) => {
// if the face is not already in our faces array, add it
if (faces.length > 0) {
let haveAlreadyp = verifyFace(person.faceId, faces[0].faceId);
$.when(haveAlreadyp).then((data) => {
console.log('haveAlreadyp result:', data.isIdentical);
if (!data.isIdentical) {
// update our array of known faces
faces.push(person);
console.log('faces array:', faces);
// print the new face ID to the DOM
let $row = $('<tr>').append( $('<td>').text(person.faceId) );
$('#faceIds').append($row).show();
// TODO: DISPLAY BOX AROUND THIS PERSON'S FACE ON TOP OF PICTURE
}
});
} else {
// update our array of known faces
faces.push(person);
console.log('faces array:', faces);
// print the new face ID to the DOM
let $row = $('<tr>').append( $('<td>').text(person.faceId) );
$('#faceIds').append($row).show();
}
});
})
.fail(function(err) {
console.log(err);
console.log('Detect-Faces Failed');
});
}; // end detectFaces function
function paramForDetect(url) {
return {
url: 'https://api.projectoxford.ai/face/v0/detections?' + $.param(imageParams),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", key);
},
type: 'POST',
data: `{'url': '${url}'}`
};
}
function verifyFace(faceId1, faceId2) {
return $.ajax( paramForVerify({"faceId1":faceId1, "faceId2":faceId2}) )
.fail(function(err) {
console.log(err);
console.log('Verify-Face Failed for:', faceId1, 'vs', faceId2);
});
}
function paramForVerify(faceIds) {
return {
url: 'https://api.projectoxford.ai/face/v0/verifications',
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", key);
},
type: 'POST',
data: JSON.stringify(faceIds)
};
}
});