-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect-objects.js
More file actions
75 lines (62 loc) · 1.94 KB
/
detect-objects.js
File metadata and controls
75 lines (62 loc) · 1.94 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
module.exports = function (RED) {
const NODE_TYPE = "google-video-object-detection";
const video = require('@google-cloud/video-intelligence');
const fs = require('fs');
const util = require('util');
function DetectorNode(config) {
// Set up node
RED.nodes.createNode(this, config);
const node = this;
// Get Google API credentials
const keyFilename = config.keyFilename;
// Set up Google Video API client
const client = new video.VideoIntelligenceServiceClient({
"keyFilename": keyFilename
});
/**
* Input function
*
* We are expecting either:
* > msg.filename (image source) or...
* > msg.payload (image base64 data)
*/
async function Input(msg) {
let base64 = null;
// Check for message filename
if (msg.filename) {
// If we have a filename we need to grab the file
const readFile = util.promisify(fs.readFile);
const file = await readFile(filename);
base64 = file.toString('base64');
} else {
// Check if we've been given a buffer
if (msg.payload) {
base64 = msg.payload;
} else {
// No msg.filename and msg.payload is not a buffer.
node.error("Neither msg.filename nor msg.payload properly supplied.");
return;
}
}
// Create request object
const request = {
inputContent: base64,
features: ['OBJECT_TRACKING'],
locationId: 'us-east1'
};
// Make the request
try {
const [operation] = await client.annotateVideo(request);
const [operationResult] = await operation.promise();
const annotations = operationResult.annotationResults[0];
msg.payload = annotations;
node.send(msg);
} catch(err) {
node.error(err);
}
}
// Handler for input
node.on("input", Input);
}
RED.nodes.registerType(NODE_TYPE, DetectorNode);
};