Created
May 19, 2018 14:48
-
-
Save icebeam7/2c81266f0f5d7ce68803d44d034d88a8 to your computer and use it in GitHub Desktop.
FaceBot: ServicioFace.cs
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.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.ProjectOxford.Face; | |
using Microsoft.ProjectOxford.Face.Contract; | |
using FaceBot.Helpers; | |
using FaceBot.Modelos; | |
namespace FaceBot.Servicios | |
{ | |
public static class ServicioFace | |
{ | |
public static async Task<Emocion> ObtenerEmocion(Stream foto) | |
{ | |
Emocion emocion = null; | |
try | |
{ | |
if (foto != null) | |
{ | |
var clienteFace = new FaceServiceClient(Constantes.FaceApiKey, Constantes.FaceApiURL); | |
var atributosFace = new FaceAttributeType[] { FaceAttributeType.Emotion, FaceAttributeType.Age }; | |
using (var stream = foto) | |
{ | |
Face[] rostros = await clienteFace.DetectAsync(stream, false, false, atributosFace); | |
if (rostros.Any()) | |
{ | |
var analisisEmocion = rostros.FirstOrDefault().FaceAttributes.Emotion.ToRankedList().FirstOrDefault(); | |
emocion = new Emocion() | |
{ | |
Nombre = analisisEmocion.Key, | |
Score = analisisEmocion.Value | |
}; | |
} | |
foto.Dispose(); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
} | |
return emocion; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment