diff --git a/src/Functions/SantaTalk.Functions/sendPicture.cs b/src/Functions/SantaTalk.Functions/sendPicture.cs new file mode 100644 index 0000000..f13e9f0 --- /dev/null +++ b/src/Functions/SantaTalk.Functions/sendPicture.cs @@ -0,0 +1,126 @@ +public static class SendPicture +{ + static string subscriptionKey = Environment.GetEnvironmentVariable("APIKey"); + static string endpoint = Environment.GetEnvironmentVariable("APIEndPoint"); + static string subscriptionKeyFace = Environment.GetEnvironmentVariable("APIKey"); + static string endpointFace = Environment.GetEnvironmentVariable("APIEndPoint"); + + [FunctionName("SendPicture")] + public static async Task Run( + [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, + ILogger log) + { + log.LogInformation("C# HTTP trigger function processed a request."); + + var filesProvider = await req.Content.ReadAsMultipartAsync(); + var fileContents = filesProvider.Contents.FirstOrDefault(); + + + byte[] payload = await fileContents.ReadAsByteArrayAsync(); + Stream stream = new MemoryStream(payload); + + faceFactory ff = new faceFactory(endpointFace, subscriptionKeyFace); + var result = ff.analyzeImageAsync(stream); + Task.WaitAll(result); + + string responseBody = JsonConvert.SerializeObject(result.Result); + return (ActionResult)new OkObjectResult(responseBody); + + } + + + +} + +public class faceFactory +{ + + const string RECOGNITION_MODEL2 = RecognitionModel.Recognition02; + const string RECOGNITION_MODEL1 = RecognitionModel.Recognition01; + private string endpoint { get; set; } + private string subscriptionKey { get; set; } + public faceFactory(string _endpoint, string _subscriptionKey) + { + endpoint = _endpoint; + subscriptionKey = _subscriptionKey; + } + + public async Task> analyzeImageAsync(Stream st) + { + IFaceClient client = Authenticate(endpoint, subscriptionKey); + + return await DetectFace(client, st, RECOGNITION_MODEL2); + + } + + protected Stream GetStream(String gazouUrl) + { + Stream rtn = null; + System.Net.HttpWebRequest aRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(gazouUrl); + System.Net.HttpWebResponse aResponse = (System.Net.HttpWebResponse)aRequest.GetResponse(); + return aResponse.GetResponseStream(); + + using (StreamReader sReader = new StreamReader(aResponse.GetResponseStream(), System.Text.Encoding.Default)) + { + rtn = sReader.BaseStream; + } + return rtn; + } + + + private static IFaceClient Authenticate(string endpoint, string key) + { + return new FaceClient(new Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; + } + + public static async Task> DetectFace(IFaceClient client, Stream image, string recognitionModel) + { + List facesI = new List(); + try + { + + + Console.WriteLine("========DETECT FACES========"); + Console.WriteLine(); + IList detectedFaces = await client.Face.DetectWithStreamAsync(image, + returnFaceAttributes: new List { FaceAttributeType.Accessories, FaceAttributeType.Age, + FaceAttributeType.Blur, FaceAttributeType.Emotion, FaceAttributeType.Exposure, FaceAttributeType.FacialHair, + FaceAttributeType.Gender, FaceAttributeType.Glasses, FaceAttributeType.Hair, FaceAttributeType.HeadPose, + FaceAttributeType.Makeup, FaceAttributeType.Noise, FaceAttributeType.Occlusion, FaceAttributeType.Smile }, + recognitionModel: recognitionModel); + + Console.WriteLine($"{detectedFaces.Count} face(s) detected from image ."); + // Parse and print all attributes of each detected face. + foreach (var face in detectedFaces) + { + Console.WriteLine($"Face attributes "); + // Get emotion on the face + string emotionType = string.Empty; + double emotionValue = 0.0; + Emotion emotion = face.FaceAttributes.Emotion; + if (emotion.Anger > emotionValue) { emotionValue = emotion.Anger; emotionType = "Anger"; } + if (emotion.Contempt > emotionValue) { emotionValue = emotion.Contempt; emotionType = "Contempt"; } + if (emotion.Disgust > emotionValue) { emotionValue = emotion.Disgust; emotionType = "Disgust"; } + if (emotion.Fear > emotionValue) { emotionValue = emotion.Fear; emotionType = "Fear"; } + if (emotion.Happiness > emotionValue) { emotionValue = emotion.Happiness; emotionType = "Happiness"; } + if (emotion.Neutral > emotionValue) { emotionValue = emotion.Neutral; emotionType = "Neutral"; } + if (emotion.Sadness > emotionValue) { emotionValue = emotion.Sadness; emotionType = "Sadness"; } + if (emotion.Surprise > emotionValue) { emotionType = "Surprise"; } + Console.WriteLine($"Emotion : {emotionType}"); + FaceInfo f = new FaceInfo(); + f.Age = face.FaceAttributes.Age; + f.emotion = emotionType; + f.Gender = face.FaceAttributes.Gender.Value.ToString(); + f.smile = face.FaceAttributes.Smile; + facesI.Add(f); + Console.WriteLine(); + } + } + catch (Exception error) + { + + + } + return facesI; + } +} diff --git a/src/Functions/SantaTalk.Models/FaceInfo.cs b/src/Functions/SantaTalk.Models/FaceInfo.cs new file mode 100644 index 0000000..5a949a9 --- /dev/null +++ b/src/Functions/SantaTalk.Models/FaceInfo.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SantaTalk.Models +{ + public class FaceInfo + { + + public double Age { get; set; } + public double smile { get; set; } + + public string Gender { get; set; } + + /// + /// Valids values ("Anger","Contempt","Disgust","Fear","Happiness","Neutral","Sadness","Surprise") + /// + public string emotion { get; set; } + } +} + diff --git a/src/SantaTalk.Android/MainActivity.cs b/src/SantaTalk.Android/MainActivity.cs index 15fa73e..d1f5ae7 100644 --- a/src/SantaTalk.Android/MainActivity.cs +++ b/src/SantaTalk.Android/MainActivity.cs @@ -6,6 +6,7 @@ using Android.Views; using Android.Widget; using Android.OS; +using Plugin.CurrentActivity; namespace SantaTalk.Droid { @@ -21,6 +22,7 @@ protected override void OnCreate(Bundle savedInstanceState) Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); + CrossCurrentActivity.Current.Init(this, savedInstanceState); FFImageLoading.Forms.Platform.CachedImageRenderer.Init(true); LoadApplication(new App()); } diff --git a/src/SantaTalk.Android/Properties/AndroidManifest.xml b/src/SantaTalk.Android/Properties/AndroidManifest.xml index ba3f8c2..eaec5c3 100644 --- a/src/SantaTalk.Android/Properties/AndroidManifest.xml +++ b/src/SantaTalk.Android/Properties/AndroidManifest.xml @@ -1,6 +1,16 @@  - - - - + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/SantaTalk.Android/Resources/Resource.designer.cs b/src/SantaTalk.Android/Resources/Resource.designer.cs index 1d944e7..d75b72e 100644 --- a/src/SantaTalk.Android/Resources/Resource.designer.cs +++ b/src/SantaTalk.Android/Resources/Resource.designer.cs @@ -1,11 +1,11 @@ #pragma warning disable 1591 //------------------------------------------------------------------------------ // -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 +// Este código fue generado por una herramienta. +// Versión de runtime:4.0.30319.42000 // -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. +// Los cambios en este archivo podrían causar un comportamiento incorrecto y se perderán si +// se vuelve a generar el código. // //------------------------------------------------------------------------------ @@ -11424,10 +11424,13 @@ public partial class Xml { // aapt resource value: 0x7F100000 - public const int network_security_config = 2131755008; + public const int file_paths = 2131755008; // aapt resource value: 0x7F100001 - public const int xamarin_essentials_fileprovider_file_paths = 2131755009; + public const int network_security_config = 2131755009; + + // aapt resource value: 0x7F100002 + public const int xamarin_essentials_fileprovider_file_paths = 2131755010; static Xml() { diff --git a/src/SantaTalk.Android/Resources/xml/file_paths.xml b/src/SantaTalk.Android/Resources/xml/file_paths.xml new file mode 100644 index 0000000..24e75b1 --- /dev/null +++ b/src/SantaTalk.Android/Resources/xml/file_paths.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/SantaTalk.Android/SantaTalk.Android.csproj b/src/SantaTalk.Android/SantaTalk.Android.csproj index ffcd81d..731295c 100644 --- a/src/SantaTalk.Android/SantaTalk.Android.csproj +++ b/src/SantaTalk.Android/SantaTalk.Android.csproj @@ -1,4 +1,4 @@ - + Debug @@ -31,6 +31,10 @@ prompt 4 None + false + false + false + false true @@ -52,6 +56,9 @@ + + 2.4.11.982 + @@ -98,43 +105,50 @@ - - + + + + - - + + + + - - + + + + - - + + + + - - + + + + - - + + + + - - + + + + - - - - - - - @@ -148,5 +162,10 @@ - - + + + Designer + + + + \ No newline at end of file diff --git a/src/SantaTalk.iOS/SantaTalk.iOS.csproj b/src/SantaTalk.iOS/SantaTalk.iOS.csproj index 19ef860..26045f7 100644 --- a/src/SantaTalk.iOS/SantaTalk.iOS.csproj +++ b/src/SantaTalk.iOS/SantaTalk.iOS.csproj @@ -122,6 +122,9 @@ + + 2.4.11.982 + diff --git a/src/SantaTalk.sln b/src/SantaTalk.sln index bb1864b..6fc40dc 100644 --- a/src/SantaTalk.sln +++ b/src/SantaTalk.sln @@ -1,71 +1,79 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SantaTalk", "SantaTalk\SantaTalk.csproj", "{091675BF-545D-4B21-A77B-F24ABDEAF6AA}" +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29418.71 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SantaTalk", "SantaTalk\SantaTalk.csproj", "{091675BF-545D-4B21-A77B-F24ABDEAF6AA}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SantaTalk.Android", "SantaTalk.Android\SantaTalk.Android.csproj", "{9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SantaTalk.Models", "Functions\SantaTalk.Models\SantaTalk.Models.csproj", "{07CBBC6F-2C49-44FD-B479-90907EC442BA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SantaTalk.Models", "Functions\SantaTalk.Models\SantaTalk.Models.csproj", "{07CBBC6F-2C49-44FD-B479-90907EC442BA}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SantaTalk.iOS", "SantaTalk.iOS\SantaTalk.iOS.csproj", "{1A8F4AAD-CF42-44DA-8289-1E100047AE1D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - Debug|iPhoneSimulator = Debug|iPhoneSimulator - Release|iPhoneSimulator = Release|iPhoneSimulator Debug|iPhone = Debug|iPhone + Debug|iPhoneSimulator = Debug|iPhoneSimulator + Release|Any CPU = Release|Any CPU Release|iPhone = Release|iPhone + Release|iPhoneSimulator = Release|iPhoneSimulator EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Debug|Any CPU.Build.0 = Debug|Any CPU - {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|Any CPU.ActiveCfg = Release|Any CPU - {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|Any CPU.Build.0 = Release|Any CPU - {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU - {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU - {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|iPhoneSimulator.Build.0 = Release|Any CPU {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Debug|iPhone.ActiveCfg = Debug|Any CPU {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Debug|iPhone.Build.0 = Debug|Any CPU + {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU + {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|Any CPU.Build.0 = Release|Any CPU {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|iPhone.ActiveCfg = Release|Any CPU {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|iPhone.Build.0 = Release|Any CPU + {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {091675BF-545D-4B21-A77B-F24ABDEAF6AA}.Release|iPhoneSimulator.Build.0 = Release|Any CPU {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|Any CPU.Build.0 = Release|Any CPU - {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU - {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU - {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|iPhoneSimulator.Build.0 = Release|Any CPU + {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Debug|Any CPU.Deploy.0 = Debug|Any CPU {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Debug|iPhone.ActiveCfg = Debug|Any CPU {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Debug|iPhone.Build.0 = Debug|Any CPU + {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU + {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|Any CPU.Build.0 = Release|Any CPU {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|iPhone.ActiveCfg = Release|Any CPU {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|iPhone.Build.0 = Release|Any CPU + {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {9F3CF5B6-BC95-42B0-97A9-6024681F1FC0}.Release|iPhoneSimulator.Build.0 = Release|Any CPU {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Debug|Any CPU.Build.0 = Debug|Any CPU - {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|Any CPU.ActiveCfg = Release|Any CPU - {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|Any CPU.Build.0 = Release|Any CPU - {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU - {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU - {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|iPhoneSimulator.Build.0 = Release|Any CPU {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Debug|iPhone.ActiveCfg = Debug|Any CPU {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Debug|iPhone.Build.0 = Debug|Any CPU + {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU + {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|Any CPU.Build.0 = Release|Any CPU {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|iPhone.ActiveCfg = Release|Any CPU {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|iPhone.Build.0 = Release|Any CPU + {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {07CBBC6F-2C49-44FD-B479-90907EC442BA}.Release|iPhoneSimulator.Build.0 = Release|Any CPU {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Debug|Any CPU.ActiveCfg = Debug|iPhoneSimulator {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Debug|Any CPU.Build.0 = Debug|iPhoneSimulator - {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|Any CPU.ActiveCfg = Release|iPhoneSimulator - {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|Any CPU.Build.0 = Release|iPhoneSimulator - {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator - {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator - {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator - {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Debug|iPhone.ActiveCfg = Debug|iPhone {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Debug|iPhone.Build.0 = Debug|iPhone + {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator + {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator + {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|Any CPU.ActiveCfg = Release|iPhoneSimulator + {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|Any CPU.Build.0 = Release|iPhoneSimulator {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|iPhone.ActiveCfg = Release|iPhone {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|iPhone.Build.0 = Release|iPhone + {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator + {1A8F4AAD-CF42-44DA-8289-1E100047AE1D}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {644603D6-029B-4BE4-949F-FAF395AD1D79} EndGlobalSection EndGlobal diff --git a/src/SantaTalk/ResultsPage.xaml b/src/SantaTalk/ResultsPage.xaml index a31892f..a5d59a0 100644 --- a/src/SantaTalk/ResultsPage.xaml +++ b/src/SantaTalk/ResultsPage.xaml @@ -4,7 +4,9 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" xmlns:controls="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView" - xmlns:state="clr-namespace:Xamarin.Forms.StateSquid;assembly=Xamarin.Forms.StateSquid" + xmlns:state="clr-namespace:Xamarin.Forms.StateSquid;assembly=Xamarin.Forms.StateSquid" + xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms" + xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations" x:Class="SantaTalk.ResultsPage" ios:Page.UseSafeArea="True" BackgroundColor="{StaticResource dark_gradient}" @@ -25,7 +27,6 @@ BackgroundGradientEndColor="{StaticResource light_gradient}"> - @@ -42,6 +43,30 @@ + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/src/SantaTalk/ResultsPage.xaml.cs b/src/SantaTalk/ResultsPage.xaml.cs index 26c040a..f60857a 100644 --- a/src/SantaTalk/ResultsPage.xaml.cs +++ b/src/SantaTalk/ResultsPage.xaml.cs @@ -31,6 +31,7 @@ public ResultsPage(string kidsName, string letterText) _formsWidth = Convert.ToInt32(DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density); _formsHeight = Convert.ToInt32(DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density); + vm.preparePictureSantaAsync(); } protected override async void OnAppearing() diff --git a/src/SantaTalk/SantaTalk.csproj b/src/SantaTalk/SantaTalk.csproj index 7ad5715..b9dbf0c 100644 --- a/src/SantaTalk/SantaTalk.csproj +++ b/src/SantaTalk/SantaTalk.csproj @@ -11,6 +11,8 @@ + + diff --git a/src/SantaTalk/Services/LetterDeliveryService.cs b/src/SantaTalk/Services/LetterDeliveryService.cs index 9a55d77..31ce567 100644 --- a/src/SantaTalk/Services/LetterDeliveryService.cs +++ b/src/SantaTalk/Services/LetterDeliveryService.cs @@ -1,7 +1,11 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Net; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json; +using Plugin.Media.Abstractions; using SantaTalk.Models; using Xamarin.Essentials; @@ -12,6 +16,7 @@ public class LetterDeliveryService //string santaUrl = "{REPLACE WITH YOUR FUNCTION URL}/api/WriteSanta"; string santaUrl = "http://localhost:7071/api/WriteSanta"; + string santaPictureUrl = "http://localhost:7071/api/SendPicture"; static HttpClient httpClient = new HttpClient(); public async Task WriteLetterToSanta(SantaLetter letter) @@ -42,5 +47,33 @@ public async Task WriteLetterToSanta(SantaLetter letter) return results; } + public async Task> sendPictureToSanta(MediaFile f) + { + + using (HttpClient client = new HttpClient()) + { + var request = new HttpRequestMessage(HttpMethod.Post, santaPictureUrl); + + var content = new MultipartFormDataContent(); + + byte[] byteArray = File.ReadAllBytes(f.Path); + + var webClient = new WebClient(); + + content.Add(new ByteArrayContent(byteArray), "file", "file.jpg"); + + request.Content = content; + + var response = await client.SendAsync(request).ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + List fileInfo = JsonConvert.DeserializeObject>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); + + return fileInfo; + } + + + } } } diff --git a/src/SantaTalk/Services/SantasCommentsService.cs b/src/SantaTalk/Services/SantasCommentsService.cs index fb51e1a..053938e 100644 --- a/src/SantaTalk/Services/SantasCommentsService.cs +++ b/src/SantaTalk/Services/SantasCommentsService.cs @@ -5,26 +5,26 @@ namespace SantaTalk { public class SantasCommentsService { - public SantaResultDisplay MakeGiftDecision(SantaResults results) + public SantaResultDisplay MakeGiftDecision(SantaResults results, FaceInfo resultface) { // Based on the results - have Santa make some comments on the kids behavior throughout the year // and then decide on whether to give them a gift or not. SantaResultDisplay comments = new SantaResultDisplay(); - if (results.SentimentScore < .3) + if (results.SentimentScore < .3 || resultface.emotion.Equals("Anger", StringComparison.OrdinalIgnoreCase) || resultface.emotion.Equals("Contempt", StringComparison.OrdinalIgnoreCase)) { // very bad behavior comments.SentimentInterpretation = "Seriously though, why did you act like that this year? Don't you know I'm always watching? Always. Watching."; comments.GiftPrediction = "You'll get nothing and you'll like it."; } - else if (results.SentimentScore >= .3 && results.SentimentScore < .66) + else if ((results.SentimentScore >= .3 && results.SentimentScore < .66) || resultface.emotion.Equals("Sadness", StringComparison.OrdinalIgnoreCase)) { // bad saide of average comments.SentimentInterpretation = "You were kind of a good kid this year. You should have probably been better. I get it though, probably your brother's fault."; comments.GiftPrediction = "If you put out enough cookies, I might leave you something."; } - else if (results.SentimentScore >= .66 && results.SentimentScore < .95) + else if ((results.SentimentScore >= .66 && results.SentimentScore < .95) || resultface.emotion.Equals("Happinness", StringComparison.OrdinalIgnoreCase) || resultface.smile == 1) { // good side of average comments.SentimentInterpretation = "Nice work there kid. You were a good kid all year long. Santa for sure is stopping at your house!"; diff --git a/src/SantaTalk/ViewModels/ResultsPageViewModel.cs b/src/SantaTalk/ViewModels/ResultsPageViewModel.cs index 696a611..847b7c1 100644 --- a/src/SantaTalk/ViewModels/ResultsPageViewModel.cs +++ b/src/SantaTalk/ViewModels/ResultsPageViewModel.cs @@ -1,6 +1,8 @@ using System; using System.Threading.Tasks; using MvvmHelpers; +using Plugin.Media; +using Plugin.Media.Abstractions; using SantaTalk.Models; using Xamarin.Forms.StateSquid; @@ -8,6 +10,8 @@ namespace SantaTalk { public class ResultsPageViewModel : BaseViewModel { + private SantaResults resultsLetter; + private FaceInfo resultsFace; string kidsName; public string KidsName { @@ -50,6 +54,32 @@ public string GiftDecision set => SetProperty(ref giftDecision, value); } + string faceImgSource; + public string FaceImgSource + { + get => faceImgSource; + set => SetProperty(ref faceImgSource, value); + } + double faceAge; + public double FaceAge + { + get => faceAge; + set => SetProperty(ref faceAge, value); + } + string faceGender; + public string FaceGender + { + get => faceGender; + set => SetProperty(ref faceGender, value); + } + bool faceNaughty; + public bool FaceNaughty + { + get => faceNaughty; + set => SetProperty(ref faceNaughty, value); + } + + public async Task SendLetterToSanta() { CurrentState = State.Loading; @@ -61,22 +91,67 @@ public async Task SendLetterToSanta() }; var letterService = new LetterDeliveryService(); - var results = await letterService.WriteLetterToSanta(letter); + resultsLetter = await letterService.WriteLetterToSanta(letter); - if (results.SentimentScore == -1) + if (resultsLetter.SentimentScore == -1) { - CurrentState = State.Error; + resultsLetter.SentimentScore = 1; + // CurrentState = State.Error; return; } + + + + + } + + public async Task preparePictureSantaAsync() + { + CurrentState = State.Loading; + + var pathToNewFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); + + + var fold = System.IO.Directory.CreateDirectory(pathToNewFolder + "/testSanta"); + + + var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions + { + Directory = "testSanta", + SaveToAlbum = true, + CompressionQuality = 75, + CustomPhotoSize = 50, + PhotoSize = PhotoSize.MaxWidthHeight, + MaxWidthHeight = 2000, + DefaultCamera = CameraDevice.Front + + }); + var letterService = new LetterDeliveryService(); + var result = await letterService.sendPictureToSanta(file); + if (result.Count > 0) + { + //prepare to multiple faces + resultsFace = result[0]; + } + FaceImgSource = file.Path; + FaceGender = resultsFace.Gender; + FaceAge = resultsFace.Age; + FaceNaughty = resultsFace.smile == 1 ? false : true; + prepareResult(); + CurrentState = State.Success; + } + + private void prepareResult() + { var commentsService = new SantasCommentsService(); - var comments = commentsService.MakeGiftDecision(results); + var comments = commentsService.MakeGiftDecision(resultsLetter, resultsFace); SantasComment = comments.SentimentInterpretation; GiftDecision = comments.GiftPrediction; - DetectedLanguage = results.DetectedLanguage; - + DetectedLanguage = resultsLetter.DetectedLanguage; CurrentState = State.Success; } + } }