/
test.html
112 lines (92 loc) · 3.39 KB
/
test.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
<!DOCTYPE html>
<html>
<!--<a href='' target='_blank'>Click</a>-->
<video id="player" controls autoplay></video>
<canvas id="canvas" width=320 height=240></canvas>
<script>
</script>
<head>
<title>JSSample</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<h2>Face Rectangle</h2>
<ul id="faceRectangle">
<!-- Will populate list with response content -->
</ul>
<h2>Emotions</h2>
<ul id="scores">
<!-- Will populate list with response content -->
</ul>
<body>
<script type="text/javascript">
const player = document.getElementById('player');
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
//var snap = document.createElement("img");
var picURL;
player.style.display='none';
//player.style.visibility='hidden';
const constraints = {
video: true,
};
window.setInterval(capture, 5000);
function capture() {
context.drawImage(player, 0, 0, canvas.width, canvas.height);
picURL = canvas.toDataURL("image/png");
console.log(picURL);
}
// Attach the video stream to the video element and autoplay.
navigator.mediaDevices.getUserMedia(constraints)
.then((stream) => {
player.srcObject = stream;
})
if (picURL!==undefined){
$(function() {
// No query string parameters for this API call.
var params = { };
// var $snap = picURL;
// var dat = {};
// dat[$snap]=1;
//console.log(picURL);
var ajaxData = {
url:picURL
}
var stringified = JSON.stringify(ajaxData)
$.ajax({
// NOTE: You must use the same location in your REST call as you used to obtain your subscription keys.
// For example, if you obtained your subscription keys from westcentralus, replace "westus" in the
// URL below with "westcentralus".
url: "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers, also supports "application/octet-stream"
xhrObj.setRequestHeader("Content-Type","application/json");
// NOTE: Replace the "Ocp-Apim-Subscription-Key" value with a valid subscription key.
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","be6b417581304bdaa97448676d4171d5");
},
type: "POST",
// Request body
// url: picURL,
data: {url:picURL},
}).done(function(data) {
// Get face rectangle dimensions
//console.log(data);
var faceRectangle = data[0].faceRectangle;
var faceRectangleList = $('#faceRectangle');
// Append to DOM
for (var prop in faceRectangle) {
faceRectangleList.append("<li> " + prop + ": " + faceRectangle[prop] + "</li>");
}
// Get emotion confidence scores
var scores = data[0].scores;
var scoresList = $('#scores');
// Append to DOM
for(var prop in scores) {
scoresList.append("<li> " + prop + ": " + scores[prop] + "</li>")
}
}).fail(function(err) {
alert("Error: " + JSON.stringify(err));
});
});}
</script>
</body>
</html>