-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (102 loc) · 5.02 KB
/
Program.cs
File metadata and controls
116 lines (102 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Microsoft.Azure.CognitiveServices.Samples.ComputerVision.DetectObjects
{
using Newtonsoft.Json.Linq;
class Program
{
// Add your Azure Computer Vision subscription key and endpoint to your environment variables
public const string subscriptionKey = Environment.GetEnvironmentVariable("COMPUTER_VISION_SUBSCRIPTION_KEY");
public const string endpoint = Environment.GetEnvironmentVariable("COMPUTER_VISION_ENDPOINT");
static void Main(string[] args)
{
DetectObjectSample.RunAsync(endpoint, subscriptionKey).Wait(5000);
Console.WriteLine("\nPress ENTER to exit.");
Console.ReadLine();
}
}
public class DetectObjectSample
{
public static async Task RunAsync(string endpoint, string key)
{
Console.WriteLine("Detect objects in images:");
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.
string remoteImageUrl = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/celebrities.jpg";
await DetectObjectsFromStreamAsync(imageFilePath, endpoint, key);
await DetectObjectsFromUrlAsync(remoteImageUrl, endpoint, key);
}
static async Task DetectObjectsFromStreamAsync(string imageFilePath, string endpoint, string subscriptionKey)
{
if (!File.Exists(imageFilePath))
{
Console.WriteLine("\nInvalid file path");
return;
}
try
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
string uri = $"{endpoint}/vision/v2.0/detect";
// Read the contents of the specified local image into a byte array.
byte[] byteData = GetImageAsByteArray(imageFilePath);
// Add the byte array as an octet stream to the request body.
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
// This example uses the "application/octet-stream" content type.
// The other content types you can use are "application/json" and "multipart/form-data".
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Asynchronously call the REST API method.
HttpResponseMessage response = await client.PostAsync(uri, content);
// Asynchronously get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n\n{0}\n", JToken.Parse(contentString).ToString());
}
}
catch (Exception e)
{
Console.WriteLine("\n" + e.Message);
}
}
static byte[] GetImageAsByteArray(string imageFilePath)
{
// Open a read-only file stream for the specified file.
using (FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
{
// Read the file's contents into a byte array.
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}
static async Task DetectObjectsFromUrlAsync(string imageUrl, string endpoint, string subscriptionKey)
{
if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
{
Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
return;
}
try
{
HttpClient client = new HttpClient();
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
string uri = $"{endpoint}/vision/v2.0/detect";
string requestBody = " {\"url\":\"" + imageUrl + "\"}";
var content = new StringContent(requestBody);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
// Post the request and display the result
HttpResponseMessage response = await client.PostAsync(uri, content);
string contentString = await response.Content.ReadAsStringAsync();
Console.WriteLine("\nResponse:\n\n{0}\n", JToken.Parse(contentString).ToString());
}
catch (Exception e)
{
Console.WriteLine("\n" + e.Message);
}
}
}
}