A sample bot that illustrates how to use the Bing Image Search API to find visually similar products from an image stream or a URL. Here's a demo of this bot in a web chat.
The minimum prerequisites to run this sample are:
- Latest Node.js with NPM. Download it from here.
- The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.
- [Recommended] Visual Studio Code for IntelliSense and debugging, download it from here for free.
- This sample currently uses a free trial Microsoft Bing Search API key with limited QPS. Please subscribe here to obtain your own key and update the
BING_SEARCH_API_KEY
key in .env file to try it out further.
Microsoft Bing Image Search API provides a number of modules that allows you to search by image. Check out the reference to know more about the modules available. In this sample we are using the 'SimilarProducts' module to get similar products to an image. We set the 'modulesRequested' parameter to 'SimilarProducts' https://api.cognitive.microsoft.com/bing/v5.0/images/search?modulesRequested=SimilarProducts
The main components are:
- image-service.js: is the core component illustrating how to call the Bing Image Search RESTful API.
- app.js: is the bot service listener receiving messages from the connector service and passing them down to image-service.js and constructing the response.
In this sample we are using the API to get the similar products and send it back to the user. Check out the use of the imageService.getSimilarProductsFromStream(stream)
method in app.js.
if (hasImageAttachment(session)) {
var stream = getImageStreamFromMessage(session.message);
imageService
.getSimilarProductsFromStream(stream)
.then(function (visuallySimilarProducts) { handleApiResponse(session, visuallySimilarProducts); })
.catch(function (error) { handleErrorResponse(session, error); });
}
And here is the implementation of imageService.getSimilarProductsFromStream(stream)
in image-service.js
/**
* Gets the similar products of the image from an image stream
* @param {stream} stream The stream to an image.
* @return {Promise} Promise with visuallySimilarProducts array if succeeded, error otherwise
*/
exports.getSimilarProductsFromStream = function (stream) {
return new Promise(
function (resolve, reject) {
var requestData = {
url: BING_API_URL,
encoding: 'binary',
formData: {
file: stream
},
headers: {
'Ocp-Apim-Subscription-Key': BING_SEARCH_API_KEY
}
};
request.post(requestData, function (error, response, body) {
if (error) {
reject(error);
}
else if (response.statusCode !== 200) {
reject(body);
}
else {
resolve(JSON.parse(body).visuallySimilarProducts);
}
});
}
);
};
You will see the following when connecting the Bot to the Emulator and send it an image or a URL:
Input:
Output:
To get more information about how to get started in Bot Builder for Node and Microsoft Bing Images Search API please review the following resources: