How To Post A Image Correctly To Alchemy Node.js Server?
I try to implement a post method on the node.js server for alchemy. The problem is how to handle the give image provided be a mobile app. I get the Image as a part of a json. The j
Solution 1:
The Alchemy Vision API was deprecated in May this year, not sure that is related to the problem your having but migrating to the IBM Watson Visual Recognition service will likely be your best bet going forward.
See the service documentation and also the migration guide for details.
Solution 2:
I was able to do the post.
Inside the app.post function:
- Posting the image as a json content.
- Check the filesystem on the server
- using a callback function to connect to the alchemy service
Inside the callback function:
- set key of the alchemy service
- check the json content
- convert the image to a storeable format
- create a file to save a temp image
- write the temp image-file to a server directory
- close the file
- prepare the alchemy service parameter
- request tag information from alchemy
- delete temp file
- provide the tag result as callback
app.post function:
app.post('/visualRecognition', function(req, res){
var result = 'unassigned';
var path = '/app/public';
// getTags(req, result, callback)// check filesystemif (fs.existsSync(path)) {
console.log('---> starting point - path exists -> /app/public ');
if (onFinished.isFinished(req)) {
var input = req;
getTags(input, result, functionextractCallbackGetTags(req,result){
console.log('---> last step - extractCallbackGetTags --> RESULT: ', JSON.stringify(result));
//res.json(JSON.stringify(result));
res.end(JSON.stringify(result));
});
}
}
}); // end post
callback function:
functiongetTags(inputdata, result, callback){
var theInputData = inputdata;
var theImage = 'unassigned';
var filepath = '/app/public/images/temp.jpg';
var serverfilepath = '/images/temp.jpg';
var serverURL = 'http://XXXXXXXXXXXX.mybluemix.net';
var imageURL = serverURL + serverfilepath;
// prepare service callconsole.log('---> step 1 - alchemy_vision');
var alchemy_vision = watson.alchemy_vision({
api_key: 'XXXXXXXXXXXX'
});
// check dataif(theInputData.body.body.image) {
theImage = theInputData.body.body.image;
var theImageBuffer = decodeBase64Image(theImage);
// Create filevar fd = fs.openSync(filepath, 'w');
// Write file
fs.writeFile(filepath, theImageBuffer.data, function (err) {
// Save fileif(err) {
console.log(err);
result = {'err': err };
callback(theInputData,result);
} else {
console.log('--> step 2 - File saved!');
// Close file
fs.close( fd, function(err) {
if (err){
console.log(err);
callback(theInputData,result);
}
console.log('--> step 3 - File closed successfully.');
// prepare service parametervar params = {
url: imageURL
};
// call service
alchemy_vision.getImageKeywords(params, function (err, keywords){
if (err) {
console.log('---> alchemy_vision error:', err);
result = {'error': err };
callback(theInputData,result);
} else {
result = JSON.stringify(keywords, null, 2);
console.log('---> step 4 - alchemy_vision keywords JSON: ', JSON.stringify(keywords, null, 2));
// delete file
fs.unlink(filepath,function(err) {
if(err){
console.log('---> Error : ', err);
result = {'error': err };
callback(theInputData,result);
} else {
console.log('---> step 5 - File deleted successfully and JSON Result is: ', result);
callback(theInputData,result);
}}); // end delete file
}; // end else alchemy_vision
}); // end alchemy_vision
}); // end file close
}}); // end else and filewrite
} else { //end if
result = {'error': "No Image"};
//res.end(JSON.stringify(result));callback(theInputData,result);
}
}
poster result:
console result:
Post a Comment for "How To Post A Image Correctly To Alchemy Node.js Server?"