Skip to content

Commit 26f0ab6

Browse files
committed
2 parents 9856f4c + 1ca8112 commit 26f0ab6

File tree

208 files changed

+12490
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

208 files changed

+12490
-1
lines changed

Tutorials/Bing-Visual-Search/BingVisualSearchInisghtsTokens.cs renamed to Tutorials/Bing-Visual-Search/BingVisualSearchInsightsTokens.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,4 @@ public static void VisualSearchInsightsToken(string subscriptionKey, string insi
137137
}
138138
}
139139
}
140-
}
140+
}
691 KB
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>netcoreapp2.0;net461;netstandard1.4</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Http;
4+
using System.Net.Http.Headers;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Azure.CognitiveServices.Samples.ComputerVision.AnalyzeImage
8+
{
9+
using Newtonsoft.Json.Linq;
10+
11+
class Program
12+
{
13+
public const string subscriptionKey = "<your training key here>"; //Insert your Cognitive Services subscription key here
14+
public const string endpoint = "https://westus.api.cognitive.microsoft.com"; // You must use the same Azure region that you generated your subscription keys for. Free trial subscription keys are generated in the westus region.
15+
16+
static void Main(string[] args)
17+
{
18+
AnalyzeImageSample.RunAsync(endpoint, subscriptionKey).Wait(6000);
19+
Console.WriteLine("\nPress ENTER to exit.");
20+
Console.ReadLine();
21+
}
22+
}
23+
24+
public class AnalyzeImageSample
25+
{
26+
public static async Task RunAsync(string endpoint, string key)
27+
{
28+
Console.WriteLine("Images being analyzed:");
29+
30+
string imageFilePath = @"Images\faces.jpg"; // See this repo's readme.md for info on how to get these images. Alternatively, you can just set the path to any image on your machine.
31+
string remoteImageUrl = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/landmark.jpg";
32+
33+
await AnalyzeFromUrlAsync(remoteImageUrl, endpoint, key);
34+
await AnalyzeFromStreamAsync(imageFilePath, endpoint, key);
35+
}
36+
37+
static async Task AnalyzeFromStreamAsync(string imageFilePath, string endpoint, string subscriptionKey)
38+
{
39+
if (!File.Exists(imageFilePath))
40+
{
41+
Console.WriteLine("Invalid file path");
42+
return;
43+
}
44+
45+
try
46+
{
47+
HttpClient client = new HttpClient();
48+
49+
// Request headers.
50+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
51+
52+
// Request parameters. A third optional parameter is "details".
53+
// Comment parameters that aren't required
54+
string requestParameters = "visualFeatures=" +
55+
"Categories," +
56+
"Description," +
57+
"Color, " +
58+
"Tags, " +
59+
"Faces, " +
60+
"ImageType, " +
61+
"Adult , " +
62+
"Brands , " +
63+
"Objects"
64+
;
65+
66+
// Assemble the URI for the REST API method.
67+
string uriBase = endpoint+@"/vision/v2.0/analyze";
68+
string uri = uriBase + "?" + requestParameters;
69+
70+
// Read the contents of the specified local image
71+
// into a byte array.
72+
byte[] byteData = GetImageAsByteArray(imageFilePath);
73+
74+
// Add the byte array as an octet stream to the request body.
75+
using (ByteArrayContent content = new ByteArrayContent(byteData))
76+
{
77+
// This example uses the "application/octet-stream" content type.
78+
// The other content types you can use are "application/json" and "multipart/form-data".
79+
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
80+
// Asynchronously call the REST API method.
81+
HttpResponseMessage response = await client.PostAsync(uri, content);
82+
// Asynchronously get the JSON response.
83+
string contentString = await response.Content.ReadAsStringAsync();
84+
85+
// Display the JSON response.
86+
Console.WriteLine("\nResponse:\n\n{0}\n", JToken.Parse(contentString).ToString());
87+
}
88+
}
89+
catch (Exception e)
90+
{
91+
Console.WriteLine("\n" + e.Message);
92+
}
93+
}
94+
95+
static byte[] GetImageAsByteArray(string imageFilePath)
96+
{
97+
// Open a read-only file stream for the specified file.
98+
using (FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
99+
{
100+
// Read the file's contents into a byte array.
101+
BinaryReader binaryReader = new BinaryReader(fileStream);
102+
return binaryReader.ReadBytes((int)fileStream.Length);
103+
}
104+
}
105+
106+
static async Task AnalyzeFromUrlAsync(string imageUrl, string endpoint, string subscriptionKey)
107+
{
108+
if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
109+
{
110+
Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
111+
return;
112+
}
113+
114+
try
115+
{
116+
HttpClient client = new HttpClient();
117+
118+
// Request headers.
119+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
120+
121+
// Request parameters. A third optional parameter is "details".
122+
// Comment parameters that aren't required
123+
string requestParameters = "visualFeatures=" +
124+
"Categories," +
125+
"Description," +
126+
"Color, " +
127+
"Tags, " +
128+
"Faces, " +
129+
"ImageType, " +
130+
"Adult , " +
131+
"Brands , " +
132+
"Objects"
133+
;
134+
135+
//Assemble the URI and content header for the REST API request
136+
string uriBase = endpoint + @"/vision/v2.0/analyze";
137+
string uri = uriBase + "?" + requestParameters;
138+
139+
string requestBody = " {\"url\":\"" + imageUrl + "\"}";
140+
var content = new StringContent(requestBody);
141+
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
142+
143+
// Post the request and display the result
144+
HttpResponseMessage response = await client.PostAsync(uri, content);
145+
string contentString = await response.Content.ReadAsStringAsync();
146+
Console.WriteLine("\nResponse:\n\n{0}\n", JToken.Parse(contentString).ToString());
147+
}
148+
catch (Exception e)
149+
{
150+
Console.WriteLine("\n" + e.Message);
151+
}
152+
}
153+
}
154+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.539
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnalyzeImage", "AnalyzeImage\Microsoft.Azure.CognitiveServices.Samples.ComputerVision.AnalyzeImage.csproj", "{D80D48F2-F52C-4649-A298-FB9F22668B19}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DetectObjects", "DetectObjects\Microsoft.Azure.CognitiveServices.Samples.ComputerVision.DetectObjects.csproj", "{ECC75ECA-5F9E-49FA-8480-B94A8A5DBB97}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtractText", "ExtractText\Microsoft.Azure.CognitiveServices.Samples.ComputerVision.ExtractText.csproj", "{C813CB28-39BD-4383-A59B-99020576057F}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OCR", "OCR\Microsoft.Azure.CognitiveServices.Samples.ComputerVision.OCR.csproj", "{F3F98FCD-728E-424C-A66E-64F5F0870298}"
13+
EndProject
14+
Global
15+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16+
Debug|Any CPU = Debug|Any CPU
17+
Release|Any CPU = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
20+
{D80D48F2-F52C-4649-A298-FB9F22668B19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{D80D48F2-F52C-4649-A298-FB9F22668B19}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{D80D48F2-F52C-4649-A298-FB9F22668B19}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{D80D48F2-F52C-4649-A298-FB9F22668B19}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{ECC75ECA-5F9E-49FA-8480-B94A8A5DBB97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{ECC75ECA-5F9E-49FA-8480-B94A8A5DBB97}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{ECC75ECA-5F9E-49FA-8480-B94A8A5DBB97}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{ECC75ECA-5F9E-49FA-8480-B94A8A5DBB97}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{C813CB28-39BD-4383-A59B-99020576057F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{C813CB28-39BD-4383-A59B-99020576057F}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{C813CB28-39BD-4383-A59B-99020576057F}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{C813CB28-39BD-4383-A59B-99020576057F}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{F3F98FCD-728E-424C-A66E-64F5F0870298}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{F3F98FCD-728E-424C-A66E-64F5F0870298}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{F3F98FCD-728E-424C-A66E-64F5F0870298}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{F3F98FCD-728E-424C-A66E-64F5F0870298}.Release|Any CPU.Build.0 = Release|Any CPU
36+
EndGlobalSection
37+
GlobalSection(SolutionProperties) = preSolution
38+
HideSolutionNode = FALSE
39+
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {4E45C63C-8CE1-48AD-A295-05FBCE469D66}
42+
EndGlobalSection
43+
EndGlobal
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>netcoreapp2.0;net461;netstandard1.4</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Http;
4+
using System.Net.Http.Headers;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Azure.CognitiveServices.Samples.ComputerVision.DetectObjects
8+
{
9+
using Newtonsoft.Json.Linq;
10+
11+
class Program
12+
{
13+
public const string subscriptionKey = "<your training key here>"; //Insert your Cognitive Services subscription key here
14+
public const string endpoint = "https://westus.api.cognitive.microsoft.com"; // You must use the same Azure region that you generated your subscription keys for. Free trial subscription keys are generated in the westus region.
15+
16+
static void Main(string[] args)
17+
{
18+
DetectObjectSample.RunAsync(endpoint, subscriptionKey).Wait(5000);
19+
20+
Console.WriteLine("\nPress ENTER to exit.");
21+
Console.ReadLine();
22+
}
23+
}
24+
25+
public class DetectObjectSample
26+
{
27+
public static async Task RunAsync(string endpoint, string key)
28+
{
29+
Console.WriteLine("Detect objects in images:");
30+
31+
string imageFilePath = @"Images\objects.jpg"; // See this repo's readme.md for info on how to get these images. Alternatively, you can just set the path to any appropriate image on your machine.
32+
string remoteImageUrl = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/celebrities.jpg";
33+
34+
await DetectObjectsFromStreamAsync(imageFilePath, endpoint, key);
35+
await DetectObjectsFromUrlAsync(remoteImageUrl, endpoint, key);
36+
}
37+
38+
static async Task DetectObjectsFromStreamAsync(string imageFilePath, string endpoint, string subscriptionKey)
39+
{
40+
if (!File.Exists(imageFilePath))
41+
{
42+
Console.WriteLine("\nInvalid file path");
43+
return;
44+
}
45+
try
46+
{
47+
HttpClient client = new HttpClient();
48+
49+
// Request headers.
50+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
51+
string uri = endpoint+@"/vision/v2.0/detect";
52+
// Read the contents of the specified local image into a byte array.
53+
byte[] byteData = GetImageAsByteArray(imageFilePath);
54+
// Add the byte array as an octet stream to the request body.
55+
using (ByteArrayContent content = new ByteArrayContent(byteData))
56+
{
57+
// This example uses the "application/octet-stream" content type.
58+
// The other content types you can use are "application/json" and "multipart/form-data".
59+
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
60+
61+
// Asynchronously call the REST API method.
62+
HttpResponseMessage response = await client.PostAsync(uri, content);
63+
// Asynchronously get the JSON response.
64+
string contentString = await response.Content.ReadAsStringAsync();
65+
// Display the JSON response.
66+
Console.WriteLine("\nResponse:\n\n{0}\n", JToken.Parse(contentString).ToString());
67+
}
68+
}
69+
catch (Exception e)
70+
{
71+
Console.WriteLine("\n" + e.Message);
72+
}
73+
}
74+
75+
static byte[] GetImageAsByteArray(string imageFilePath)
76+
{
77+
// Open a read-only file stream for the specified file.
78+
using (FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
79+
{
80+
// Read the file's contents into a byte array.
81+
BinaryReader binaryReader = new BinaryReader(fileStream);
82+
return binaryReader.ReadBytes((int)fileStream.Length);
83+
}
84+
}
85+
86+
static async Task DetectObjectsFromUrlAsync(string imageUrl, string endpoint, string subscriptionKey)
87+
{
88+
if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
89+
{
90+
Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
91+
return;
92+
}
93+
try
94+
{
95+
HttpClient client = new HttpClient();
96+
// Request headers
97+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
98+
string uri = endpoint + @"/vision/v2.0/detect";
99+
100+
string requestBody = " {\"url\":\"" + imageUrl + "\"}";
101+
var content = new StringContent(requestBody);
102+
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
103+
104+
// Post the request and display the result
105+
HttpResponseMessage response = await client.PostAsync(uri, content);
106+
string contentString = await response.Content.ReadAsStringAsync();
107+
Console.WriteLine("\nResponse:\n\n{0}\n", JToken.Parse(contentString).ToString());
108+
}
109+
catch (Exception e)
110+
{
111+
Console.WriteLine("\n" + e.Message);
112+
}
113+
}
114+
}
115+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>netcoreapp2.0;net461;netstandard1.4</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)