|
| 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 | +} |
0 commit comments