-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (72 loc) · 2.59 KB
/
index.js
File metadata and controls
82 lines (72 loc) · 2.59 KB
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
const fs = require('fs');
const configs = JSON.parse(fs.readFileSync('config.json', 'utf8'));
const axios = require('axios');
const io = require('socket.io-client');
const chokidar = require('chokidar');
let yolo = require('@vapi/node-yolo');
let detector = new yolo("darknet-configs", "cfg/coco.data", "cfg/yolov3.cfg", "yolov3.weights");
startJob();
function startJob() {
getToken().then((token) => {
let watcher;
let socket = io(configs.server.url + ':' + configs.server.port, {
'reconnection': false
});
let processNewImages = (path) => {
watcher = chokidar.watch(path, {
ignoreInitial: true,
awaitWriteFinish: true
});
watcher.on('add', (path) => {
console.log('File added');
detector.detect(path).then(data => {
let detection = {
objects: data,
imgUrl: path,
};
if (socket.connected) {
console.log(data);
socket.emit('detection', detection);
} else {
console.error('Seems that the socket has been disconnected, this detection will be ignored');
}
});
});
};
let handleSocketConnection = () => {
console.log('Connected to socket server');
socket.emit('authenticate', token);
socket.on('set-folder', processNewImages);
};
socket.on('connect', handleSocketConnection);
socket.on('disconnect', () => {
console.log('Disconnected from socket server');
if (watcher) {
watcher.close();
}
socket.removeAllListeners('set-folder');
socket.removeAllListeners("connect");
socket.removeAllListeners("disconnect");
startJob();
});
});
}
function getToken() {
return new Promise(((resolve) => {
axios.post(configs.server.url + ':' + configs.server.port + '/api/login',
{
username: configs.username,
password: configs.password
})
.then(function (response) {
resolve(response.data.token);
})
.catch(function (error) {
//console.error(error);
setTimeout(() => {
startJob();
console.error('[getToken] Request fail, retrying...');
}, 2000);
});
}));
}