Exemple #1
0
        public async Task <IActionResult> Detect([FromBody] ImagePost image)
        {
            byte[] bytes = Convert.FromBase64String(image.ImageData);
            IList <DetectedFace> detectedFaces;

            using (MemoryStream stream = new MemoryStream(bytes))
            {
                HttpOperationResponse <IList <DetectedFace> > response =
                    await _altClient.Face.DetectWithStreamWithHttpMessagesAsync(stream);

                detectedFaces = response.Body;
            }
            if (detectedFaces != null && detectedFaces.Count > 0)
            {
                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    HttpOperationResponse <IList <DetectedFace> > response =
                        await _client.Face.DetectWithStreamWithHttpMessagesAsync(stream);

                    detectedFaces = response.Body;
                }
                List <Guid> guids = detectedFaces
                                    .Where(d => d.FaceId.HasValue)
                                    .Select(d => d.FaceId.Value)
                                    .ToList();
                HttpOperationResponse <IList <IdentifyResult> > identifyResult = await _client.Face.IdentifyWithHttpMessagesAsync("1", guids);

                List <DetectedPerson> foundPersons = identifyResult.Body
                                                     .Select(r => GetPersonFromResult(r, GetPersons().ToList()))
                                                     .Where(dp => dp != null)
                                                     .ToList();
                string   url;
                DateTime dateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId
                                        (DateTime.UtcNow, "Mountain Standard Time");

                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    url = await _imageManager.UploadImageAsync(stream, image.DeviceName + dateTime.ToString("yyMMddHHmmss") + ".png");
                }

                DetectionResults results = new DetectionResults
                {
                    Persons    = foundPersons,
                    Url        = url,
                    DeviceName = image.DeviceName,
                    DateTime   = dateTime.ToString("G")
                };
                string email           = HttpContext.Session.GetString("Email");
                string serializeObject = JsonConvert.SerializeObject(results);
                if (email != null)
                {
                    await EmailSender.SendEmailsAsync(email, "Mobot Alert", serializeObject);
                }
                IotMessageSender.SendDeviceToCloudMessagesAsync(image.DeviceName, serializeObject);
                return(Ok(results.ToString()));
            }
            return(Ok("No face detected!"));
        }