-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.js
131 lines (119 loc) · 3.6 KB
/
app.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
// declare global variable
let video = null; // video element
let detector = null; // detector object
let detections = []; // store detection result
let videoVisibility = true;
let detecting = false;
// global HTML element
const toggleVideoEl = document.getElementById('toggleVideoEl');
const toggleDetectingEl = document.getElementById('toggleDetectingEl');
// set cursor to wait until video elment is loaded
document.body.style.cursor = 'wait';
// The preload() function if existed is called before the setup() function
function preload() {
// create detector object from "cocossd" model
detector = ml5.objectDetector('cocossd');
console.log('detector object is loaded');
}
// The setup() function is called once when the program starts.
function setup() {
// create canvas element with 640 width and 480 height in pixel
createCanvas(640, 480);
// Creates a new HTML5 <video> element that contains the audio/video feed from a webcam.
// The element is separate from the canvas and is displayed by default.
video = createCapture(VIDEO);
video.size(640, 480);
console.log('video element is created');
video.elt.addEventListener('loadeddata', function() {
// set cursor back to default
if (video.elt.readyState >= 2) {
document.body.style.cursor = 'default';
console.log('video element is ready! Click "Start Detecting" to see the magic!');
}
});
}
// the draw() function continuously executes until the noLoop() function is called
function draw() {
if (!video || !detecting) return;
// draw video frame to canvas and place it at the top-left corner
image(video, 0, 0);
// draw all detected objects to the canvas
for (let i = 0; i < detections.length; i++) {
drawResult(detections[i]);
}
}
/*
Exaple of an detect object
{
"label": "person",
"confidence": 0.8013999462127686,
"x": 7.126655578613281,
"y": 148.3782720565796,
"width": 617.7880859375,
"height": 331.60210132598877,
}
*/
function drawResult(object) {
drawBoundingBox(object);
drawLabel(object);
}
// draw bounding box around the detected object
function drawBoundingBox(object) {
// Sets the color used to draw lines.
stroke('green');
// width of the stroke
strokeWeight(4);
// Disables filling geometry
noFill();
// draw an rectangle
// x and y are the coordinates of upper-left corner, followed by width and height
rect(object.x, object.y, object.width, object.height);
}
// draw label of the detected object (inside the box)
function drawLabel(object) {
// Disables drawing the stroke
noStroke();
// sets the color used to fill shapes
fill('white');
// set font size
textSize(24);
// draw string to canvas
text(object.label, object.x + 10, object.y + 24);
}
// callback function. it is called when object is detected
function onDetected(error, results) {
if (error) {
console.error(error);
}
detections = results;
// keep detecting object
if (detecting) {
detect();
}
}
function detect() {
// instruct "detector" object to start detect object from video element
// and "onDetected" function is called when object is detected
detector.detect(video, onDetected);
}
function toggleVideo() {
if (!video) return;
if (videoVisibility) {
video.hide();
toggleVideoEl.innerText = 'Show Video';
} else {
video.show();
toggleVideoEl.innerText = 'Hide Video';
}
videoVisibility = !videoVisibility;
}
function toggleDetecting() {
if (!video || !detector) return;
if (!detecting) {
detect();
toggleDetectingEl.innerText = 'Stop Detecting';
} else {
toggleDetectingEl.innerText = 'Start Detecting';
}
detecting = !detecting;
}