Skip to content

Commit 8b52984

Browse files
committed
adding bing search files
1 parent 391e3d6 commit 8b52984

File tree

10 files changed

+1919
-0
lines changed

10 files changed

+1919
-0
lines changed

Tutorials/Bing-Visual-Search/BingVisualSearchApp.html

Lines changed: 637 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using Microsoft.Azure.CognitiveServices.Search.VisualSearch;
5+
using Microsoft.Azure.CognitiveServices.Search.VisualSearch.Models;
6+
using Microsoft.Azure.CognitiveServices.Search.ImageSearch;
7+
8+
namespace VisualSearchFeatures
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
String subscriptionKey = "YOUR-ACCESS-KEY";
15+
16+
VisualSearchImageBinaryWithCropArea(subscriptionKey);
17+
18+
Console.WriteLine("Any key to quit...");
19+
Console.ReadKey();
20+
21+
}
22+
23+
public static void VisualSearchImageBinaryWithCropArea(string subscriptionKey)
24+
{
25+
var client = new VisualSearchAPI(new Microsoft.Azure.CognitiveServices.Search.VisualSearch.ApiKeyServiceClientCredentials(subscriptionKey));
26+
27+
try
28+
{
29+
CropArea CropArea = new CropArea(top: (float)0.01, bottom: (float)0.30, left: (float)0.01, right: (float)0.20);
30+
31+
// The ImageInfo struct specifies the crop area in the image and the URL of the larger image.
32+
string imageURL = "https://docs.microsoft.com/azure/cognitive-services/bing-visual-search/media/ms_srleaders.jpg";
33+
ImageInfo imageInfo = new ImageInfo(cropArea: CropArea, url: imageURL);
34+
35+
VisualSearchRequest visualSearchRequest = new VisualSearchRequest(imageInfo: imageInfo);
36+
37+
var visualSearchResults = client.Images.VisualSearchMethodAsync(knowledgeRequest: visualSearchRequest).Result;
38+
39+
Console.WriteLine("\r\nVisual search request with image from URL and crop area combined in knowledgeRequest");
40+
41+
if (visualSearchResults == null)
42+
{
43+
Console.WriteLine("No visual search result data.");
44+
}
45+
else
46+
{
47+
// List of tags
48+
if (visualSearchResults.Tags.Count > 0)
49+
{
50+
51+
foreach(ImageTag t in visualSearchResults.Tags)
52+
{
53+
foreach (ImageAction i in t.Actions)
54+
{
55+
Console.WriteLine("\r\n" + "ActionType: " + i.ActionType + " -> WebSearchUrl: " + i.WebSearchUrl);
56+
57+
if (i.ActionType == "PagesIncluding")
58+
{
59+
foreach(ImageObject o in (i as ImageModuleAction).Data.Value)
60+
{
61+
Console.WriteLine("ContentURL: " + o.ContentUrl);
62+
}
63+
}
64+
}
65+
66+
}
67+
68+
}
69+
else
70+
{
71+
Console.WriteLine("Couldn't find image tags!");
72+
}
73+
}
74+
}
75+
76+
catch (Exception ex)
77+
{
78+
Console.WriteLine("Encountered exception. " + ex.Message);
79+
}
80+
}
81+
}
82+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using Microsoft.Azure.CognitiveServices.Search.VisualSearch;
5+
using Microsoft.Azure.CognitiveServices.Search.VisualSearch.Models;
6+
using Microsoft.Azure.CognitiveServices.Search.ImageSearch;
7+
8+
namespace VisualSearchFeatures
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
String subscriptionKey = "YOUR-ACCESS-KEY";
15+
16+
insightsToken = ImageResults(subscriptionKey);
17+
18+
VisualSearchInsightsToken(subscriptionKey, insightsToken);
19+
20+
Console.WriteLine("Any key to quit...");
21+
Console.ReadKey();
22+
23+
}
24+
25+
// Searches for results on query (Canadian Rockies) and gets an ImageInsightsToken.
26+
// Also prints first image insights token, thumbnail url, and image content url.
27+
private static String ImageResults(String subKey)
28+
{
29+
String insightTok = "None";
30+
try
31+
{
32+
var client = new ImageSearchAPI(new Microsoft.Azure.CognitiveServices.Search.ImageSearch.ApiKeyServiceClientCredentials(subKey)); //
33+
var imageResults = client.Images.SearchAsync(query: "canadian rockies").Result;
34+
Console.WriteLine("Search images for query \"canadian rockies\"");
35+
36+
if (imageResults == null)
37+
{
38+
Console.WriteLine("No image result data.");
39+
}
40+
else
41+
{
42+
// Image results
43+
if (imageResults.Value.Count > 0)
44+
{
45+
var firstImageResult = imageResults.Value.First();
46+
47+
Console.WriteLine($"\r\nImage result count: {imageResults.Value.Count}");
48+
insightTok = firstImageResult.ImageInsightsToken;
49+
Console.WriteLine($"First image insights token: {firstImageResult.ImageInsightsToken}");
50+
Console.WriteLine($"First image thumbnail url: {firstImageResult.ThumbnailUrl}");
51+
Console.WriteLine($"First image content url: {firstImageResult.ContentUrl}");
52+
}
53+
else
54+
{
55+
insightTok = "None found";
56+
Console.WriteLine("Couldn't find image results!");
57+
}
58+
}
59+
}
60+
61+
catch (Exception ex)
62+
{
63+
Console.WriteLine("\r\nEncountered exception. " + ex.Message);
64+
}
65+
66+
return insightTok;
67+
}
68+
69+
70+
// This method will get Visual Search results from an imageInsightsToken obtained from the return value of the ImageResults method.
71+
// The method includes imageInsightsToken the in a knowledgeRequest parameter, along with a cropArea object.
72+
// It prints out the imageInsightsToken uploaded in the request.
73+
// Finally the example prints URLs of images found using the imageInsightsToken.
74+
75+
public static void VisualSearchInsightsToken(string subscriptionKey, string insightsTok)
76+
{
77+
var client = new VisualSearchAPI(new Microsoft.Azure.CognitiveServices.Search.VisualSearch.ApiKeyServiceClientCredentials(subscriptionKey));
78+
79+
try
80+
{
81+
// The image can be specified via an insights token, in the ImageInfo object.
82+
ImageInfo ImageInfo = new ImageInfo(imageInsightsToken: insightsTok);
83+
84+
// An image binary is not necessary here, as the image is specified by insights token.
85+
VisualSearchRequest VisualSearchRequest = new VisualSearchRequest(ImageInfo);
86+
87+
var visualSearchResults = client.Images.VisualSearchMethodAsync(knowledgeRequest: VisualSearchRequest).Result;
88+
Console.WriteLine("\r\nVisual search request with imageInsightsToken and knowledgeRequest");
89+
90+
if (visualSearchResults == null)
91+
{
92+
Console.WriteLine("No visual search result data.");
93+
}
94+
else
95+
{
96+
// Visual Search results
97+
if (visualSearchResults.Image?.ImageInsightsToken != null)
98+
{
99+
Console.WriteLine($"Uploaded image insights token: {insightsTok}");
100+
}
101+
else
102+
{
103+
Console.WriteLine("Couldn't find image insights token!");
104+
}
105+
106+
if (visualSearchResults.Tags.Count > 0)
107+
{
108+
// List of tags
109+
foreach (ImageTag t in visualSearchResults.Tags)
110+
{
111+
foreach (ImageAction i in t.Actions)
112+
{
113+
Console.WriteLine("\r\n" + "ActionType: " + i.ActionType + " WebSearchURL: " + i.WebSearchUrl);
114+
115+
if (i.ActionType == "PagesIncluding")
116+
{
117+
foreach (ImageObject o in (i as ImageModuleAction).Data.Value)
118+
{
119+
Console.WriteLine("ContentURL: " + o.ContentUrl);
120+
}
121+
}
122+
}
123+
}
124+
}
125+
126+
else
127+
{
128+
Console.WriteLine("Couldn't find image tags!");
129+
}
130+
131+
}
132+
}
133+
134+
catch (Exception ex)
135+
{
136+
Console.WriteLine("Encountered exception. " + ex.Message);
137+
}
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)