Beispiel #1
0
        internal void Classify(ClassifyTaskModel[] tasks)
        {
            if (tasks == null || tasks.Length == 0)
            {
                this.log.LogWarning($"No images to process");
                return;
            }
            var batches = tasks.Batch(MAX_BATCH_SIZE);

            if (batches == null)
            {
                this.log.LogError($"Batch splitting error");
                return;
            }
            foreach (var batch in batches)
            {
                // batch now has MAX_BATCH_SIZE items to work with

                /*  if (batch == null)
                 * {
                 *    throw new ArgumentException($"Illegal arguments count in batch. min is 1 max is 8");
                 * }*/
                dynamic taskResponse;
                bool    isErr = true;
                do
                {
                    BatchAnalyzeRequest analyzeRequest = new BatchAnalyzeRequest()
                    {
                        FolderId = config.folderId
                    };
                    foreach (ClassifyTaskModel t in batch)
                    {
                        analyzeRequest.AnalyzeSpecs.Add(makeAnalyzeSpec(t));
                    }

                    var call = visionClassifierClient.BatchAnalyzeAsync(
                        request: analyzeRequest,
                        headers: MakeMetadata(),
                        deadline: DateTime.UtcNow.AddMinutes(5)
                        ).GetAwaiter().GetResult();

                    taskResponse = JObject.Parse(call.ToString());

                    isErr = isError(taskResponse);
                    if (isErr)
                    {
                        this.log.LogInformation($"Quota exceeded waiting 5 sec.");
                        Thread.Sleep(5 * 1000);
                    }
                } while (isErr);

                safeResults(batch, taskResponse);
            }
        }
Beispiel #2
0
        public async Task <AnalyzeResult[]> RecognizeText(string folderId,
                                                          string iamToken,
                                                          Stream image,
                                                          string[] languages)
        {
            var channel = new Channel("vision.api.cloud.yandex.net", 443, new SslCredentials());
            var client  = new VisionService.VisionServiceClient(channel);
            var request = new BatchAnalyzeRequest
            {
                FolderId     = folderId,
                AnalyzeSpecs =
                {
                    new AnalyzeSpec
                    {
                        Content  = await ByteString.FromStreamAsync(image),
                        Features =
                        {
                            new Feature
                            {
                                Type = Feature.Types.Type.TextDetection,
                                TextDetectionConfig = new FeatureTextDetectionConfig
                                {
                                    LanguageCodes =
                                    {
                                        languages
                                    }
                                }
                            }
                        }
                    }
                }
            };
            var headers = new Metadata
            {
                {
                    "Authorization", "Bearer " + iamToken
                }
            };
            var response = await client.BatchAnalyzeAsync(request, headers).ResponseAsync;

            return(response.Results
                   .Select(ConvertAnalyzeResult)
                   .ToArray());
        }