-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modifying an existing quickstart for consistency (#38)
* Added GetThumbnail quickstart * Updated readme * Used Path inbuilt functions to replace clunky hardcoded line * Updated Readme
- Loading branch information
1 parent
164d180
commit 9d7393e
Showing
4 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...GetThumbnail/Microsoft.Azure.CognitiveServices.Samples.ComputerVision.GetThumbnail.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>netcoreapp2.0;net461;netstandard1.4</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.CognitiveServices.Samples.ComputerVision.GetThumbnail | ||
{ | ||
using Newtonsoft.Json.Linq; | ||
|
||
class Program | ||
{ | ||
public const string subscriptionKey = "<your training key here>"; //Insert your Cognitive Services subscription key here | ||
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. | ||
|
||
static void Main(string[] args) | ||
{ | ||
GetThumbnailSample.RunAsync(endpoint, subscriptionKey).Wait(5000); | ||
|
||
Console.WriteLine("\nPress ENTER to exit."); | ||
Console.ReadLine(); | ||
} | ||
} | ||
|
||
public class GetThumbnailSample | ||
{ | ||
public static async Task RunAsync(string endpoint, string key) | ||
{ | ||
Console.WriteLine("Get thumbnail of specific size 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 GetThumbnailFromStreamAsync(imageFilePath, endpoint, key, 50, 60, "."); | ||
await GetThumbnailFromUrlAsync(remoteImageUrl, endpoint, key, 50, 60, "."); | ||
} | ||
|
||
static async Task GetThumbnailFromStreamAsync(string imageFilePath, string endpoint, string subscriptionKey, int width, int height, string localSavePath) | ||
{ | ||
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 uriBase = $"{endpoint}/vision/v2.0/generateThumbnail"; | ||
string requestParameters = $"width={width}&height={height}&smartCropping=true"; | ||
// Assemble the URI for the REST API method. | ||
string uri = uriBase + "?" + requestParameters; | ||
|
||
// 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); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
//Display the response | ||
Console.WriteLine("\nResponse:\n{0}", response); | ||
//Get the thumbnail image to save | ||
byte[] thumbnailImageData = await response.Content.ReadAsByteArrayAsync(); | ||
//Save the thumbnail image. This will overwrite existing images at the path | ||
string imageName = Path.GetFileName(imageFilePath); | ||
string thumbnailFilePath = Path.Combine(localSavePath, imageName.Insert(imageName.Length - 4, "_thumb")); | ||
File.WriteAllBytes(thumbnailFilePath, thumbnailImageData); | ||
Console.WriteLine("Saved the image to {0}\n", thumbnailFilePath); | ||
} | ||
else | ||
{ | ||
// Display the JSON error data. | ||
string errorString = await response.Content.ReadAsStringAsync(); | ||
Console.WriteLine("\n\nResponse:\n{0}\n", JToken.Parse(errorString).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 GetThumbnailFromUrlAsync(string imageUrl, string endpoint, string subscriptionKey, int width, int height, string localSavePath) | ||
{ | ||
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 uriBase = $"{endpoint}/vision/v2.0/generateThumbnail"; | ||
string requestParameters = $"width={width}&height={height}&smartCropping=true"; | ||
string uri = uriBase + "?" + requestParameters; | ||
Console.WriteLine(uri); | ||
|
||
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); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
Console.WriteLine("\nResponse:\n{0}", response); | ||
//Get the thumbnail image to save | ||
byte[] thumbnailImageData = await response.Content.ReadAsByteArrayAsync(); | ||
//Save the thumbnail image. This will overwrite existing images at the path | ||
string imageName = Path.GetFileName(imageUrl); | ||
string thumbnailFilePath = Path.Combine(localSavePath, imageName.Insert(imageName.Length - 4, "_thumb")); | ||
File.WriteAllBytes(thumbnailFilePath, thumbnailImageData); | ||
Console.WriteLine("Saved the thumbnail image from URL to {0}\n", thumbnailFilePath); | ||
} | ||
else | ||
{ | ||
// Display the JSON error data. | ||
string errorString = await response.Content.ReadAsStringAsync(); | ||
Console.WriteLine("\n\nResponse:\n{0}\n", JToken.Parse(errorString).ToString()); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine("\n" + e.Message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters