예제 #1
0
파일: Inspector.cs 프로젝트: nikkh/Horus
        private async Task <List <ScoreRecord> > InspectModelRegistration()
        {
            var results = new List <ScoreRecord>();

            log.LogTrace($"Checking that a Model has been registered for each document type");
            var documentTypesForChallenge = Environment.GetEnvironmentVariable("DocumentTypesForChallenge").Split(',').ToList();

            foreach (var documentType in documentTypesForChallenge)
            {
                log.LogTrace($"Checking {documentType}");
                var mtr = HorusSql.GetModelIdByDocumentFormat(documentType);
                if (mtr.DocumentFormat != null)
                {
                    log.LogTrace($"Model {mtr.ModelId} has been registered for {documentType}");
                    results.Add(new ScoreRecord {
                        Type = $"Training", Notes = $"{mtr.ModelId} has been registered for document type {documentType}", Score = 500
                    });
                }
            }
            return(results);
        }
예제 #2
0
        public async Task <DocumentProcessingJob> StartRecognizer([ActivityTrigger] DocumentProcessingJob job, ILogger log, Microsoft.Azure.WebJobs.ExecutionContext ec)
        {
            var snip  = $"Orchestration { job.OrchestrationId}: { ec.FunctionName} - ";
            var model = HorusSql.GetModelIdByDocumentFormat(job.DocumentFormat);

            job.Model = model;
            log.LogTrace($"{snip} Document Name={job.DocumentName}, Format={job.DocumentFormat}, Model={model.ModelId}, Version={model.ModelVersion}");

            var queryString = HttpUtility.ParseQueryString(string.Empty);

            queryString["includeTextDetails"] = "True";
            var uri = $"{recognizerServiceBaseUrl}{BaseConstants.FormRecognizerApiPath}/{model.ModelId}/{BaseConstants.FormRecognizerAnalyzeVerb}?{queryString}";

            log.LogTrace($"{snip} Recognizer Uri={uri}");

            HttpResponseMessage response;

            byte[] image             = null;
            byte[] md5hash           = null;
            var    orchestrationBlob = await orchestrationBlobClient.GetBlobReferenceFromServerAsync(new Uri(job.OrchestrationBlobUrl));

            using (var memoryStream = new MemoryStream())
            {
                await orchestrationBlob.DownloadToStreamAsync(memoryStream);

                image = memoryStream.ToArray();
                using (var md5 = MD5.Create())
                {
                    memoryStream.Position = 0;
                    md5hash = md5.ComputeHash(memoryStream);
                }
            }
            job.Thumbprint = BitConverter.ToString(md5hash).Replace("-", " ");
            log.LogTrace($"{snip} Orchestration Blob={job.OrchestrationBlobName} downloaded.  Thumbprint={job.Thumbprint}");

            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", recognizerApiKey);
            using (var postContent = new ByteArrayContent(image))
            {
                postContent.Headers.ContentType = new MediaTypeHeaderValue(job.ContentType);
                response = await client.PostAsync(uri, postContent);
            }

            string getUrl = "";

            if (response.IsSuccessStatusCode)
            {
                log.LogTrace($"{snip} Recognition request successful.");
                HttpHeaders headers = response.Headers;
                if (headers.TryGetValues("operation-location", out IEnumerable <string> values))
                {
                    getUrl = values.First();
                    log.LogTrace($"{snip} Recognition progress can be tracked at {getUrl}");
                }
            }
            else
            {
                log.LogTrace($"{snip} Recognition request unsuccessful.");
                throw new Exception($"{snip} That didnt work.  Trying to submit image for analysis {uri} Content:{response.Content.ReadAsStringAsync().Result}");
            }

            job.RecognizerStatusUrl = getUrl;
            log.LogInformation($"{snip} Completed successfully");
            return(job);
        }