Esempio n. 1
0
        public void GetIcrChannelResult(OcrVoucher voucher)
        {
            var requestId = 0;

            try
            {
                requestId = GetIcrChannelResult(_channelId, voucher.RequestId, _channelTimeout);
                if (voucher.RequestId == requestId)
                {
                    Functions.GetResult(_a2IaEngine, requestId, voucher);
                }
                //Console.WriteLine("Voucher request {0} being retrieved", requestId);
                CloseRequest(requestId);
            }
            catch (Exception ex)
            {
                var errorMessage = _a2IaEngine.ScrGetLastError();
                Log.Error(ex, "An error has ocurred getting the results for the image for voucher {@voucherId}, with error {1}", voucher.Id, errorMessage);
                if (requestId > 0)
                {
                    CloseRequest(requestId);
                }
                throw;
            }
        }
Esempio n. 2
0
        //private readonly BlockingCollection<OcrVoucher> voucherQueue = new BlockingCollection<OcrVoucher>(10000);

        private void ProcessVouchers(IEnumerable <OcrVoucher> vouchers, VoucherType voucherType)
        {
            var voucherTable = new Dictionary <VoucherType, string>
            {
                { VoucherType.Credit, "Credit" },
                { VoucherType.Debit, "Debit" }
            };

            var voucherSubset = vouchers.Where(v => v.VoucherType == voucherType);
            var enumerable    = voucherSubset as IList <OcrVoucher> ?? voucherSubset.ToList();

            if (!enumerable.Any())
            {
                return;
            }
            SelectDocumentTable(voucherTable[voucherType]);
            Log.Debug("Process {0} vouchers of type {1}", enumerable.Count, voucherType);
            var ocrVouchers = enumerable.ToList();

            foreach (var voucher in ocrVouchers)
            {
                ProcessVoucher(voucher);
                Log.Debug("Adding voucher id {0} with request id {1} to queue", voucher.Id, voucher.RequestId);
                _voucherQueue.Enqueue(voucher);
                //voucherQueue.Add(voucher);
            }

            while (!_voucherQueue.IsEmpty)
            //while (!voucherQueue.IsCompleted)
            {
                OcrVoucher voucher = null;

                try
                {
                    if (!_voucherQueue.TryDequeue(out voucher))
                    {
                        continue;
                    }
                    //voucher = voucherQueue.Take();
                }
                catch (InvalidOperationException)
                {
                }
                if (voucher == null)
                {
                    continue;
                }
                Log.Debug("Retrieving voucher id {0} with request id {1} to queue", voucher.Id, voucher.RequestId);
                GetIcrChannelResult(voucher);
            }
        }
Esempio n. 3
0
 public void ProcessVoucher(OcrVoucher voucher)
 {
     try
     {
         LoadImage(DocumentId, voucher.ImagePath);
         voucher.RequestId = OpenIcrChannel(ChannelId, DocumentId);
     }
     catch (Exception ex)
     {
         Log.Error(ex, "An error has ocurred while processing the image for voucher {@voucherId}", voucher.Id);
         CloseChannel(ChannelId);
         throw;
     }
 }
Esempio n. 4
0
        private string SetVoucherConfiguration(OcrVoucher voucher)
        {
            string documentTableFilename;

            switch (voucher.VoucherType)
            {
            case VoucherType.Credit:
                documentTableFilename = configuration.CreditTablePath;
                break;

            default:
                documentTableFilename = configuration.DebitTablePath;
                break;
            }

            return(documentTableFilename);
        }
Esempio n. 5
0
        public void GetIcrChannelResult(OcrVoucher voucher)
        {
            var requestId = 0;

            try
            {
                requestId = GetIcrChannelResult(ChannelId, voucher.RequestId, configuration.ChannelTimeout);
                if (voucher.RequestId == requestId)
                {
                    Functions.GetResult(a2IaEngine, requestId, voucher);
                }
                CloseRequest(requestId);
            }
            catch (Exception ex)
            {
                var errorMessage = a2IaEngine.ScrGetLastError();
                Log.Error(ex, "An error has ocurred while processing the image for voucher {@voucherId} with message {1}", voucher.Id, errorMessage);
                if (requestId > 0)
                {
                    CloseRequest(requestId);
                }
                throw;
            }
        }
Esempio n. 6
0
 // ReSharper disable once UnusedMember.Local
 private async Task LoadMem(int channelId, int documentId, OcrVoucher voucher)
 {
     using (var imageFile = System.IO.File.OpenRead(voucher.ImagePath))
     {
         var closureVoucher = voucher;
         closureVoucher.ImageBuffer = new byte[imageFile.Length];
         try
         {
             //read the image file into memory
             await imageFile
             .ReadAsync(closureVoucher.ImageBuffer, 0, (int)imageFile.Length)
             .ContinueWith(t =>
             {
                 //initialize processing in A2IA
                 if (t.IsCompleted && !t.IsFaulted)
                 {
                     a2IaEngine.ScrDefineImage(documentId, configuration.FileType, "MEM", closureVoucher.ImageBuffer);
                     closureVoucher.RequestId = (int)a2IaEngine.ScrOpenRequest(channelId, documentId);
                 }
                 else
                 {
                     throw t.Exception ?? new Exception("Could not process the image.");
                 }
             });
         }
         catch (Exception ex)
         {
             Log.Error(ex, "An error has ocurred while processing the image for voucher {@voucherId}",
                       closureVoucher.Id);
             if (closureVoucher.RequestId > 0)
             {
                 a2IaEngine.ScrCloseRequest(closureVoucher.RequestId);
             }
         }
     }
 }
