-
Notifications
You must be signed in to change notification settings - Fork 12
/
sketch.js
109 lines (74 loc) · 2.21 KB
/
sketch.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
let yolo;
let status;
let width = 960;
let height = 540;
let current_image;
let objects = [];
let image_el;
// meraki camera config
let network_id = "L_668784544664521658";
let camera_serial = "Q2EV-4LMQ-R4LW";
async function setup() {
createCanvas(width, height);
status = select('#status');
image_el = select('#image');
yolo = await ml5.YOLO();
status.html("Model loaded - loading snapshot from Meraki MV camera");
await load_image()
}
async function load_image() {
current_image = await fetch_meraki_live_snapshort();
status.html("Image loaded");
objects = await yolo.detect(current_image);
console.table(objects);
status.html("Image detected");
// Continuously detecting
await load_image()
}
function draw() {
if (!current_image) {
return
}
try{
image(current_image, 0, 0, width, height);
objects.forEach((item) => {
noStroke();
fill(0, 255, 0);
textSize(14);
text(`${item.className}(${item.classProb.toFixed(2)})`, item.x * width, item.y * height - 5);
noFill();
strokeWeight(3);
stroke(0, 255, 0);
rect(item.x * width, item.y * height, item.w * width, item.h * height);
})
}catch (e) {
}
}
function fetch_meraki_live_snapshort() {
return new Promise((resolve, reject) => {
let url = `/networks/${network_id}/cameras/${camera_serial}/snapshot`;
fetch(url, {
mode: "cors"
}).then(res => res.json())
.then(response => {
image_el.elt.onload = function () {
resolve(select("#image"))
};
image_el.attribute("src", response.url);
})
})
}
function fetch_static_image(index) {
return new Promise(function (resolve, reject) {
let urls = [
"/static/images/difference-between-fruits-and-vegetables.jpg",
"/static/images/snapshot.jpeg",
"/static/images/snapshot2.jpg",
"/static/images/snapshot3.png"
];
image_el.elt.onload = function () {
resolve(select("#image"))
};
image_el.attribute("src", urls[index || 0]);
})
}