Created
April 19, 2019 10:48
-
-
Save NeelBhatt/3cf62224c0d37aa5ce010783f65dbab3 to your computer and use it in GitHub Desktop.
FaceClientCognitiveService
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
public class FaceClientCognitiveService | |
{ | |
private readonly IFaceClient faceClient; | |
private readonly float scoreLimit = 0.5f; | |
private readonly ILogger<FaceClientCognitiveService> _logger; | |
public FaceClientCognitiveService(IConfiguration config, ILogger<FaceClientCognitiveService> logger) | |
{ | |
_logger = logger; | |
faceClient = new FaceClient(new ApiKeyServiceClientCredentials(config["SubscriptionKey"]), | |
new System.Net.Http.DelegatingHandler[] { }); | |
faceClient.Endpoint = config["FaceEndPointURL"]; | |
} | |
public async Task<bool> IsCryingDetected(Stream stream) | |
{ | |
IList<FaceAttributeType> faceAttributes = new FaceAttributeType[] | |
{ | |
FaceAttributeType.Emotion | |
}; | |
// Call the Face API. | |
try | |
{ | |
IList<DetectedFace> faceList = await faceClient.Face.DetectWithStreamAsync(stream, false, false, faceAttributes); | |
if (faceList.Count > 0) | |
{ | |
var face = faceList[0]; | |
if (face.FaceAttributes.Emotion.Sadness >= scoreLimit || | |
face.FaceAttributes.Emotion.Anger >= scoreLimit || | |
face.FaceAttributes.Emotion.Fear >= scoreLimit) | |
{ | |
_logger.LogInformation($"Crying Detected with the score of {face.FaceAttributes.Emotion.Sadness}"); | |
return true; | |
} | |
else | |
{ | |
_logger.LogInformation($"Crying Not Detected with the score of {face.FaceAttributes.Emotion.Sadness}"); | |
} | |
} | |
else | |
{ | |
_logger.LogInformation("No Face Detected"); | |
} | |
} | |
catch (Exception e) | |
{ | |
_logger.LogError(e.Message); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment