Beispiel #1
0
 public DemographicsAnalyzer(CamFrameAnalysis analysis,
                             IVisitorsRepository vRepo,
                             ICrowdDemographicsRepository cRepo,
                             ILogger logger)
 {
     frameAnalysis         = analysis;
     visitorRepo           = vRepo;
     crowdDemographicsRepo = cRepo;
     origin = GlobalSettings.GetKeyValue("origin");
     log    = logger;
 }
        public async Task <string> Run(
            [ServiceBusTrigger("crowd-analysis", "crowd-analyzer", Connection = "serviceBusConnection")] string request,
            ILogger log)
        {
            DateTime startTime = DateTime.UtcNow;

            log.LogInformation($"FUNC (CrowdAnalyzer): crowd-analysis topic triggered processing message: {request.Substring(0, 20)}");

            CamFrameAnalysis analysis = null;

            try
            {
                analysis = JsonConvert.DeserializeObject <CamFrameAnalysis>(request);

                // Only process if there are detected faces
                if (analysis.SimilarFaces == null && analysis.IdentifiedPersons == null)
                {
                    log.LogWarning($"FUNC (CrowdAnalyzer): starting crowd-analysis: found no faces in the analysis request");
                    return(null);
                }

                DemographicsAnalyzer demographics = new DemographicsAnalyzer(
                    analysis,
                    visitorRepo,
                    crowdDemographicsRepo,
                    identifiedVisitorRepo,
                    log);
                var isDemographicsUpdated = await demographics.UpdateDemographics();

                log.LogInformation($"FUNC (CrowdAnalyzer): finished processing with result: {JsonConvert.SerializeObject(demographics.Demographics)}");

                // If changes where made, publish changes to demographics-analysis topic
                if (isDemographicsUpdated)
                {
                    return(JsonConvert.SerializeObject(demographics.Demographics));
                }
            }
            catch (Exception e)
            {
                log.LogError($"FUNC (CrowdAnalyzer): Failed with error: {JsonConvert.SerializeObject(e.Message)}");
            }


            return(null);
        }
Beispiel #3
0
        public async Task <CamFrameAnalysis> FaceDetection(CamFrameAnalysis input, ILogger log)
        {
            log.LogInformation($"FUNC (CamFrameAnalyzer): Starting Face Detection");

            IList <FaceAttributeType> faceAttributes = new FaceAttributeType[]
            {
                FaceAttributeType.Gender, FaceAttributeType.Age,
                FaceAttributeType.Smile, FaceAttributeType.Emotion,
                FaceAttributeType.Glasses, FaceAttributeType.Hair
            };

            try
            {
                using (Stream imageFileStream = new MemoryStream(input.Data))
                {
                    // The second argument specifies to return the faceId, while
                    // the third argument specifies not to return face landmarks.
                    IList <DetectedFace> faceList =
                        await faceClient.Face.DetectWithStreamAsync(
                            imageFileStream, true, false, faceAttributes);

                    input.DetectedFaces = faceList;
                    input.IsSuccessfull = true;
                    input.Status        = $"Detected Faces: {faceList.Count}";

                    log.LogInformation($"FUNC (CamFrameAnalyzer): Finished Face Detection");

                    return(input);
                }
            }
            // Catch and display Face API errors.
            catch (APIErrorException e)
            {
                log.LogError($"####### Failed to detect faces: {e.Message}");
                input.IsSuccessfull = false;
                input.Status        = e.Message;
                return(input);
            }
        }
