Are you sure you want to delete this access key?
# Image Caption Bot Sample
A sample bot that illustrates how to use the Microsoft Cognitive Services Computer Vision API to analyze an image from a stream or a URL and return to the user the image caption.
The minimum prerequisites to run this sample are:
MICROSOFT_VISION_API_KEY
key in .env file to try it out further.Microsoft Computer Vision API provides a number of methods that allows you to analyze an image. Check out Computer Vision API - v1.0 for a complete reference of the methods available. In this sample we are using the 'analyze' endpoint with the 'visualFeatures' parameter set to 'Description' https://api.projectoxford.ai/vision/v1.0/analyze/?visualFeatures=Description
The main components are:
In this sample we are using the API to get the image description and send it back to the user. Check out the use of the captionService.getCaptionFromStream(stream)
method in app.js.
if (hasImageAttachment(session)) {
var stream = getImageStreamFromMessage(session.message);
captionService
.getCaptionFromStream(stream)
.then(function (caption) { handleSuccessResponse(session, caption); })
.catch(function (error) { handleErrorResponse(session, error); });
}
And here is the implementation of captionService.getCaptionFromStream(stream)
in caption-service.js.
/**
* Gets the caption of the image from an image stream
* @param {stream} stream The stream to an image.
* @return {Promise} Promise with caption string if succeeded, error otherwise
*/
exports.getCaptionFromStream = function (stream) {
return new Promise(
function (resolve, reject) {
var requestData = {
url: VISION_URL,
encoding: 'binary',
headers: { 'content-type': 'application/octet-stream' }
};
stream.pipe(request.post(requestData, function (error, response, body) {
if (error) {
reject(error);
}
else if (response.statusCode !== 200) {
reject(body);
}
else {
resolve(extractCaption(JSON.parse(body)));
}
}));
}
);
};
You will see the following when connecting the Bot to the Emulator and send it an image URL:
Input:
Output:
You can also choose to upload an image directly to the bot:
To get more information about how to get started in Bot Builder for Node and Microsoft Cognitive Services Computer Vision API please review the following resources:
Press p or to see the previous file or, n or to see the next file
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?