Skip to content

Commit

Permalink
Added VisualSearch sample
Browse files Browse the repository at this point in the history
  • Loading branch information
kirthik committed May 14, 2018
1 parent 5c2ff78 commit a347fb2
Show file tree
Hide file tree
Showing 6 changed files with 4,275 additions and 152 deletions.
Binary file added Data/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
services: cognitive-services
platforms: node.js
author: kirthik
---
# Cognitive Services SDK Samples

These samples will show you how to get up and running using the SDKs for various Cognitive Services. They'll cover a few rudimentary use cases and hopefully express best practices for interacting with the data from these APIs.
Expand All @@ -14,6 +19,7 @@ This project framework provides examples for the following services:
* Using the **Bing News Search SDK** \[[individual package](https://www.npmjs.com/package/azure-cognitiveservices-newssearch)\]\[[complete package](https://www.npmjs.com/package/azure-cognitiveservices-search)\] for the [News Search API](https://azure.microsoft.com/en-us/services/cognitive-services/bing-news-search-api/)
* Using the **Bing Image Search SDK** \[[individual package](https://www.npmjs.com/package/azure-cognitiveservices-imagesearch)\]\[[complete package](https://www.npmjs.com/package/azure-cognitiveservices-search)\] for the [Image Search API](https://azure.microsoft.com/en-us/services/cognitive-services/bing-image-search-api/)
* Using the **Bing Custom Search SDK** \[[individual package](https://www.npmjs.com/package/azure-cognitiveservices-customsearch)\]\[[complete package](https://www.npmjs.com/package/azure-cognitiveservices-search)\] for the [Custom Search API](https://azure.microsoft.com/en-us/services/cognitive-services/bing-custom-search/)
* Using the **Bing Visual Search SDK** \[[individual package](https://www.npmjs.com/package/azure-cognitiveservices-visualsearch)\]\[[complete package](https://www.npmjs.com/package/azure-cognitiveservices-search)\] for the [Visual Search API](https://azure.microsoft.com/en-us/services/cognitive-services/bing-visual-search/)
* Using the **Bing Spell Check SDK** \[[individual package](https://www.npmjs.com/package/azure-cognitiveservices-spellcheck)\]\[[complete package](https://www.npmjs.com/package/azure-cognitiveservices-language)\] for the [Spell Check API](https://azure.microsoft.com/en-us/services/cognitive-services/spell-check/)
* Using the **Computer Vision SDK** \[[individual package](https://www.npmjs.com/package/azure-cognitiveservices-computervision)\]\[[complete package](https://www.npmjs.com/package/azure-cognitiveservices-vision)\] for the [Computer Vision API](https://azure.microsoft.com/en-us/services/cognitive-services/computer-vision/)
* Using the **Content Moderator SDK** \[[individual package](https://www.npmjs.com/package/azure-cognitiveservices-contentmoderator)\]\[[complete package](https://www.npmjs.com/package/azure-cognitiveservices-vision)\] for the [Content Moderator API](https://azure.microsoft.com/en-us/services/cognitive-services/content-moderator/)
Expand Down Expand Up @@ -49,6 +55,7 @@ This project framework provides examples for the following services:
export AZURE_NEWS_SEARCH_KEY={your service key}
export AZURE_IMAGE_SEARCH_KEY={your service key}
export AZURE_CUSTOM_SEARCH_KEY={your service key}
export AZURE_VISUAL_SEARCH_KEY={your service key}
export AZURE_SPELL_CHECK_KEY={your service key}
export AZURE_COMPUTER_VISION_KEY={your service key}
export AZURE_CONTENT_MODERATOR_KEY={your service key}
Expand Down
231 changes: 231 additions & 0 deletions Samples/visualSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
'use strict';

const os = require("os");
const async = require('async');
const fs = require('fs');
const Search = require('azure-cognitiveservices-visualsearch');
const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;

let keyVar = 'AZURE_VISUAL_SEARCH_KEY';

if (!process.env[keyVar]) {
throw new Error('please set/export the following environment variable: ' + keyVar);
}

let serviceKey = process.env[keyVar];

///////////////////////////////////////////
// Entrypoint for sample script //
///////////////////////////////////////////

let credentials = new CognitiveServicesCredentials(serviceKey);
let visualSearchApiClient = new Search.VisualSearchAPIClient(credentials);
let visualModels = visualSearchApiClient.models;

function sample() {
async.series([
async function () {
let fileStream = fs.createReadStream('Data/image.jpg');
let visualSearchRequest = JSON.stringify({});
let visualSearchResults;
try {
visualSearchResults = await visualSearchApiClient.images.visualSearch({
image: fileStream,
knowledgeRequest: visualSearchRequest
});
console.log("Search visual search request with binary of dog image");
} catch (err) {
console.log("Encountered exception. " + err.message);
}
if (!visualSearchResults) {
console.log("No visual search result data. ");
} else {
// Visual Search results
if (visualSearchResults.image.imageInsightsToken) {
console.log(`Uploaded image insights token: ${visualSearchResults.image.imageInsightsToken}`);
}
else {
console.log("Couldn't find image insights token!");
}

// List of tags
if (visualSearchResults.tags.length > 0) {
let firstTagResult = visualSearchResults.tags[0];
console.log(`Visual search tag count: ${visualSearchResults.tags.length}`);

// List of actions in first tag
if (firstTagResult.actions.length > 0) {
let firstActionResult = firstTagResult.actions[0];
console.log(`First tag action count: ${firstTagResult.actions.length}`);
console.log(`First tag action type: ${firstActionResult.actionType}`);
}
else {
console.log("Couldn't find tag actions!");
}

}
else {
console.log("Couldn't find image tags!");
}
}
},
async function () {
let fileStream = fs.createReadStream('Data/image.jpg');
let cropArea = { top: 0.1, bottom: 0.5, left: 0.1, right: 0.9 };
let imageInfo = { cropArea: cropArea };
let visualSearchRequest = JSON.stringify({ imageInfo: imageInfo });
let visualSearchResults;
try {
visualSearchResults = await visualSearchApiClient.images.visualSearch({
image: fileStream,
knowledgeRequest: visualSearchRequest
});
console.log("Search visual search request with binary of dog image");
if (!visualSearchResults) {
console.log("No visual search result data.");
}
else {
// Visual Search results
if (visualSearchResults.image.imageInsightsToken) {
console.log(`Uploaded image insights token: ${visualSearchResults.image.imageInsightsToken}`);
}
else {
console.log("Couldn't find image insights token!");
}

// List of tags
if (visualSearchResults.tags.length > 0) {
var firstTagResult = visualSearchResults.tags[0];
console.log(`Visual search tag count: ${visualSearchResults.tags.length}`);

// List of actions in first tag
if (firstTagResult.actions.length > 0) {
var firstActionResult = firstTagResult.actions[0];
console.log(`First tag action count: ${firstTagResult.actions.length}`);
console.log(`First tag action type: ${firstActionResult.actionType}`);
}
else {
console.log("Couldn't find tag actions!");
}

}
else {
console.log("Couldn't find image tags!");
}
}
} catch (err) {
console.log("Encountered exception. " + err.message);
}
},
async function () {
let imageInfo = { url: "https://images.unsplash.com/photo-1512546148165-e50d714a565a?w=600&q=80" };
let filters = { site: "www.bing.com" };
let knowledgeRequest = { filters: filters };
let visualSearchRequest = JSON.stringify({ imageInfo: imageInfo, knowledgeRequest: knowledgeRequest });
let visualSearchResults;
try {
visualSearchResults = await visualSearchApiClient.images.visualSearch({
knowledgeRequest: visualSearchRequest
});
console.log("Search visual search request with binary of dog image");
if (!visualSearchResults) {
console.log("No visual search result data.");
}
else {
// Visual Search results
if (visualSearchResults.image.imageInsightsToken) {
console.log(`Uploaded image insights token: ${visualSearchResults.image.imageInsightsToken}`);
}
else {
console.log("Couldn't find image insights token!");
}

// List of tags
if (visualSearchResults.tags.length > 0) {
var firstTagResult = visualSearchResults.tags[0];
console.log(`Visual search tag count: ${visualSearchResults.tags.length}`);

// List of actions in first tag
if (firstTagResult.actions.length > 0) {
var firstActionResult = firstTagResult.actions[0];
console.log(`First tag action count: ${firstTagResult.actions.length}`);
console.log(`First tag action type: ${firstActionResult.actionType}`);
}
else {
console.log("Couldn't find tag actions!");
}

}
else {
console.log("Couldn't find image tags!");
}
}
} catch (err) {
console.log("Encountered exception. " + err.message);
}
},
async function () {
var imageInsightsToken = "bcid_113F29C079F18F385732D8046EC80145*ccid_oV/QcH95*mid_687689FAFA449B35BC11A1AE6CEAB6F9A9B53708*thid_R.113F29C079F18F385732D8046EC80145";
let cropArea = { top: 0.1, bottom: 0.5, left: 0.1, right: 0.9 };
let imageInfo = { imageInsightsToken: imageInsightsToken, cropArea: cropArea };
let visualSearchRequest = JSON.stringify({ imageInfo: imageInfo });
let visualSearchResults;
try {
visualSearchResults = await visualSearchApiClient.images.visualSearch({
knowledgeRequest: visualSearchRequest
});
console.log("Search visual search request with binary of dog image");
if (!visualSearchResults) {
console.log("No visual search result data.");
}
else {
// Visual Search results
if (visualSearchResults.image.imageInsightsToken) {
console.log(`Uploaded image insights token: ${visualSearchResults.image.imageInsightsToken}`);
}
else {
console.log("Couldn't find image insights token!");
}

// List of tags
if (visualSearchResults.tags.length > 0) {
var firstTagResult = visualSearchResults.tags[0];
console.log(`Visual search tag count: ${visualSearchResults.tags.length}`);

// List of actions in first tag
if (firstTagResult.actions.length > 0) {
var firstActionResult = firstTagResult.actions[0];
console.log(`First tag action count: ${firstTagResult.actions.length}`);
console.log(`First tag action type: ${firstActionResult.actionType}`);
}
else {
console.log("Couldn't find tag actions!");
}

}
else {
console.log("Couldn't find image tags!");
}
}
} catch (err) {
console.log("Encountered exception. " + err.message);
}
},
function () {
return new Promise((resolve) => {
console.log(os.EOL);
console.log("Finished running Visual-Search sample.");
resolve();
})
}
], (err) => {
throw (err);
});
}

exports.sample = sample;
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const samples = {
VideoSearch: './Samples/videoSearch',
NewsSearch: './Samples/newsSearch',
ImageSearch: './Samples/imageSearch',
CustomSearch: './Samples/customSearch'
CustomSearch: './Samples/customSearch',
VisualSearch: './Samples/visualSearch'
},
Vision: {
ComputerVision: './Samples/computerVision',
Expand Down
Loading

0 comments on commit a347fb2

Please sign in to comment.