-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.js
108 lines (91 loc) · 2.4 KB
/
camera.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
const fs = require('fs');
const { spawn, fork } = require('child_process');
const { sendTweet } = require('./tweet');
let count = 1;
let hasMotion;
let imagesArray = [];
const CROW = 'crow';
function startPhoto() {
hasMotion = true;
console.log('hasMotion is ', hasMotion);
}
function stopPhoto() {
hasMotion = false;
console.log('hasMotion is ', hasMotion);
if (imagesArray.length > 0) batchPhotos();
}
// Take a photo with the camera module using Raspistill on the command line with spawn
function takePhoto() {
if (!hasMotion) return;
let filename = `${__dirname}/photos/image_${count}.jpg`;
let args = [
'-rot', // rotate 90 degrees
'90',
'-n', // no preview
'-t', // time to take photo
'1000', // 1 second
'-w', // width
'400',
'-h', // height
'400',
'-ev',
'auto',
'auto',
'-o',
filename,
];
const child = spawn('raspistill', args);
child.on('exit', code => {
console.log(filename + ' was taken');
count++;
if (hasMotion) takePhoto();
// Use child_process fork():
// Send image to trained model to detect if there's a crow
// let checkPhoto = fork(`${__dirname}/detect.js`);
// if (/jpg$/.test(filename)) {
// checkPhoto.send(filename);
// Get result from model
// checkPhoto.on('message', data => {
// console.log(data);
// if (data === CROW) {
// console.log('CROW IS HERE!');
// batch up to 4 photos to upload to twitter
// batchPhotos(filename);
// } else {
// Delete non-crow photos to clean up space for now
// deletePhoto(filename);
// }
// });
// }
});
}
// Batch burst images and send multiple images in 1 tweet
// https://github.com/desmondmorris/node-twitter/issues/54
function batchPhotos(filename = null) {
// Load image if there
if (filename) {
const image = fs.readFileSync(filename);
imagesArray.push(image);
}
console.log('Batching photos to tweet....');
console.log(imagesArray);
if (imagesArray.length === 4 || !hasMotion) {
// Upload images to twitter
sendTweet(imagesArray);
// reset images array for next time
imagesArray.length = 0;
}
}
function deletePhoto(imgPath) {
fs.unlink(imgPath, err => {
if (err) {
return console.error(err);
}
console.log(imgPath + ' is deleted.');
});
}
module.exports = {
startPhoto,
stopPhoto,
takePhoto,
};