Last active
July 15, 2019 14:24
-
-
Save mouadcherkaoui/16f469ada20551d724541478df8741b7 to your computer and use it in GitHub Desktop.
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
private const string subscriptionKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //your-subscription-key; | |
private const string _endpoint = | |
"https://westus.api.cognitive.microsoft.com/face/v1.0/detect"; | |
private const string localImagePath = @"Images/image.jpg"; | |
private const string remoteImageUrl = | |
"http://images5.fanpop.com/image/photos/26900000/Nicolas-Cage-nicolas-cage-26969804-2069-2560.jpg"; | |
static async Task Main(string[] args) | |
{ | |
var defaultHeaders = new Dictionary<string, string>() | |
{ | |
{ "Ocp-Apim-Subscription-Key", "1e599ebabffd4def8de00251119941c4" }, | |
}; | |
var personGroupList = (await (new HttpRequestMessage() | |
.WithEndpointUri("https://westus.api.cognitive.microsoft.com/face/v1.0/persongroups/test00") | |
.WithHeaders(defaultHeaders) | |
.WithMethod("GET") | |
.WithParameters(new Dictionary<string, string>().with("Start", "")) | |
.ProcessRequestAsync(new { }))); | |
var tokenResult = (await (new HttpRequestMessage() | |
.WithEndpointUri("https://westus.api.cognitive.microsoft.com/sts/v1.0/issuetoken") | |
.WithHeaders(defaultHeaders | |
.with("Ocp-Apim-Subscription-Region", "westus") | |
).WithMethod("POST")) | |
.ProcessRequestAsync<object>(new { })) | |
.PipeTo(r => { | |
var json = r.Content.ReadAsStringAsync().Result; | |
return json; | |
//return JsonConvert.DeserializeObject(json); | |
}); | |
var textToSpeechResult = (await new HttpRequestMessage() | |
.WithEndpointUri("https://westus.tts.speech.microsoft.com/cognitiveservices/v1") | |
.WithHeaders(defaultHeaders | |
.with("Authorization", $"Bearer {tokenResult.ToString()}") | |
.with("X-Microsoft-OutputFormat", "riff-24khz-16bit-mono-pcm") | |
.with("User-Agent", "mslearn")) | |
.WithMethod("POST") | |
.ProcessSSMLRequestAsync(@"<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Female' | |
name='en-US-Jessa'>Microsoft Speech Service Text - to - Speech API</voice></speak>", new Dictionary<string, string>().with("Content-Type", "application/ssml+xml") | |
)) | |
.PipeTo(r => { | |
var result = r.Content.ReadAsByteArrayAsync().Result; | |
var stream = System.IO.File.OpenWrite($"{DateTime.UtcNow.ToFileTime()}.wav"); | |
stream.Write(result); | |
return stream; | |
}); | |
var detectFaceResult = await new HttpRequestMessage() | |
.WithEndpointUri(_endpoint) | |
.WithMethod("POST") | |
.WithHeaders( | |
new Dictionary<string, string>() | |
.with("Ocp-Apim-Subscription-Key", "1e599ebabffd4def8de00251119941c4") | |
.with("Ocp-Apim-Subscription-Region", "westus")) | |
.WithParameters(new Dictionary<string, string>()) | |
.ProcessRequestAsync<object>(new { url = remoteImageUrl}); |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace Extensions | |
{ | |
public static class DictionaryExtensions | |
{ | |
public static Dictionary<TKey, TValue> ExtendWith<TKey, TValue>(this Dictionary<TKey, TValue> source, KeyValuePair<TKey, TValue> keyValuePair) | |
{ | |
return source.Append(keyValuePair).ToDictionary(kv => kv.Key, kv => kv.Value); | |
} | |
public static Dictionary<TKey, TValue> ExtendWith<TKey, TValue>(this Dictionary<TKey, TValue> source, Dictionary<TKey, TValue> keyValuesDict) | |
{ | |
return source.Concat(keyValuesDict).ToDictionary(kv => kv.Key, kv => kv.Value); | |
} | |
public static Dictionary<TKey, TValue> with<TKey, TValue>(this Dictionary<TKey, TValue> keyValueDict, TKey key, TValue value) | |
{ | |
return keyValueDict.ExtendWith(new KeyValuePair<TKey, TValue>(key, value)); | |
} | |
public static Dictionary<TKey, TValue> InitializeIfNull<TKey, TValue>(this Dictionary<TKey, TValue> source) | |
{ | |
return (source = source ?? new Dictionary<TKey, TValue>()); | |
} | |
public static Dictionary<TKey, TValue> AddOrReplaceKeyValuePair<TKey, TValue>(this Dictionary<TKey, TValue> source, | |
TKey key, TValue value) | |
{ | |
if (source.ContainsKey(key)) | |
source[key] = value; | |
else | |
{ | |
source.Add(key, value); | |
} | |
return source; | |
} | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Net.Http; | |
using System.Text; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using CognitiveAIApis.Infrastructure.Helpers; | |
namespace Extensions | |
{ | |
public static class HttpRequestMessageExtensions | |
{ | |
public static HttpRequestMessage WithHeaders(this HttpRequestMessage source, Dictionary<string, string> headers) { | |
headers.ToList().ForEach(h => source.Headers.Add(h.Key, h.Value)); | |
return source; | |
} | |
public static HttpRequestMessage WithEndpointUri(this HttpRequestMessage source, string endpointUri) | |
{ | |
source.RequestUri = new Uri(endpointUri); | |
return source; | |
} | |
public static HttpRequestMessage WithParameters(this HttpRequestMessage source, Dictionary<string, string> parameters) | |
{ | |
var queryString = parameters.Aggregate("", (i, kv) => $"{i}{kv.Key}={kv.Value}&"); | |
queryString = String.IsNullOrEmpty(queryString) ? "" : queryString.Substring(0, queryString.LastIndexOf("&")); | |
source.RequestUri = new Uri($"{source.RequestUri.AbsoluteUri}?{queryString}"); | |
return source; | |
} | |
public static HttpRequestMessage WithMethod(this HttpRequestMessage source, string method) | |
{ | |
source.Method = new HttpMethod(method); | |
return source; | |
} | |
public static async Task<HttpResponseMessage> ProcessRequestAsync<TRequest>(this HttpRequestMessage source, | |
TRequest request = null, | |
Dictionary<string, string> headers = null) | |
where TRequest: class | |
{ | |
using (var client = new HttpClient()) | |
{ | |
if(request != null) | |
{ | |
var byteArray = JsonConvert.SerializeObject(request) | |
.PipeTo(serializedPayload => Encoding.UTF8.GetBytes(serializedPayload)) | |
.PipeTo(bytes => new ByteArrayContent(bytes)); | |
source.Content = byteArray; | |
} | |
if(headers != null) | |
{ | |
foreach (var item in headers) | |
{ | |
source.Content.Headers.Add(item.Key, item.Value); | |
} | |
} | |
return await client.SendAsync(source); | |
} | |
} | |
public static async Task<HttpResponseMessage> ProcessSSMLRequestAsync(this HttpRequestMessage source, string request, Dictionary<string, string> headers = null) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
var byteArray = Encoding.UTF8.GetBytes(request) | |
.PipeTo(bytes => new ByteArrayContent(bytes)); | |
source.Content = byteArray; | |
if (headers != null) | |
{ | |
foreach (var item in headers) | |
{ | |
source.Content.Headers.Add(item.Key, item.Value); | |
} | |
} | |
return await client.SendAsync(source); | |
} | |
} | |
public static async Task<TResult> GetDeserializedContentAsync<TResult>(this HttpResponseMessage source) | |
{ | |
var resultToReturn = (await source.Content.ReadAsStringAsync()) | |
.Try(stringContent => | |
JsonConvert.DeserializeObject<TResult>(stringContent)); | |
return resultToReturn; | |
} | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace Extensions | |
{ | |
public static class ObjectExtensions | |
{ | |
public static TResult PipeTo<TSource, TResult>(this TSource source, | |
Func<TSource, TResult> destination) => destination.Invoke(source); | |
public static TResult Try<TSource, TException, TResult>(this TSource source, | |
Func<TSource, TResult> destination, Action<TException> exceptionCallback = null) where TException : Exception | |
{ | |
try | |
{ | |
return destination.Invoke(source); | |
} | |
catch (TException ex) | |
{ | |
exceptionCallback?.Invoke(ex); | |
throw ex; | |
} | |
} | |
public static TResult Try<TSource, TResult>(this TSource source, | |
Func<TSource, TResult> destination, Action<AggregateException> exceptionCallback = null) | |
{ | |
try | |
{ | |
return destination.Invoke(source); | |
} | |
catch (AggregateException ex) | |
{ | |
exceptionCallback?.Invoke(ex); | |
throw ex; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment