-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
303 lines (240 loc) · 7.83 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const CAM_WIDTH = 640;
const CAM_HEIGHT = 480;
const MIN_DETECTION_CONFIDENCE = 0.5;
const ANIMATION_TIME = 500;
const MIN_ALERT_COOLDOWN_TIME = 60;
const STEP_1 = document.getElementById("step1");
const STEP_2 = document.getElementById("step2");
const STEP_3 = document.getElementById("step3");
const ENABLE_WEBCAM_BTN = document.getElementById("webcamButton");
const ENABLE_DETECTION_BTN = document.getElementById("enableDetection");
const CHOSEN_ITEM = document.getElementById("item");
const CHOSEN_ITEM_GUI = document.getElementById("chosenItem");
const CHOSEN_PET = document.getElementById("pet");
const MONITORING_TEXT = document.getElementById("monitoring");
const VIDEO = document.getElementById("webcam");
const LIVE_VIEW = document.getElementById("liveView");
const CANVAS = document.createElement("canvas");
const CTX = CANVAS.getContext("2d");
var children = [];
var model = undefined;
var ratioX = 1;
var ratioY = 1;
var state = "setup";
var lastNaughtyAnimalCount = 0;
var sendAlerts = true;
var foundMonitoredObjects = [];
cocoSsd.load().then(function (loadedModel) {
model = loadedModel;
ENABLE_WEBCAM_BTN.classList.remove("disabled");
});
function hasGetUserMedia() {
return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
}
if (hasGetUserMedia()) {
ENABLE_WEBCAM_BTN.addEventListener("click", enableCam);
} else {
console.warn("getUserMedia() is not supported by your browser");
}
function enableCam(event) {
if (!model) {
console.log("Wait! Model not loaded yet.");
return;
}
document.documentElement.requestFullscreen({
navigationUI: "hide",
});
event.target.classList.add("removed");
STEP_1.classList.add("disabled");
STEP_2.setAttribute("class", "invisible");
const constraints = {
video: {
facingMode: "environment",
width: CAM_WIDTH,
height: CAM_HEIGHT,
},
};
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
VIDEO.srcObject = stream;
VIDEO.addEventListener("loadeddata", function () {
recalculateVideoScale();
setTimeout(function () {
STEP_2.setAttribute("class", "");
}, ANIMATION_TIME);
predictWebcam();
});
});
}
function renderFoundObject(prediction) {
const p = document.createElement("p");
p.innerText =
prediction.class +
" - with " +
Math.round(parseFloat(prediction.score) * 100) +
"% confidence.";
p.style =
"left: " +
prediction.bbox[0] * ratioX +
"px;" +
"top: " +
prediction.bbox[1] * ratioY +
"px;" +
"width: " +
(prediction.bbox[2] * ratioX - 10) +
"px;";
const highlighter = document.createElement("div");
highlighter.setAttribute("class", "highlighter");
highlighter.style =
"left: " +
prediction.bbox[0] * ratioX +
"px; top: " +
prediction.bbox[1] * ratioY +
"px; width: " +
prediction.bbox[2] * ratioX +
"px; height: " +
prediction.bbox[3] * ratioY +
"px;";
LIVE_VIEW.appendChild(highlighter);
LIVE_VIEW.appendChild(p);
children.push(highlighter);
children.push(p);
}
function predictWebcam() {
model.detect(VIDEO).then(function (predictions) {
for (let i = 0; i < children.length; i++) {
LIVE_VIEW.removeChild(children[i]);
}
children.splice(0);
foundMonitoredObjects.splice(0);
for (let n = 0; n < predictions.length; n++) {
if (predictions[n].score > MIN_DETECTION_CONFIDENCE) {
if (state === "searching") {
renderFoundObject(predictions[n]);
if (predictions[n].class === CHOSEN_ITEM.value) {
state = "monitoring";
STEP_1.classList.remove("grayscale");
STEP_1.classList.remove("disabled");
STEP_3.classList.add("invisible");
MONITORING_TEXT.setAttribute("class", "");
setTimeout(function () {
STEP_3.setAttribute("class", "removed");
STEP_2.setAttribute("class", "removed");
}, ANIMATION_TIME);
}
} else if (state === "monitoring") {
if (predictions[n].class === CHOSEN_ITEM.value) {
renderFoundObject(predictions[n]);
foundMonitoredObjects.push(predictions[n]);
huntForPets(predictions[n], predictions, CHOSEN_PET.value);
break;
}
}
}
}
window.requestAnimationFrame(predictWebcam);
});
}
class BBox {
constructor(bbox) {
let x = bbox[0];
let y = bbox[1];
this.width = bbox[2];
this.height = bbox[3];
this.midX = x + this.width / 2;
this.midY = y + this.height / 2;
}
distance(bbox) {
let xDiff =
Math.abs(this.midX - bbox.midX) - this.width / 2 - bbox.width / 2;
let yDiff =
Math.abs(this.midY - bbox.midY) - this.height / 2 - bbox.height / 2;
if (xDiff < 0) {
return Math.max(yDiff, 0);
}
if (yDiff < 0) {
return xDiff;
}
return Math.sqrt(xDiff ** 2 + yDiff ** 2);
}
}
function checkIfNear(item1, item2, distance = 0) {
const BOUNDING_BOX_1 = new BBox(item1.bbox);
const BOUNDING_BOX_2 = new BBox(item2.bbox);
return BOUNDING_BOX_1.distance(BOUNDING_BOX_2) <= distance;
}
function cooldown() {
sendAlerts = true;
}
function sendAlert(naughtyAnimals) {
var detectionEvent = {};
detectionEvent.dateTime = Date.now();
detectionEvent.eventData = [];
for (let i = 0; i < foundMonitoredObjects.length; i++) {
var event = {};
event.eventType = foundMonitoredObjects[i].class + "_" + CHOSEN_ITEM.value;
event.score = foundMonitoredObjects[i].score;
event.x1 = foundMonitoredObjects[i].bbox[0] / VIDEO.videoWidth;
event.y1 = foundMonitoredObjects[i].bbox[1] / VIDEO.videoHeight;
event.width = foundMonitoredObjects[i].bbox[2] / VIDEO.videoWidth;
event.height = foundMonitoredObjects[i].bbox[3] / VIDEO.videoHeight;
event.detections = [];
for (let n = 0; n < naughtyAnimals.length; n++) {
let animal = {};
animal.objectType = naughtyAnimals[n].class;
animal.score = naughtyAnimals[n].score;
animal.x1 = naughtyAnimals[n].bbox[0] / VIDEO.videoWidth;
animal.y1 = naughtyAnimals[n].bbox[1] / VIDEO.videoHeight;
animal.width = naughtyAnimals[n].bbox[2] / VIDEO.videoWidth;
animal.height = naughtyAnimals[n].bbox[3] / VIDEO.videoHeight;
event.detections.push(animal);
}
detectionEvent.eventData.push(event);
}
CTX.drawImage(VIDEO, 0, 0);
CANVAS.toBlob(function (blob) {
detectionEvent.img = blob;
console.log(detectionEvent);
}, "image/png");
}
function huntForPets(monitoredItem, detectionArray, target) {
var naughtyAnimals = [];
for (let i = 0; i < detectionArray.length; i++) {
if (
detectionArray[i].class === target &&
detectionArray[i].score > MIN_DETECTION_CONFIDENCE
) {
renderFoundObject(detectionArray[i]);
if (checkIfNear(monitoredItem, detectionArray[i])) {
naughtyAnimals.push(detectionArray[i]);
}
}
}
if (naughtyAnimals.length > lastNaughtyAnimalCount) {
lastNaughtyAnimalCount = naughtyAnimals.length;
if (sendAlerts) {
sendAlerts = false;
sendAlert(naughtyAnimals);
setTimeout(cooldown, MIN_ALERT_COOLDOWN_TIME * 1000);
}
} else if (naughtyAnimals.length === 0) {
lastNaughtyAnimalCount = 0;
}
}
function recalculateVideoScale() {
ratioY = VIDEO.clientHeight / VIDEO.videoHeight;
ratioX = VIDEO.clientWidth / VIDEO.videoWidth;
CANVAS.width = VIDEO.videoWidth;
CANVAS.height = VIDEO.videoHeight;
}
function enableDetection() {
CHOSEN_ITEM_GUI.innerText = CHOSEN_ITEM.value;
STEP_1.classList.add("grayscale");
STEP_2.setAttribute("class", "invisible");
STEP_3.setAttribute("class", "invisible");
setTimeout(function () {
STEP_3.setAttribute("class", "");
state = "searching";
}, ANIMATION_TIME);
}
window.addEventListener("resize", recalculateVideoScale);
ENABLE_DETECTION_BTN.addEventListener("click", enableDetection);