-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_from_webcam.js
62 lines (56 loc) · 1.53 KB
/
detect_from_webcam.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
let video;
let detector;
let darr=[]; //detection array
img.crossOrigin='Anonymous';
async function preload(){
detector=await ml5.objectDetector('cocossd');
}
function modelready(){
console.log('Model is ready!');
begindet();
}
function begindet(){
detector.detect(video,gotdetections);
}
function gotdetections(error,results){
if(error){console.error(error);}
else{
console.log(results);
darr=results;
setTimeout(function(){
detector.detect(video,gotdetections);
},100);
//detector.detect(video,gotdetections);
}
}
function setup(){
const ht=480;
const wd=640;
createCanvas(wd,ht);
video=createCapture(VIDEO);
video.size(wd,ht);
video.hide();
detectobjects=detector.detect(video,modelready);
video.elt.addEventListener('loadeddata', begindet);
document.getElementById('content').appendChild(canvas);
document.getElementById('content').height=ht;
document.getElementById('content').width=wd;
}
function draw(){
image(video,0,0);
for(let i=0;i<darr.length;i++){
let ob=darr[i];
let xnorm=ob.normalized.x*width;
let ynorm=ob.normalized.y*height;
let wnorm=ob.normalized.width*width;
let hnorm=ob.normalized.height*height;
stroke(0,255,0);
strokeWeight(4);
noFill();
rect(xnorm,ynorm,wnorm,hnorm);
noStroke();
fill(255);
textSize(24);
text(ob.label+', with '+(ob.confidence*100).toFixed(2)+'% confidence',xnorm+10,ynorm+24);
}
}