-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.html
80 lines (69 loc) · 2.71 KB
/
detect.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
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emotion Intelligence using MS Cognitive Service</title>
<script src="https://code.jquery.com/jquery-3.2.0.js"></script>
<script language="JavaScript">
var stopIntervalIsTrue = true;
function take_snapshot() {
//document.getElementById("takeSnapshotButton").setAttribute("disabled","disabled");
Webcam.snap(function(data_uri) {
document.getElementById('busyimg').style.visibility = 'visible';
var file = data_uri.substring(23).replace(' ', '+');
var img = Base64Binary.decodeArrayBuffer(file);
var ajax = new XMLHttpRequest();
ajax.addEventListener("load", function(event) {
var data = JSON.parse(event.target.response);
var happiness = 0;
if(data.length > 0) {
// we got a face
happiness = data[0].scores.happiness;
}
var resEl = document.getElementById('results');
resEl.innerHTML = '<div><p>' + happiness + '</p><img src="'+ data_uri +'"/></div>' + resEl.innerHTML;
document.getElementById('busyimg').style.visibility = 'hidden';
document.getElementById('debugtext').innerHTML = '<div><p>' +intervalID + ' ' + stopIntervalIsTrue + '</p></div>'
if (stopIntervalIsTrue) {
clearInterval(intervalID);
}
// document.getElementById("takeSnapshotButton").removeAttribute("disabled");
}, false);
ajax.open("POST", "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize","image/jpg");
ajax.setRequestHeader("Content-Type","application/octet-stream");
ajax.setRequestHeader("Accept","application/json,text/html,application/xhtml+xml,application/xml");
ajax.setRequestHeader("Ocp-Apim-Subscription-Key","fa5211a24a3448eebf0fae55eb9d0b5f");
ajax.send(img);
});
}
function stop_take_snapshot() {
stopIntervalIsTrue = true;
}
$(function() {
Webcam.set({
width: 320,
height: 240,
image_format: 'jpeg',
jpeg_quality: 100
});
Webcam.attach('#my_camera');
var intervalID = setInterval(take_snapshot, 5000);
stopIntervalIsTrue = false;
});
</script>
</head>
<body>
<div><b>Webcam Preview...</b>
<div id="my_camera"></div>
<img id="busyimg" src="img\busy.gif" height="50px" width="50px"/>
<button onClick="stop_take_snapshot">Stop It</button>
</div>
<div>
<div><b>Results:</b><div id="results"></div></div>
</div>
<p id="debugtext"/>
<div class="container" id="results" />
</body>
<script type="text/javascript" src="webcam.js"></script>
<script type="text/javascript" src="base64-binary.js"></script>
</html>