Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Created May 19, 2018 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/2c81266f0f5d7ce68803d44d034d88a8 to your computer and use it in GitHub Desktop.
Save icebeam7/2c81266f0f5d7ce68803d44d034d88a8 to your computer and use it in GitHub Desktop.
FaceBot: ServicioFace.cs
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