-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
157 lines (135 loc) · 5.22 KB
/
index.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<title>Detect Faces Sample</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
function drawImage(canvasId, imageUrl) {
var c = document.getElementById(canvasId);
var ctx = c.getContext("2d");
var imageObj = new Image();
imageObj.onload = function() {
c.width = imageObj.width;
c.height = imageObj.height;
ctx.drawImage(imageObj, 0, 0);
}
imageObj.src = imageUrl;
}
function drawFaceRects(canvasId, faceApiResponse) {
var c = document.getElementById(canvasId);
var ctx = c.getContext("2d");
var ctx_lineWidth = ctx.lineWidth;
var ctx_strokeStyle = ctx.strokeStyle;
var ctx_fillStyle = ctx.fillStyle;
var ctx_font = ctx.font;
ctx.font = "15px Arial";
ctx.lineWidth = 3;
for (let face of faceApiResponse) {
if (face.faceAttributes.gender == "male") {
ctx.strokeStyle = "#00BFFF";
ctx.fillStyle = "#00BFFF";
} else if (face.faceAttributes.gender == "female") {
ctx.strokeStyle = "#FF69B4";
ctx.fillStyle = "#FF69B4";
} else {
ctx.strokeStyle = "#000000";
ctx.fillStyle = "#000000";
}
ctx.strokeRect(face.faceRectangle.left, face.faceRectangle.top, face.faceRectangle.width, face.faceRectangle.height);
var text = "age: " + face.faceAttributes.age;
var width = ctx.measureText(text).width;
var height = 15;
var x = face.faceRectangle.left;
var y = face.faceRectangle.top + face.faceRectangle.height;
ctx.fillRect(x, y, width, height);
ctx.textBaseline = "top";
ctx.fillStyle = "#000000";
ctx.fillText(text, x, y);
}
ctx.lineWidth = ctx_lineWidth;
ctx.strokeStyle = ctx_strokeStyle;
ctx.font = ctx_font;
ctx.fillStyle = ctx_fillStyle;
}
function processImage() {
var sourceImageUrl = document.getElementById("inputImage").value;
drawImage("myCanvas", sourceImageUrl);
var subscriptionKey = "c5bb4f0880364322991e7c985ac37023";
var uriBase = "https://westeurope.api.cognitive.microsoft.com/face/v1.0/detect";
var params = {
"returnFaceId": "true",
"returnFaceLandmarks": "false",
"returnFaceAttributes": "age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise",
};
$.ajax({
beforeSend: function(xhrObj){
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
},
url: uriBase + "?" + $.param(params),
type: "POST",
data: '{"url": ' + '"' + sourceImageUrl + '"}',
})
.done(function(data) {
document.getElementById('responseTextArea').style.display = "block";
$("#responseTextArea").val(JSON.stringify(data, null, 2));
drawFaceRects("myCanvas", data);
sendTelemetry(prepareTelemetry(data, sourceImageUrl));
})
.fail(function(jqXHR, textStatus, errorThrown) {
var errorString = (errorThrown === "") ? "Error. " : errorThrown + " (" + jqXHR.status + "): ";
errorString += (jqXHR.responseText === "") ? "" : (jQuery.parseJSON(jqXHR.responseText).message) ?
jQuery.parseJSON(jqXHR.responseText).message : jQuery.parseJSON(jqXHR.responseText).error.message;
alert(errorString);
});
}
function sendTelemetry(toSend) {
for (let data of toSend) {
$.ajax({
url: "http://test-fa1.azurewebsites.net/tables/lab1?zumo-api-version=2.0.0",
type: "POST",
contentType: 'application/json',
data: JSON.stringify(data),
});
}
}
function prepareTelemetry(faceApiResponse, imageUrl) {
var preparedTelemetry = [];
for (let face of faceApiResponse) {
preparedTelemetry.push({
"url" : imageUrl,
"faceId" : face.faceId,
"gender" : face.faceAttributes.gender,
"age" : face.faceAttributes.age,
"top": face.faceRectangle.top,
"left": face.faceRectangle.left,
"width": face.faceRectangle.width,
"height": face.faceRectangle.height,
});
}
return preparedTelemetry;
}
</script>
<h1>Detect Faces:</h1>
Enter the URL to an image that includes a face or faces, then click the <strong>Analyze face</strong> button.
<br><br>
Image to analyze: <input type="text" name="inputImage" id="inputImage" value="http://pxhst.co/avaxhome/30/52/002d5230_medium.jpeg" />
<button onclick="processImage()">Analyze face</button>
<br><br>
<div id="wrapper" style="width:1020px; display:table;">
<!-- <div id="jsonOutput" style="width:600px; display:table-cell;">
</div> -->
<div id="imageDiv" style="width:420px; display:table-cell;">
<!-- Source image: -->
<!-- <br><br> -->
<canvas id="myCanvas" width="0" height="0" style="border:0px solid #000000;">
</canvas>
<!-- Response:
<br><br> -->
<textarea id="responseTextArea" class="UIInput" style="width:580px; height:400px; display:none;"></textarea>
</div>
</div>
</body>
</html>