Ejemplo n.º 1
0
        public static async Task <IActionResult> IdentifyAudience([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    data        = JsonConvert.DeserializeObject <AudienceRequest>(requestBody);
            var    response    = new AudienceResponse();

            try
            {
                // Decode data from Base64
                byte[] imageBytes = Convert.FromBase64String(data.Image);

                // Process the image
                using (Stream stream = new MemoryStream(imageBytes))
                {
                    var members = await FaceService.DetectMembers(stream, DateTime.Now);

                    if (members.Count > 0)
                    {
                        // Storage the results
                        var token    = new System.Threading.CancellationToken();
                        var location = !string.IsNullOrEmpty(data.Location) ? data.Location : DefaultLocationId;
                        await storage.WriteAsync(location, members, token);

                        response.Audience   = MemberUtils.CreateAudience(members);
                        response.StatusCode = (int)HttpStatusCode.OK;

                        return(new OkObjectResult(response));
                    }
                }
            }
            catch (Exception ex)
            {
                response.Message    = ex.Message;
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(new BadRequestObjectResult(response));
            }

            response.Message    = "Unable to detect faces.";
            response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(new BadRequestObjectResult(response));
        }
Ejemplo n.º 2
0
        public static async Task <IActionResult> AudienceHistory([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    data        = JsonConvert.DeserializeObject <AudienceHistoryRequest>(requestBody);
            var    response    = new AudienceResponse();

            try
            {
                var endDate   = (data.EndDate != default(DateTime)) ? data.EndDate : DateTime.Now;
                var startDate = (data.StartDate != default(DateTime)) ? data.StartDate : endDate.Subtract(TimeSpan.FromMinutes(data.Minutes));
                var token     = new System.Threading.CancellationToken();
                var locations = data.Locations;

                List <Member> collection;
                if (data.UniqueMembersOnly)
                {
                    collection = await storage.UniqueMembersTimeSpan(startDate, endDate, locations, token);
                }

                else
                {
                    collection = await storage.QueryTimeSpan(startDate, endDate, locations, token);
                }

                response.Audience   = MemberUtils.CreateAudience(collection);
                response.StatusCode = (int)HttpStatusCode.OK;

                return(new OkObjectResult(response));
            }
            catch (Exception ex)
            {
                response.Message    = ex.Message;
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(new BadRequestObjectResult(response));
            }
        }