예제 #1
0
        public static async Task <string> AnalyzePhoto(TraceWriter log, PhotoToAnalyze photoToAnalyze, ImageAnalysisTableAdapter imageAnalysisTableAdapter)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                VisionApiResponse visionApiResponse = await GetGoogleVisionApi(log, photoToAnalyze, httpClient);

                List <Face> msFaces = await GetMsFaces(log, photoToAnalyze, httpClient);

                Analysis msAnalysis = await GetMsAnalysis(log, photoToAnalyze, httpClient);

                if (visionApiResponse?.Responses.Count == 1 && msAnalysis != null)
                {
                    ImageAnalysis       canonicalImageAnalysis = new ImageAnalysis(visionApiResponse.Responses[0], msAnalysis, msFaces);
                    ImageAnalysisEntity imageAnalysisEntity    = new ImageAnalysisEntity
                    {
                        // for canonical truncate decimal precision to 4 decimal places, for others keep original precision
                        CanonicalJson             = JsonConvert.SerializeObject(canonicalImageAnalysis, JsonUtils.AnalysisSerializerSettings),
                        GoogleVisionApiJson       = JsonConvert.SerializeObject(visionApiResponse.Responses[0], JsonUtils.JsonSerializerSettings),
                        MsCognitiveFaceDetectJson = JsonConvert.SerializeObject(msFaces, JsonUtils.JsonSerializerSettings),
                        MsAnalysisJson            = JsonConvert.SerializeObject(msAnalysis, JsonUtils.JsonSerializerSettings)
                    };

                    if (imageAnalysisEntity.GoogleVisionApiJson.Length > 30000)
                    {
                        log.Warning($"Google vision API response JSON is {imageAnalysisEntity.GoogleVisionApiJson.Length} chars, removing TextAnnotations");
                        visionApiResponse.Responses[0].TextAnnotations = null;
                        imageAnalysisEntity.GoogleVisionApiJson        = JsonConvert.SerializeObject(visionApiResponse.Responses[0], JsonUtils.JsonSerializerSettings);
                    }

                    if (imageAnalysisEntity.GoogleVisionApiJson.Length > 45000)
                    {
                        log.Warning($"GoogleVisionApiJson still is {imageAnalysisEntity.GoogleVisionApiJson.Length} chars after removing TextAnnotations");
                    }

                    if (imageAnalysisEntity.CanonicalJson.Length > 45000)
                    {
                        log.Warning($"CanonicalJson is {imageAnalysisEntity.CanonicalJson.Length} chars");
                    }
                    if (imageAnalysisEntity.MsCognitiveFaceDetectJson.Length > 45000)
                    {
                        log.Warning($"MsCognitiveFaceDetectJson is {imageAnalysisEntity.MsCognitiveFaceDetectJson.Length} chars");
                    }
                    if (imageAnalysisEntity.MsAnalysisJson.Length > 45000)
                    {
                        log.Warning($"MsAnalysisJson is {imageAnalysisEntity.MsAnalysisJson.Length} chars");
                    }

                    imageAnalysisTableAdapter.InsertImageAnalysis(imageAnalysisEntity, photoToAnalyze.Url);
                    imageAnalysisTableAdapter.InsertBlogImageAnalysis(SanityHelper.SanitizeSourceBlog(photoToAnalyze.Blog), photoToAnalyze.Url);
                    log.Info($"All analyses for {photoToAnalyze.Url} saved in {stopwatch.ElapsedMilliseconds}ms");
                    return(null);
                }

                log.Warning("Failed to get all responses");
                return("Failed to get all responses");
            }
        }
예제 #2
0
        public void UpdateImageAnalysis(ImageAnalysisEntity imageAnalysisEntity)
        {
            if (imageAnalysisEntity.PartitionKey == null || imageAnalysisEntity.RowKey == null)
            {
                throw new InvalidOperationException("Can only update entities with valid PartitionKey and RowKey");
            }

            TableOperation mergeOperation = TableOperation.Merge(imageAnalysisEntity);

            imageAnalysisTable.Execute(mergeOperation);
        }
예제 #3
0
        public ImageAnalysisEntity GetImageAnalysis(string photoUrl)
        {
            string         encodedUrl        = WebUtility.UrlEncode(photoUrl);
            TableOperation retrieveOperation = TableOperation.Retrieve <ImageAnalysisEntity>(encodedUrl, "analysis");
            TableResult    result            = imageAnalysisTable.Execute(retrieveOperation);

            if (result.HttpStatusCode == 200)
            {
                ImageAnalysisEntity entity = (ImageAnalysisEntity)result.Result;
                return(entity);
            }

            return(null);
        }
예제 #4
0
        public void InsertImageAnalysis(ImageAnalysisEntity imageAnalysisEntity, string photoUrl)
        {
            imageAnalysisEntity.PartitionKey = WebUtility.UrlEncode(photoUrl);
            imageAnalysisEntity.RowKey       = "analysis";
            TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(imageAnalysisEntity);

            try
            {
                imageAnalysisTable.Execute(insertOrMergeOperation);
            }
            catch (Exception ex)
            {
                throw new DataException($"InsertImageAnalysis failed with URL {photoUrl}", ex);
            }
        }