-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
237 lines (182 loc) · 5.18 KB
/
script.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var dps = [];
var xVal = 0;
var happy_count = 0;
var angry_count = 0;
var fear_count = 0;
var disgust_count = 0;
var sad_count = 0;
var surprised_count = 0;
var neutral_count = 0;
var emotion_labels = [
"angry",
"disgust",
"fear",
"happy",
"sad",
"surprise",
"neutral"
];
const video = document.getElementById('video')
Promise.all([
faceapi.nets.tinyFaceDetector.loadFromUri('/models'),
faceapi.nets.faceLandmark68Net.loadFromUri('/models'),
faceapi.nets.faceRecognitionNet.loadFromUri('/models'),
faceapi.nets.faceExpressionNet.loadFromUri('/models')
]).then(startVideo)
function startVideo() {
navigator.getUserMedia(
{ video: {} },
stream => video.srcObject = stream,
err => console.error(err)
)
}
video.addEventListener('play', () => {
const canvas = faceapi.createCanvasFromMedia(video)
document.body.append(canvas)
const displaySize = { width: video.width, height: video.height }
faceapi.matchDimensions(canvas, displaySize)
setInterval(async () => {
const detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceExpressions()
const resizedDetections = faceapi.resizeResults(detections, displaySize)
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
// faceapi.draw.drawDetections(canvas, resizedDetections)
// faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
// faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
detections.forEach((detection) => {
function pushing(){
if (detection.expressions.happy >= 0.51) {
console.log("happy" + happy_count);
happy_count++;
dps.push({
x: xVal,
y: 100,
});
}
else if (detection.expressions.angry >= 0.51) {
console.log("angry"+angry_count);
angry_count++;
dps.push({
x: xVal,
y: -80,
});
}
else if (detection.expressions.disgust >= 0.51) {
console.log("disgust"+disgust_count);
disgust_count++;
dps.push({
x: xVal,
y: -60,
});
}
else if (detection.expressions.fearful >= 0.51) {
console.log("fear"+fear_count);
fear_count++;
dps.push({
x: xVal,
y: -90,
});
}
else if (detection.expressions.sad >= 0.51) {
console.log("sad"+sad_count);
sad_count++;
dps.push({
x: xVal,
y: -100,
});
}
else if (detection.expressions.surprised >= 0.51) {
console.log("surprised" + surprised_count);
surprised_count++;
dps.push({
x: xVal,
y: 50,
});
}
else if (detection.expressions.neutral >= 0.51) {
console.log("neutral" +neutral_count);
neutral_count++;
dps.push({
x: xVal,
y: 0,
});
}
else {
console.log("patani");
}
}
pushing();
});
// console.log(detections);
// faceapi.return(withFaceExpressions);
// var b = faceapi.isWithFaceExpressions('happy');
// console.log(b);
}, 100)
})
// window.onload = function () {
function chartstart () {
happy_count = 0;
angry_count = 0;
fear_count = 0;
disgust_count = 0;
sad_count = 0;
surprised_count = 0;
neutral_count = 0;
// var dps = []; // dataPoints
var chart = new CanvasJS.Chart("chartContainer", {
theme: "dark1",
data: [{
type: "line",
dataPoints: dps
}]
});
// var xVal = 0;
var yVal = 100;
var updateInterval = 500;
var dataLength = 50; // number of dataPoints visible at any point
var updateChart = function (count) {
count = count || 1;
for (var j = 0; j < count; j++) {
yVal = yVal + Math.round(5 + Math.random() *(-5-5));
// dps.push({
// x: xVal,
// y: yVal
// });
xVal++;
}
if (dps.length > dataLength) {
dps.shift();
}
chart.render();
};
updateChart(dataLength);
setInterval(function(){updateChart()}, updateInterval);
}
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'damn'],
['sad', sad_count],
['happy', happy_count],
['neutral', neutral_count],
['angry', angry_count],
['fear', fear_count],
['surprised', surprised_count],
['disgust', disgust_count]
]);
// Optional; add a title and set the width and height of the chart
var options = { 'width':550, 'height':400 , backgroundColor: 'transparent', chartArea: {width: 500, height: 500}
};
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
function mylinkfunction(e) {
window.location.href="#piechart";
/* need to stop the form sending of the form
UPDATE as comment: This may not be exactly correct syntax
for stopping a form , look up preventing form submission */
e.preventDefault();
e.stopPropagation();
}