-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (54 loc) · 1.73 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const NodeWebcam = require("node-webcam");
const CAMERA_NAME = 'FaceTime HDカメラ(内蔵)';
const TEMP_PHOTO_FILE = 'temppicture';
const opts = {
width: 1280,
height: 720,
quality: 100,
frames: 60,
delay: 2,
saveShots: true,
output: "jpeg",
device: CAMERA_NAME,
callbackReturn: "location",
verbose: false
};
const camera = NodeWebcam.create(opts);
const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();
async function capturePhotoAndDetectLabels() {
return new Promise((resolve, reject) => {
camera.capture(TEMP_PHOTO_FILE, (err, data) => {
if (err)
return reject(err);
client.labelDetection(`./${data}`, (err, result) => {
if (err)
return reject(err);
resolve(result.labelAnnotations);
});
});
});
}
const schedule = require('node-schedule');
const async = require('async');
const request = require('request');
// todo: ルーティンを増やしたらconfig化する
schedule.scheduleJob('/20 * * * * *', () => {
async.retry({ times: 10, interval: 1000 }, cb => {
console.log('attempt');
capturePhotoAndDetectLabels().then((labels) => {
if (labels.some(label => label.description === 'Vacuum cleaner'))
return cb(null, true);
cb(new Error('retry'));
});
}, (err, results) => {
if (results)
return;
// アラート
request({ url: require('./config').ZAPIER_HOOK_URL, method: 'POST', json: { hoge: true } }, (err, res, body) => {
if (err)
console.error(err);
console.log(body);
});
});
});