Esempio n. 7
0
 private string GetBatchId(OcrVoucher voucher)
 {
     return(voucher.BatchId);
 }
Esempio n. 8
0
        /// <summary>
        /// Populate the OCR results into the metadata associated with the image
        /// </summary>
        /// <param name="a2IaEngine"></param>
        /// <param name="resultId">OCR result Id</param>
        /// <param name="voucher">Metadata of the image to be populated</param>
        public static void GetResult(API a2IaEngine, int resultId, OcrVoucher voucher)
        {
            // A2iA amount recognition
            if (a2IaEngine.GetStringProperty(resultId, Constants.EngineFields.Amount) == Constants.Enabled)
            {
                switch (voucher.VoucherType)
                {
                case VoucherType.Credit:
                    voucher.AmountResult.Result = a2IaEngine.GetStringProperty(resultId,
                                                                               Constants.ResultFields.Amount1);
                    var temp = a2IaEngine.GetStringProperty(resultId,
                                                            string.Concat(Constants.ResultFields.CreditBase, "Amount"));
                    var temp1 = a2IaEngine.GetStringProperty(resultId,
                                                             string.Concat(Constants.ResultFields.AltCreditBase, "Amount"));
                    var temp2 = a2IaEngine.GetStringProperty(resultId,
                                                             string.Concat(Constants.ResultFields.CheckBase, "Amount"));
                    System.Diagnostics.Debug.WriteLine("credit base = {0}, alt credit base = {1} check base = {2}", temp, temp1, temp2);

                    voucher.AmountResult.Score = a2IaEngine.GetStringProperty(resultId,
                                                                              Constants.ResultFields.AmountConfidence1);
                    break;

                default:
                    voucher.AmountResult.Result = a2IaEngine.GetStringProperty(resultId,
                                                                               Constants.ResultFields.Amount);
                    voucher.AmountResult.Score = a2IaEngine.GetStringProperty(resultId,
                                                                              Constants.ResultFields.AmountConfidence);
                    break;
                }

                var x1 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.AmountLocationX1]);
                var y1 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.AmountLocationY1]);
                var x2 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.AmountLocationX2]);
                var y2 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.AmountLocationY2]);

                voucher.AmountResult.Location = new Rectangle(x1, y1, x2 - x1, y2 - y1);
            }

            // A2iA codeline recognition
            if (a2IaEngine.GetStringProperty(resultId, Constants.EngineFields.CodeLine) == Constants.Enabled)
            {
                voucher.CodelineResult.Result = a2IaEngine.GetStringProperty(resultId, Constants.ResultFields.CodelineRecognition);
                voucher.CodelineResult.Score  = a2IaEngine.GetStringProperty(resultId, Constants.ResultFields.CodelineConfidence);

                var x1 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.CodelineLocationX1]);
                var y1 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.CodelineLocationY1]);
                var x2 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.CodelineLocationX2]);
                var y2 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.CodelineLocationY2]);

                voucher.CodelineResult.Location = new Rectangle(x1, y1, x2 - x1, y2 - y1);
            }

            // A2iA date recognition
            if (a2IaEngine.GetStringProperty(resultId, Constants.EngineFields.Date) == Constants.Enabled)
            {
                var day   = (uint)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.DateRecognitionDay];
                var month = (uint)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.DateRecognitionMonth];
                var year  = (uint)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.DateRecognitionYear];

                voucher.DateResult.Result = string.Format("{0}/{1}/{2}", day, month, year);
                voucher.DateResult.Score  = a2IaEngine.GetStringProperty(resultId, Constants.ResultFields.DateConfidence);

                var x1 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.DateLocationX1]);
                var y1 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.DateLocationY1]);
                var x2 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.DateLocationX2]);
                var y2 = (int)((float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.DateLocationY2]);

                voucher.DateResult.Location = new Rectangle(x1, y1, x2 - x1, y2 - y1);
            }

            // Image Rotation
            voucher.ImageRotation = (int)(float)a2IaEngine.ObjectProperty[resultId, Constants.ResultFields.OrientationCorrection];

            //Log.Verbose("v {0} - amt {1} score {2} - micr {3} score {4}", voucher.Id, voucher.AmountResult.Result, voucher.AmountResult.Score, voucher.CodelineResult.Result, voucher.CodelineResult.Score);
        }
Esempio n. 9
0
 public Task GetResultAsync(int requestId, int documentId, int tableId, int channelId, OcrVoucher voucher)
 {
     return(Task.Run(() =>
     {
         int resultId;
         lock (processingLock)
         {
             resultId = GetIcrChannelResult(channelId, requestId, 3000);
         }
         if (resultId == requestId)
         {
             Functions.GetResult(a2IaEngine, resultId, voucher);
         }
         //ReleaseResources(requestId, documentId, tableId, channelId);
         a2IaEngine.ScrCloseRequest(requestId);
     }));
 }