360 строки
17 KiB
C#
360 строки
17 KiB
C#
namespace Microsoft.Bing.VisualSearch.Samples
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Microsoft.Bing.VisualSearch;
|
|
using Microsoft.Bing.VisualSearch.Models;
|
|
using Newtonsoft.Json;
|
|
|
|
[SampleCollection("VisualSearch")]
|
|
public class VisualSearchSamples
|
|
{
|
|
[Example("This will send an image binary in the body of the post request and print out the imageInsightsToken, the number of tags, the number of actions, and the first actionType")]
|
|
public static void VisualSearchImageBinary(string subscriptionKey)
|
|
{
|
|
var client = new VisualSearchClient(new ApiKeyServiceClientCredentials(subscriptionKey));
|
|
|
|
try
|
|
{
|
|
using (FileStream stream = new FileStream(Path.Combine("TestImages", "image.jpg"), FileMode.Open))
|
|
{
|
|
// The knowledgeRequest parameter is not required if an image binary is passed in the request body
|
|
var visualSearchResults = client.Images.VisualSearchMethodAsync(image: stream, knowledgeRequest: (string)null).Result;
|
|
Console.WriteLine("Search visual search request with binary of eiffel tower image");
|
|
|
|
if (visualSearchResults == null)
|
|
{
|
|
Console.WriteLine("No visual search result data.");
|
|
}
|
|
else
|
|
{
|
|
// Visual Search results
|
|
if (visualSearchResults.Image?.ImageInsightsToken != null)
|
|
{
|
|
Console.WriteLine($"Uploaded image insights token: {visualSearchResults.Image.ImageInsightsToken}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image insights token!");
|
|
}
|
|
|
|
// List of tags
|
|
if (visualSearchResults.Tags.Count > 0)
|
|
{
|
|
var firstTagResult = visualSearchResults.Tags.First();
|
|
Console.WriteLine($"Visual search tag count: {visualSearchResults.Tags.Count}");
|
|
|
|
// List of actions in first tag
|
|
if (firstTagResult.Actions.Count > 0)
|
|
{
|
|
var firstActionResult = firstTagResult.Actions.First();
|
|
Console.WriteLine($"First tag action count: {firstTagResult.Actions.Count}");
|
|
Console.WriteLine($"First tag action type: {firstActionResult.ActionType}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find tag actions!");
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image tags!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Encountered exception. " + ex.Message);
|
|
}
|
|
}
|
|
|
|
[Example("This will send an image binary in the body of the post request, along with a cropArea object, and print out the imageInsightsToken, the number of tags, the number of actions, and the first actionType")]
|
|
public static void VisualSearchImageBinaryWithCropArea(string subscriptionKey)
|
|
{
|
|
var client = new VisualSearchClient(new ApiKeyServiceClientCredentials(subscriptionKey));
|
|
|
|
try
|
|
{
|
|
using (FileStream stream = new FileStream(Path.Combine("TestImages", "image.jpg"), FileMode.Open))
|
|
{
|
|
// The ImageInfo struct contains a crop area specifying a region to crop in the uploaded image
|
|
CropArea CropArea = new CropArea(top: (float)0.1, bottom: (float)0.5, left: (float)0.1, right: (float)0.9);
|
|
ImageInfo ImageInfo = new ImageInfo(cropArea: CropArea);
|
|
VisualSearchRequest VisualSearchRequest = new VisualSearchRequest(imageInfo: ImageInfo);
|
|
|
|
// The knowledgeRequest here holds additional information about the image, which is passed in in binary form
|
|
var visualSearchResults = client.Images.VisualSearchMethodAsync(image: stream, knowledgeRequest: VisualSearchRequest.ToString()).Result;
|
|
Console.WriteLine("Search visual search request with binary of eiffel tower image");
|
|
|
|
if (visualSearchResults == null)
|
|
{
|
|
Console.WriteLine("No visual search result data.");
|
|
}
|
|
else
|
|
{
|
|
// Visual Search results
|
|
if (visualSearchResults.Image?.ImageInsightsToken != null)
|
|
{
|
|
Console.WriteLine($"Uploaded image insights token: {visualSearchResults.Image.ImageInsightsToken}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image insights token!");
|
|
}
|
|
|
|
// List of tags
|
|
if (visualSearchResults.Tags.Count > 0)
|
|
{
|
|
var firstTagResult = visualSearchResults.Tags.First();
|
|
Console.WriteLine($"Visual search tag count: {visualSearchResults.Tags.Count}");
|
|
|
|
// List of actions in first tag
|
|
if (firstTagResult.Actions.Count > 0)
|
|
{
|
|
var firstActionResult = firstTagResult.Actions.First();
|
|
Console.WriteLine($"First tag action count: {firstTagResult.Actions.Count}");
|
|
Console.WriteLine($"First tag action type: {firstActionResult.ActionType}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find tag actions!");
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image tags!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Encountered exception. " + ex.Message);
|
|
}
|
|
}
|
|
|
|
[Example("This will send an image url in the knowledgeRequest parameter, along with a \"site:www.pinterest.com\" filter, and print out the imageInsightsToken, the number of tags, the number of actions, and the first actionType")]
|
|
public static void VisualSearchUrlWithFilters(string subscriptionKey)
|
|
{
|
|
var client = new VisualSearchClient(new ApiKeyServiceClientCredentials(subscriptionKey));
|
|
|
|
try
|
|
{
|
|
// The image can be specified via URL, in the ImageInfo object
|
|
var ImageUrl = "https://www.publicdomainpictures.net/pictures/80000/velka/paris-eiffel-tower-1393127408EHt.jpg";
|
|
ImageInfo ImageInfo = new ImageInfo(url: ImageUrl);
|
|
|
|
// Optional filters inside the knowledgeRequest will restrict similar products and images to certain domains
|
|
Filters Filters = new Filters(site: "www.pinterest.com");
|
|
KnowledgeRequest KnowledgeRequest = new KnowledgeRequest(filters: Filters);
|
|
|
|
// An image binary is not necessary here, as the image is specified via URL
|
|
VisualSearchRequest VisualSearchRequest = new VisualSearchRequest(imageInfo: ImageInfo, knowledgeRequest: KnowledgeRequest);
|
|
var visualSearchResults = client.Images.VisualSearchMethodAsync(knowledgeRequest: VisualSearchRequest.ToString()).Result;
|
|
Console.WriteLine("Search visual search request with url of eiffel tower image");
|
|
|
|
if (visualSearchResults == null)
|
|
{
|
|
Console.WriteLine("No visual search result data.");
|
|
}
|
|
else
|
|
{
|
|
// Visual Search results
|
|
if (visualSearchResults.Image?.ImageInsightsToken != null)
|
|
{
|
|
Console.WriteLine($"Uploaded image insights token: {visualSearchResults.Image.ImageInsightsToken}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image insights token!");
|
|
}
|
|
|
|
// List of tags
|
|
if (visualSearchResults.Tags.Count > 0)
|
|
{
|
|
var firstTagResult = visualSearchResults.Tags.First();
|
|
Console.WriteLine($"Visual search tag count: {visualSearchResults.Tags.Count}");
|
|
|
|
// List of actions in first tag
|
|
if (firstTagResult.Actions.Count > 0)
|
|
{
|
|
var firstActionResult = firstTagResult.Actions.First();
|
|
Console.WriteLine($"First tag action count: {firstTagResult.Actions.Count}");
|
|
Console.WriteLine($"First tag action type: {firstActionResult.ActionType}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find tag actions!");
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image tags!");
|
|
}
|
|
}
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Encountered exception. " + ex.Message);
|
|
}
|
|
}
|
|
|
|
[Example("This will send an image insights token in the knowledgeRequest parameter, along with a cropArea object, and print out the imageInsightsToken, the number of tags, the number of actions, and the first actionType")]
|
|
public static void VisualSearchInsightsTokenWithCropArea(string subscriptionKey)
|
|
{
|
|
var client = new VisualSearchClient(new ApiKeyServiceClientCredentials(subscriptionKey));
|
|
|
|
try
|
|
{
|
|
// The image can be specified via an insights token, in the ImageInfo object
|
|
var ImageInsightsToken = "bcid_CA6BDBEA28D57D52E0B9D4B254F1DF0D*ccid_6J+8V1zi*thid_R.CA6BDBEA28D57D52E0B9D4B254F1DF0D";
|
|
|
|
// An optional crop area can be passed in to define a region of interest in the image
|
|
CropArea CropArea = new CropArea(top: (float)0.1, bottom: (float)0.5, left: (float)0.1, right: (float)0.9);
|
|
ImageInfo ImageInfo = new ImageInfo(imageInsightsToken: ImageInsightsToken, cropArea: CropArea);
|
|
|
|
// An image binary is not necessary here, as the image is specified via insights token
|
|
VisualSearchRequest VisualSearchRequest = new VisualSearchRequest(imageInfo: ImageInfo);
|
|
var visualSearchResults = client.Images.VisualSearchMethodAsync(knowledgeRequest: VisualSearchRequest.ToString()).Result;
|
|
Console.WriteLine("Search visual search request with url of eiffel tower image");
|
|
|
|
if (visualSearchResults == null)
|
|
{
|
|
Console.WriteLine("No visual search result data.");
|
|
}
|
|
else
|
|
{
|
|
// Visual Search results
|
|
if (visualSearchResults.Image?.ImageInsightsToken != null)
|
|
{
|
|
Console.WriteLine($"Uploaded image insights token: {visualSearchResults.Image.ImageInsightsToken}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image insights token!");
|
|
}
|
|
|
|
// List of tags
|
|
if (visualSearchResults.Tags.Count > 0)
|
|
{
|
|
var firstTagResult = visualSearchResults.Tags.First();
|
|
Console.WriteLine($"Visual search tag count: {visualSearchResults.Tags.Count}");
|
|
|
|
// List of actions in first tag
|
|
if (firstTagResult.Actions.Count > 0)
|
|
{
|
|
var firstActionResult = firstTagResult.Actions.First();
|
|
Console.WriteLine($"First tag action count: {firstTagResult.Actions.Count}");
|
|
Console.WriteLine($"First tag action type: {firstActionResult.ActionType}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find tag actions!");
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image tags!");
|
|
}
|
|
}
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Encountered exception. " + ex.Message);
|
|
}
|
|
}
|
|
|
|
[Example("This will send an image url in the knowledgeRequest parameter, along with a crop area, and print out the imageInsightsToken, the number of tags, the number of actions, and the first actionType")]
|
|
public static void VisualSearchUrlWithJson(string subscriptionKey)
|
|
{
|
|
var client = new VisualSearchClient(new ApiKeyServiceClientCredentials(subscriptionKey));
|
|
|
|
try
|
|
{
|
|
/*
|
|
* The visual search request can be passed in as a JSON string
|
|
* The image is specified via URL in the ImageInfo object, along with a crop area as shown below:
|
|
* {
|
|
* "imageInfo": {
|
|
* "url": "https://www.publicdomainpictures.net/pictures/80000/velka/paris-eiffel-tower-1393127408EHt.jpg",
|
|
* "cropArea": {
|
|
* "top": 0.1,
|
|
* "bottom": 0.5,
|
|
* "left": 0.1,
|
|
* "right": 0.9
|
|
* }
|
|
* },
|
|
* "knowledgeRequest": {
|
|
* "filters": {
|
|
* "site": "www.pinterest.com"
|
|
* }
|
|
* }
|
|
* }
|
|
*/
|
|
var VisualSearchRequestJSON = "{\"imageInfo\":{\"url\":\"https://www.publicdomainpictures.net/pictures/80000/velka/paris-eiffel-tower-1393127408EHt.jpg\",\"cropArea\":{\"top\":0.1,\"bottom\":0.5,\"left\":0.1,\"right\":0.9}},\"knowledgeRequest\":{\"filters\":{\"site\":\"www.pinterest.com\"}}}";
|
|
|
|
// An image binary is not necessary here, as the image is specified via URL
|
|
var visualSearchResults = client.Images.VisualSearchMethodAsync(knowledgeRequest: VisualSearchRequestJSON).Result;
|
|
Console.WriteLine("Search visual search request with url of eiffel tower image");
|
|
|
|
if (visualSearchResults == null)
|
|
{
|
|
Console.WriteLine("No visual search result data.");
|
|
}
|
|
else
|
|
{
|
|
// Visual Search results
|
|
if (visualSearchResults.Image?.ImageInsightsToken != null)
|
|
{
|
|
Console.WriteLine($"Uploaded image insights token: {visualSearchResults.Image.ImageInsightsToken}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image insights token!");
|
|
}
|
|
|
|
// List of tags
|
|
if (visualSearchResults.Tags.Count > 0)
|
|
{
|
|
var firstTagResult = visualSearchResults.Tags.First();
|
|
Console.WriteLine($"Visual search tag count: {visualSearchResults.Tags.Count}");
|
|
|
|
// List of actions in first tag
|
|
if (firstTagResult.Actions.Count > 0)
|
|
{
|
|
var firstActionResult = firstTagResult.Actions.First();
|
|
Console.WriteLine($"First tag action count: {firstTagResult.Actions.Count}");
|
|
Console.WriteLine($"First tag action type: {firstActionResult.ActionType}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find tag actions!");
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Couldn't find image tags!");
|
|
}
|
|
}
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Encountered exception. " + ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|