Skip to content

Commit 2401644

Browse files
committed
Add new version of Visual Recognition
1 parent ded53bb commit 2401644

2 files changed

Lines changed: 346 additions & 1 deletion

File tree

services/visual_recognition/v1.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
Copyright 2015 IBM Corp.
2+
Copyright 2015 IBM Corp.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

services/visual_recognition/v1.js

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,350 @@
1515
**/
1616

1717
module.exports = function(RED) {
18+
<<<<<<< HEAD
19+
var request = require('request');
20+
var cfenv = require('cfenv');
21+
var fs = require('fs');
22+
var temp = require('temp');
23+
var fileType = require('file-type');
24+
var watson = require('watson-developer-cloud');
25+
26+
temp.track();
27+
28+
var username, password;
29+
30+
var service = cfenv.getAppEnv().getServiceCreds(/visual recognition/i)
31+
32+
if (service) {
33+
username = service.username;
34+
password = service.password;
35+
}
36+
37+
RED.httpAdmin.get('/watson-visual-recognition/vcap', function(req, res) {
38+
res.json(service ? {bound_service: true} : null);
39+
});
40+
41+
function Node(config) {
42+
RED.nodes.createNode(this,config);
43+
var node = this;
44+
45+
this.on('input', function(msg) {
46+
if (!msg.payload) {
47+
var message = 'Missing property: msg.payload';
48+
node.error(message, msg);
49+
return;
50+
}
51+
52+
if (!msg.payload instanceof Buffer || !typeof msg.payload === 'string') {
53+
var message = 'Invalid property: msg.payload, must be a URL or a Buffer.';
54+
node.error(message, msg);
55+
return;
56+
}
57+
58+
username = username || this.credentials.username;
59+
password = password || this.credentials.password;
60+
61+
if (!username || !password) {
62+
var message = 'Missing Visual Recognition service credentials';
63+
node.error(message, msg)
64+
return;
65+
}
66+
67+
var watson = require('watson-developer-cloud');
68+
69+
var visual_recognition = watson.visual_recognition({
70+
username: username,
71+
password: password,
72+
version: 'v1-beta'
73+
});
74+
75+
var file_extension = function (file) {
76+
var ext = '.jpeg';
77+
78+
// For URLs, look for file extension in the path, default to JPEG.
79+
if (typeof file === 'string') {
80+
var match = file.match(/\.[\w]{3,4}$/i)
81+
ext = match && match[0]
82+
// ...for Buffers, we can look at the file header.
83+
} else if (file instanceof Buffer) {
84+
ext = '.' + fileType(file).ext;
85+
}
86+
87+
return ext;
88+
}
89+
90+
var recognize = function (image, cb) {
91+
node.status({fill:"blue", shape:"dot", text:"requesting"});
92+
visual_recognition.recognize({image_file: image}, function(err, res) {
93+
node.status({})
94+
if (err) {
95+
node.error(err, msg);
96+
} else {
97+
msg.labels = res.images && res.images[0].labels;
98+
}
99+
100+
node.send(msg);
101+
if (cb) cb();
102+
});
103+
}
104+
105+
var stream_buffer = function (file, contents, cb) {
106+
fs.writeFile(file, contents, function (err) {
107+
if (err) throw err;
108+
cb();
109+
});
110+
};
111+
112+
var stream_url = function (file, location, cb) {
113+
var wstream = fs.createWriteStream(file)
114+
wstream.on('finish', cb);
115+
116+
request(location)
117+
.pipe(wstream);
118+
};
119+
120+
temp.open({suffix: file_extension(msg.payload)}, function (err, info) {
121+
if (err) throw err;
122+
123+
var stream_payload = (typeof msg.payload === 'string') ? stream_url : stream_buffer;
124+
125+
stream_payload(info.path, msg.payload, function () {
126+
recognize(fs.createReadStream(info.path), temp.cleanup);
127+
});
128+
});
129+
});
130+
}
131+
132+
RED.nodes.registerType("watson-visual-recognition",Node, {
133+
credentials: {
134+
username: {type:"text"},
135+
password: {type:"password"}
136+
}
137+
});
138+
139+
var appEnv = cfenv.getAppEnv();
140+
var visual = [];
141+
for (var i in appEnv.services) {
142+
if (i.match(/^(visual_recognition)/i)) {
143+
visual = visual.concat(appEnv.services[i].map(function(v) {
144+
return {
145+
name: v.name,
146+
label: v.label,
147+
username: v.credentials.username,
148+
password: v.credentials.password,
149+
};
150+
}));
151+
}
152+
}
153+
154+
RED.httpAdmin.get('/watson-visual-recognition/vcap', function(req,res) {
155+
res.send(JSON.stringify(visual));
156+
});
157+
158+
RED.httpAdmin.get('/watson-visual-recognition/list/:service', function(req,res) {
159+
160+
var username,password;
161+
var service = req.params.service;
162+
163+
for (var i2=0; i2 < visual.length; i2++) {
164+
if (visual[i2].name===service) {
165+
username = visual[i2].username;
166+
password = visual[i2].password;
167+
}
168+
}
169+
170+
var visual_recognition = watson.visual_recognition({
171+
username: username,
172+
password: password,
173+
version: 'v2-beta',
174+
version_date: '2015-12-02'
175+
});
176+
177+
visual_recognition.listClassifiers({},
178+
function(err, response) {
179+
if (err)
180+
res.send(err);
181+
else
182+
res.send(JSON.stringify(response));
183+
}
184+
);
185+
});
186+
187+
function VisualUtilNode(config) {
188+
189+
RED.nodes.createNode(this,config);
190+
191+
this.name = config.name;
192+
this.classifier = config.classifier;
193+
this.command = config.command;
194+
this.username = config.username;
195+
this.password = config.password;
196+
this.service = config.service;
197+
198+
var node = this;
199+
200+
this.doDelete = function(msg) {
201+
var visual_recognition = watson.visual_recognition({
202+
username: node.username,
203+
password: node.password,
204+
version: 'v2-beta',
205+
version_date: '2015-12-02'
206+
});
207+
208+
visual_recognition.deleteClassifier({
209+
classifier_id: node.classifier },
210+
function(err, response) {
211+
if (err)
212+
node.error(err);
213+
else
214+
node.send({"payload": response});
215+
}
216+
);
217+
};
218+
219+
this.doList = function(msg) {
220+
221+
var visual_recognition = watson.visual_recognition({
222+
223+
username: node.username,
224+
password: node.password,
225+
version: 'v2-beta',
226+
version_date: '2015-12-02'
227+
});
228+
229+
visual_recognition.listClassifiers({},
230+
function(err, response) {
231+
if (err) {
232+
node.error(err);
233+
} else {
234+
node.send({"payload": response});
235+
}
236+
}
237+
);
238+
239+
};
240+
241+
this.doDetails = function(msg) {
242+
var visual_recognition = watson.visual_recognition({
243+
username: node.username,
244+
password: node.password,
245+
version: 'v2-beta',
246+
version_date: '2015-12-02'
247+
});
248+
249+
visual_recognition.getClassifier({
250+
classifier_id: node.classifier },
251+
function(err, response) {
252+
if (err)
253+
node.error(err);
254+
else
255+
node.send({"payload": response});
256+
}
257+
);
258+
};
259+
260+
this.on('input', function (msg) {
261+
console.log("Visual command: "+node.command);
262+
switch(this.command) {
263+
case "list":
264+
this.doList(msg);
265+
break;
266+
case "details":
267+
this.doDetails(msg);
268+
break;
269+
case "delete":
270+
this.doDelete(msg);
271+
break;
272+
}
273+
});
274+
275+
}
276+
277+
RED.nodes.registerType("watson-visual-util",VisualUtilNode);
278+
279+
function VisualTrainingNode(config) {
280+
281+
RED.nodes.createNode(this,config);
282+
283+
this.name = config.name;
284+
this.classifier = config.classifier;
285+
this.username = config.username;
286+
this.password = config.password;
287+
this.service = config.service;
288+
289+
var node = this;
290+
291+
this.doCall = function(msg) {
292+
293+
var visual_recognition = watson.visual_recognition({
294+
username: node.username,
295+
password: node.password,
296+
version: 'v2-beta',
297+
version_date: '2015-12-02'
298+
});
299+
300+
var stream_buffer = function (file, contents, cb) {
301+
fs.writeFile(file, contents, function (err) {
302+
if (err) throw err;
303+
cb(fileType(contents).ext);
304+
});
305+
};
306+
307+
var stream_url = function (file, location, cb) {
308+
var wstream = fs.createWriteStream(file);
309+
wstream.on('finish', function () {
310+
fs.readFile(file, function (err, buf) {
311+
if (err) console.error(err);
312+
cb(fileType(buf).ext);
313+
});
314+
});
315+
request(location).pipe(wstream);
316+
};
317+
318+
var stream_positive = (typeof msg.positive === 'string') ? stream_url : stream_buffer;
319+
var stream_negative = (typeof msg.negative === 'string') ? stream_url : stream_buffer;
320+
321+
temp.open({suffix: '.zip'}, function (err, info) {
322+
if (err) throw err;
323+
324+
stream_positive(info.path, msg.positive, function (format) {
325+
326+
temp.open({suffix: '.zip'}, function (err2, info2) {
327+
if (err2) throw err2;
328+
329+
stream_negative(info2.path, msg.negative, function (format) {
330+
331+
var params = {
332+
name: node.classifier,
333+
positive_examples: fs.createReadStream(info.path),
334+
negative_examples: fs.createReadStream(info2.path)
335+
};
336+
337+
visual_recognition.createClassifier(params,
338+
function(err, response) {
339+
if (err) {
340+
node.error(err);
341+
} else {
342+
node.send({"payload" : response});
343+
}
344+
temp.cleanup();
345+
}
346+
);
347+
});
348+
});
349+
});
350+
});
351+
};
352+
353+
this.on('input', function (msg) {
354+
this.doCall(msg);
355+
});
356+
357+
}
358+
359+
RED.nodes.registerType("watson-visual-training",VisualTrainingNode);
360+
361+
=======
18362
var request = require('request');
19363
var cfenv = require('cfenv');
20364
var fs = require('fs');
@@ -131,4 +475,5 @@ module.exports = function(RED) {
131475
password: {type:"password"}
132476
}
133477
});
478+
>>>>>>> ded53bbc7e82d7da43797cbd08ec7895b7667588
134479
};

0 commit comments

Comments
 (0)