-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
40 lines (31 loc) · 1.11 KB
/
index.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
const cv = require('opencv')
const camera = new cv.VideoCapture(0)
const classifiers = {
face: './classifiers/face.xml',
// local training
trained: '../opencv-haar-classifier-training/classifier/cascade.xml',
// hot dog classifier, trained on a _very_ small image set
hotdogStage0: './classifiers/hotdog-stage0.xml'
}
const detect = function(err, im) {
if (err) throw err
im.detectObject(classifiers.face, {}, function(err, detectedObjects) {
if (err) throw err
// draw circles around any detected objects in the picture
detectedObjects.forEach(detectedObject => {
im.ellipse(
detectedObject.x + detectedObject.width / 2,
detectedObject.y + detectedObject.height / 2,
detectedObject.width / 2, detectedObject.height / 2
)
})
if (detectedObjects.length) {
console.log(`tracking ${detectedObjects.length} object(s)`)
im.save(`./screencaps/${Date.now()}.png`)
}
})
}
// read and detect input from camera every second
setInterval(() => camera.read(detect), 1000)
// example: read input from an image
// cv.readImage('./hotdog.jpg', detect)