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 \n Image 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 \n Encountered 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 \n Visual 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