Пример #1
0
        private string processImage(string imageInput, string outputFilePath)
        {
            var debugMessage = new StringBuilder();

            try
            {
                debugMessage.Append("Process Image output file path " + outputFilePath);
                var ocr          = new Ocr();
                var preProcessor = new PreProcessor();

                debugMessage.Append("Objects created");

                preProcessor.Deskew      = true;
                preProcessor.Autorotate  = false;
                preProcessor.RemoveLines = true;
                preProcessor.Binarize    = 96;
                preProcessor.Morph       = "c2.2";

                ocr.License =
                    "MD1BZHZhbmNlZDsxPWtlZXJ0aSAtIGt2a2lydGh5QGdtYWlsLmNvbTsyPTUyNDY4Njg5OTE5MTMwMjg5NTI7Mz1rZWVydGkgLSBrdmtpcnRoeUBnbWFpbC5jb207ND05OTk7NT1UcnVlOzUuMT1GYWxzZTs3PTYzNTE4ODYwODAwMDAwMDAwMDs4PTQxRDA3NEFFODJFQjI3QjM3RDdGMTUzQ0REQjVEQkNFNEVGRjdGREU5MEIwOTg1MjkwQ0JDREFCQTM3MEFBNzU7OT0xLjQxLjAuMA";

                ocr.ResourceFolder      = @"C:\Aquaforest\OCRSDK\bin";
                ocr.EnableConsoleOutput = true;
                ocr.EnableTextOutput    = true;

                //ocr.ReadBMPSource(@"C:\Users\KotaruV\Documents\Visual Studio 2012\Projects\Playground\OCRConsoleApp\OCRConsoleApp\images\3.jpg");
                //ocr.ReadTIFFSource(imageInput);
                ocr.ReadBMPSource(imageInput);

                debugMessage.Append("Read from bmp source. ");
                ocr.Recognize(preProcessor);
                debugMessage.Append("Recognize executed. ");

                ocr.SaveTextOutput(outputFilePath, true);
                var ocrText = getOcrTextFromFile(outputFilePath);
                debugMessage.Append("Saved text output. ");
                ocr.DeleteTemporaryFiles();
                debugMessage.Append("Deleted temporary files. ");
                return(ocrText);
            }
            finally
            {
                System.Diagnostics.EventLog.WriteEntry("Application", debugMessage.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }
Пример #2
0
        private string processImage(string imageInput, string outputFilePath)
        {
            var debugMessage = new StringBuilder();
            try
            {
                debugMessage.Append("Process Image output file path " + outputFilePath);
                var ocr = new Ocr();
                var preProcessor = new PreProcessor();

                debugMessage.Append("Objects created");

                preProcessor.Deskew = true;
                preProcessor.Autorotate = false;
                preProcessor.RemoveLines = true;
                preProcessor.Binarize = 96;
                preProcessor.Morph = "c2.2";

                ocr.License =
               "MD1BZHZhbmNlZDsxPWtlZXJ0aSAtIGt2a2lydGh5QGdtYWlsLmNvbTsyPTUyNDY4Njg5OTE5MTMwMjg5NTI7Mz1rZWVydGkgLSBrdmtpcnRoeUBnbWFpbC5jb207ND05OTk7NT1UcnVlOzUuMT1GYWxzZTs3PTYzNTE4ODYwODAwMDAwMDAwMDs4PTQxRDA3NEFFODJFQjI3QjM3RDdGMTUzQ0REQjVEQkNFNEVGRjdGREU5MEIwOTg1MjkwQ0JDREFCQTM3MEFBNzU7OT0xLjQxLjAuMA";
                
                ocr.ResourceFolder = @"C:\Aquaforest\OCRSDK\bin";
                ocr.EnableConsoleOutput = true;
                ocr.EnableTextOutput = true;

                //ocr.ReadBMPSource(@"C:\Users\KotaruV\Documents\Visual Studio 2012\Projects\Playground\OCRConsoleApp\OCRConsoleApp\images\3.jpg");
                //ocr.ReadTIFFSource(imageInput);
                ocr.ReadBMPSource(imageInput);

                debugMessage.Append("Read from bmp source. ");
                ocr.Recognize(preProcessor);
                debugMessage.Append("Recognize executed. ");
                
                ocr.SaveTextOutput(outputFilePath, true);
                var ocrText = getOcrTextFromFile(outputFilePath);
                debugMessage.Append("Saved text output. ");
                ocr.DeleteTemporaryFiles();
                debugMessage.Append("Deleted temporary files. ");
                return ocrText;
            }
            finally
            {
                System.Diagnostics.EventLog.WriteEntry("Application", debugMessage.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }

        }
Пример #3
0
        private List <LineData> ProcessDocument(COB_ParseType stepId, string sourceFile, string extension)
        {
            List <LineData> lines = new List <LineData>();

            using (Ocr ocr = new Ocr(GetConfigResource()))
            {
                try
                {
                    var preProcessor = Configure(ocr);
                    switch (extension.ToLower())
                    {
                    case ".jpg":
                    case ".jpeg":
                    case ".png":
                    case ".bmp":
                        ocr.ReadJPEGSource(sourceFile);

                        break;

                    case ".tif":
                    case ".tiff":
                        ocr.ReadTIFFSource(sourceFile);
                        break;

                    case ".pdf":
                        //ocr.EnablePdfOutput = true;
                        //ocr.EnableTextOutput = false;
                        ocr.EndPage = 2;    //process only 2 pages as currently nothing is being read from 3rd page
                        ocr.ReadPDFSource(sourceFile);
                        break;
                    }
                    if (stepId == COB_ParseType.TradeLicense)
                    {
                        preProcessor.ForceTableZones = true;
                    }
                    if (!ocr.Recognize(preProcessor))
                    {
                        if (ocr.LastException == null)
                        {
                            throw new Exception("Unable to parse document");
                        }
                        else
                        {
                            throw ocr.LastException;
                        }
                    }

                    for (int i = 1; i <= ocr.NumberPages && i <= ocr.EndPage; i++)
                    {
                        var pageLines = ocr.ReadPageLines(i);
                        if (pageLines?.Count > 0)
                        {
                            lines.AddRange(pageLines);
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    try
                    {
                        File.Copy(ocr.LogFilePath, Path.Combine(logsPath, Path.GetFileNameWithoutExtension(sourceFile) + ".log"));
                    }
                    catch (Exception exp)
                    {
                    }
                    ocr.DeleteTemporaryFiles();
                }
            }
            return(lines);
        }
Пример #4
0
        public string getValuesbyStepId(COB_ParseType stepId, string sourceFile, string extension)
        {
            string result = string.Empty;

            using (Ocr ocr = new Ocr(GetConfigResource()))
            {
                try
                {
                    var preProcessor = Configure(ocr);

                    if (stepId == COB_ParseType.EmiratesId)
                    {
                        using (Bitmap map = new Bitmap(sourceFile))
                        {
                            //using (var img = Helper.ConvertBlackWhiteImage(map))
                            {
                                var img = map;
                                //Helper.SetContrast(img, 80);
                                ocr.ReadImageSource(img);
                                if (ocr.Recognize(preProcessor))
                                {
                                    result = this.getValuesByEid(ocr);
                                }
                                else
                                {
                                    if (ocr.LastException == null)
                                    {
                                        throw new Exception("Unable to parse document");
                                    }
                                    else
                                    {
                                        throw ocr.LastException;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        switch (extension.ToLower())
                        {
                        case ".jpg":
                        case ".jpeg":
                        case ".png":
                        case ".bmp":
                            ocr.ReadJPEGSource(sourceFile);

                            break;

                        case ".tif":
                        case ".tiff":
                            ocr.ReadTIFFSource(sourceFile);
                            break;

                        case ".pdf":
                            //ocr.EnablePdfOutput = true;
                            //ocr.EnableTextOutput = false;
                            ocr.EndPage = 2;    //process only 2 pages as currently nothing is being read from 3rd page
                            ocr.ReadPDFSource(sourceFile);
                            break;
                        }
                        if (stepId == COB_ParseType.TradeLicense)
                        {
                            preProcessor.ForceTableZones = true;
                        }

                        if (!ocr.Recognize(preProcessor))
                        {
                            if (ocr.LastException == null)
                            {
                                throw new Exception("Unable to parse document");
                            }
                            else
                            {
                                throw ocr.LastException;
                            }
                        }
                        switch (stepId)
                        {
                        case COB_ParseType.TradeLicense:
                            var licenseLines = new List <LineData>();
                            for (int i = 1; i <= ocr.NumberPages && i <= ocr.EndPage; i++)
                            {
                                var pageLines = ocr.ReadPageLines(i);
                                if (pageLines?.Count > 0)
                                {
                                    licenseLines.AddRange(pageLines);
                                }
                            }
                            // currently 'dubai' is hardcoded, required changes to fetch it from Application
                            result = TradeLicenseParserFactory.Parse(licenseLines, "DUBAIECONOMY", "LLC");
                            break;

                        case COB_ParseType.Passport:
                            StringBuilder build = new StringBuilder();
                            for (int pageNo = 0; pageNo < ocr.NumberPages; pageNo++)
                            {
                                var lines = ocr.ReadPageLines(pageNo + 1);
                                if (lines?.Count > 0)
                                {
                                    int i = 0;
                                    for (i = 0; i < lines.Count; i++)
                                    {
                                        Regex re = new Regex(@"(P|V|C)(<|«)(.*)");
                                        if (re.IsMatch(lines[i].LineWords))
                                        {
                                            break;
                                        }
                                    }
                                    int counti = 0;
                                    while (i < lines.Count && counti < 2)     //only two lines to be read as passport has only two line MRZ as per standard
                                    {
                                        string temp = lines[i].LineWords;
                                        temp = temp.TrimEnd().Replace("«", "<<").Replace("&", "<").Replace("\n", "").Replace(" ", "");
                                        temp = System.Text.RegularExpressions.Regex.Replace(temp, "[']+", "");
                                        if (counti == 0)
                                        {
                                            temp = Regex.Replace(temp, ".*P<", "P<");
                                        }
                                        temp = temp.Substring(0, Math.Min(temp.Length, 44));
                                        temp = temp.PadRight(44, '<');

                                        build.Append(temp);
                                        i++;
                                    }
                                }
                            }
                            result = build.ToString();
                            MRZParser pareser = new MRZParser();
                            result = pareser.Parse(result);
                            break;

                        case COB_ParseType.EmiratesId:

                            result = getValuesByEid(ocr);
                            break;

                        default:
                            return(string.Empty);
                        }
                    }
                }
                catch (Exception exp)
                {
                    throw;
                }
                finally{
                    try
                    {
                        File.Copy(ocr.LogFilePath, Path.Combine(logsPath, Path.GetFileNameWithoutExtension(sourceFile) + ".log"));
                    }
                    catch (Exception exp) {
                    }
                    ocr.DeleteTemporaryFiles();
                }
            }

            return(result);
        }