Beispiel #4
0
        public async Task Run(
            [ServiceBusTrigger(AppConstants.SBTopic, AppConstants.SBSubscription, Connection = "SB_Connection")] string request,
            ILogger log)
        {
            CognitiveRequest cognitiveRequest = null;

            try
            {
                cognitiveRequest = JsonConvert.DeserializeObject <CognitiveRequest>(request);
            }
            catch (Exception ex)
            {
                log.LogError($"FUNC (CamFrameAnalyzer): camframe-analysis topic triggered and failed to parse message: {JsonConvert.SerializeObject(request)} with the error: {ex.Message}");
            }

            try
            {
                key      = GlobalSettings.GetKeyValue("cognitiveKey");
                endpoint = GlobalSettings.GetKeyValue("cognitiveEndpoint");

                faceClient = new FaceClient(
                    new Microsoft.Azure.CognitiveServices.Vision.Face.ApiKeyServiceClientCredentials(key),
                    new System.Net.Http.DelegatingHandler[] { })
                {
                    Endpoint = endpoint
                };

                var data = await filesStorageRepo.GetFileAsync(cognitiveRequest.FileUrl);

                var frameAnalysis = new CamFrameAnalysis();
            }
            catch (Exception ex)
            {
                log.LogError($"FUNC (CamFrameAnalyzer): camframe-analysis topic triggered and failed to parse message: {JsonConvert.SerializeObject(cognitiveRequest)} with the error: {ex.Message}");
            }

            log.LogInformation($"FUNC (CamFrameAnalyzer): camframe-analysis topic triggered and processed message: {JsonConvert.SerializeObject(cognitiveRequest)}");
        }
        public async Task <string> Run(
            [ServiceBusTrigger(AppConstants.SBTopic, AppConstants.SBSubscription, Connection = "serviceBusConnection")] string request,
            ILogger log)
        {
            DateTime startTime = DateTime.UtcNow;

            log.LogInformation($"FUNC (CamFrameAnalyzer): camframe-analysis topic triggered processing message: {JsonConvert.SerializeObject(request)}");

            CognitiveRequest cognitiveRequest = null;

            try
            {
                cognitiveRequest = JsonConvert.DeserializeObject <CognitiveRequest>(request);
            }
            catch (Exception ex)
            {
                log.LogError($"FUNC (CamFrameAnalyzer): camframe-analysis topic triggered and failed to parse message: {JsonConvert.SerializeObject(request)} with the error: {ex.Message}");
            }

            try
            {
                key      = GlobalSettings.GetKeyValue("cognitiveKey");
                endpoint = GlobalSettings.GetKeyValue("cognitiveEndpoint");
                faceWorkspaceDataFilter = GlobalSettings.GetKeyValue("faceWorkspaceDataFilter");

                FaceServiceHelper.ApiKey                = key;
                FaceServiceHelper.ApiEndpoint           = endpoint;
                FaceListManager.FaceListsUserDataFilter = faceWorkspaceDataFilter;

                frameAnalysis = new CamFrameAnalysis
                {
                    Id           = $"{Guid.NewGuid()}-{cognitiveRequest.TakenAt.Month}{cognitiveRequest.TakenAt.Year}",
                    Request      = cognitiveRequest,
                    CreatedAt    = startTime,
                    TimeKey      = $"{cognitiveRequest.TakenAt.Month}{cognitiveRequest.TakenAt.Year}",
                    IsDeleted    = false,
                    IsSuccessful = false,
                    Origin       = "CamFrameAnalyzer",
                    Status       = ProcessingStatus.Processing.ToString()
                };

                // Get image data. We need only the filename not the FQDN in FileUrl
                var fileName = cognitiveRequest.FileUrl.Substring(cognitiveRequest.FileUrl.LastIndexOf("/") + 1);
                var data     = await filesStorageRepo.GetFileAsync(fileName);

                // Load the analyzer with data
                CognitiveFacesAnalyzer.PeopleGroupsUserDataFilter = faceWorkspaceDataFilter;
                cognitiveFacesAnalyzer = new CognitiveFacesAnalyzer(data);

                await AnalyzeCameFrame(log);

                UpdateAnalysisSummary();

                frameAnalysis.TotalProcessingTime = (int)(DateTime.UtcNow - startTime).TotalMilliseconds;

                await SaveAnalysisAsync();

                log.LogInformation($"FUNC (CamFrameAnalyzer): camframe-analysis COMPLETED: {JsonConvert.SerializeObject(frameAnalysis)}");

                //Only publish a new analysis when face detection was successful with faces
                if (frameAnalysis.IsSuccessful)
                {
                    return(JsonConvert.SerializeObject(frameAnalysis));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                log.LogError($"FUNC (CamFrameAnalyzer): camframe-analysis topic triggered and failed to parse message: {JsonConvert.SerializeObject(cognitiveRequest)} with the error: {ex.Message}");
            }

            return(null);
        }