string[] IOcrSoftware.ProcessImage(OcrProcessOptions options)
        {
            using (ProcessWrapper proc = new ProcessWrapper())
            {
                // If there is no custom directory
                if (string.IsNullOrEmpty(options.SoftwarePath))
                {
                    proc.WorkingDirectory = Path.Combine(Utilities.GetWorkingDirectory(), "tesseract");
                }
                else
                {
                    proc.WorkingDirectory = options.SoftwarePath;
                }

                proc.FileName  = Path.Combine(proc.WorkingDirectory, "tesseract.exe");
                proc.Arguments = string.Format("\"{0}\" \"{1}\"  -psm 6 quiet", options.ImagePath, options.AnalyzedFileDestinationPath);

                proc.StartAndWait();
            }

            // Correct txt path for tesseract (it will append .txt under windows always)
            string analyzedFile = options.AnalyzedFileDestinationPath + ".txt";

            return(File.ReadAllLines(analyzedFile));
        }
        string[] IOcrSoftware.ProcessImage(OcrProcessOptions options)
        {
            using (ProcessWrapper proc = new ProcessWrapper())
            {
                string tesseractPath = Path.Combine(Utilities.GetWorkingDirectory(), "tesseract");

                try
                {
                    Environment.SetEnvironmentVariable(EnvTessdata, tesseractPath);
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.OcrSoftwareTesseractError);
                    Logger.Instance.LogException(this, ex);
                }


                // If there is no custom directory
                if (string.IsNullOrEmpty(options.SoftwarePath))
                {
                    proc.WorkingDirectory = tesseractPath;
                }
                else
                {
                    proc.WorkingDirectory = options.SoftwarePath;
                }

                proc.FileName  = Path.Combine(proc.WorkingDirectory, "tesseract.exe");
                proc.Arguments = string.Format("\"{0}\" \"{1}\"  -psm 6 quiet", options.ImagePath, options.AnalyzedFileDestinationPath);

                proc.StartAndWait();
            }

            // Correct txt path for tesseract (it will append .txt under windows always)
            string analyzedFile = options.AnalyzedFileDestinationPath + ".txt";

            return(File.ReadAllLines(analyzedFile));
        }
Example #3
0
        private void ProcessNewImage(FileInfo file)
        {
            EnsureDirectoriesExist();

            string analyseFileName  = DateTime.Now.ToString(AnalyzedFileNameFormat);
            string archivedFilePath = Path.Combine(_archivePath.FullName, analyseFileName + ArchivedFilePathExtension);

            MoveFileTo(file, archivedFilePath);

            string[] parsedLines = null;
            try
            {
                OcrProcessOptions options = new OcrProcessOptions();
                options.SoftwarePath = _configuration.OCRSoftwarePath;
                options.AnalyzedFileDestinationPath = Path.Combine(_analysisPath.FullName, Path.GetFileNameWithoutExtension(file.FullName));
                options.ImagePath = file.FullName;

                Logger.Instance.LogFormat(LogType.Trace, this, Properties.Resources.OcrSoftwareParseBegin, file.FullName);

                Stopwatch swParse = Stopwatch.StartNew();

                parsedLines = _ocrSoftware.ProcessImage(options);

                swParse.Stop();

                Logger.Instance.LogFormat(LogType.Trace, this, Properties.Resources.OcrSoftwareParseEndSuccess, swParse.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.OcrSoftwareParseEndFail);
                Logger.Instance.LogException(this, ex);
                return;
            }

            IList <string>    analyzedLines = new List <string>();
            ReplaceDictionary replDict      = _configuration.ReplaceDictionary;

            foreach (string preParsedLine in parsedLines)
            {
                analyzedLines.Add(replDict.ReplaceInString(preParsedLine));
            }

            Operation operation = null;

            try
            {
                Logger.Instance.LogFormat(LogType.Trace, this, Properties.Resources.BeginParsingIncomingOperation);

                string[] lines = analyzedLines.ToArray();

                if (!IsOnWhitelist(lines))
                {
                    Logger.Instance.LogFormat(LogType.Info, this, Properties.Resources.FaxIsNotOnWhitelist);
                    return;
                }

                if (IsOnBlacklist(lines))
                {
                    Logger.Instance.LogFormat(LogType.Trace, this, Properties.Resources.FaxIsOnBlacklist);
                    return;
                }

                Stopwatch sw = Stopwatch.StartNew();

                operation = _parser.Parse(lines);

                sw.Stop();
                Logger.Instance.LogFormat(LogType.Trace, this, Properties.Resources.ParsingOperationCompleted, sw.ElapsedMilliseconds);

                // If there is no timestamp, use the current time. Not too good but better than MinValue :-/
                if (operation.Timestamp == DateTime.MinValue)
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, Properties.Resources.ParsingTimestampFailedUsingCurrentTime);
                    operation.Timestamp = DateTime.Now;
                }

                IDictionary <string, object> ctxParameters = new Dictionary <string, object>();
                ctxParameters[ContextParameterKeys.ArchivedFilePath] = archivedFilePath;
                ctxParameters[ContextParameterKeys.ImagePath]        = file.FullName;

                AlarmSourceEventArgs args = new AlarmSourceEventArgs(operation);
                args.Parameters = ctxParameters;

                OnNewAlarm(args);
            }
            catch (Exception ex)
            {
                Logger.Instance.LogFormat(LogType.Warning, this, Properties.Resources.ProcessNewImageError);
                Logger.Instance.LogException(this, ex);
            }
        }
        private void ProcessNewImage(FileInfo file)
        {
            EnsureDirectoriesExist();

            string analyseFileName  = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            string archivedFilePath = Path.Combine(_archivePath.FullName, analyseFileName + ".tif");

            // Moves the file to a different location, and throws if it failed.
            MoveFileTo(file, archivedFilePath);

            List <string> analyzedLines = new List <string>();
            Stopwatch     swParse       = new Stopwatch();

            string[] parsedLines = null;
            try
            {
                OcrProcessOptions options = new OcrProcessOptions();
                options.SoftwarePath = _configuration.OCRSoftwarePath;
                options.AnalyzedFileDestinationPath = Path.Combine(_analysisPath.FullName, Path.GetFileNameWithoutExtension(file.FullName));
                options.ImagePath = file.FullName;

                Logger.Instance.LogFormat(LogType.Trace, this, Properties.Resources.OcrSoftwareParseBegin, file.FullName);

                swParse.Start();

                parsedLines = _ocrSoftware.ProcessImage(options);

                swParse.Stop();

                Logger.Instance.LogFormat(LogType.Trace, this, Properties.Resources.OcrSoftwareParseEndSuccess, swParse.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                swParse.Stop();

                Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.OcrSoftwareParseEndFail);
                Logger.Instance.LogException(this, ex);
                // Abort parsing
                return;
            }

            ReplaceDictionary replDict = _configuration.ReplaceDictionary;

            foreach (string preParsedLine in parsedLines)
            {
                analyzedLines.Add(replDict.ReplaceInString(preParsedLine));
            }

            Operation operation = null;
            Stopwatch sw        = Stopwatch.StartNew();

            try
            {
                Logger.Instance.LogFormat(LogType.Trace, this, "Begin parsing incoming operation...");

                string[] lines = analyzedLines.ToArray();

                if (IsTestFax(lines))
                {
                    sw.Stop();
                    Logger.Instance.LogFormat(LogType.Trace, this, "Operation is a test-fax. Parsing is skipped.");
                }
                else
                {
                    operation = _parser.Parse(lines);

                    sw.Stop();
                    Logger.Instance.LogFormat(LogType.Trace, this, "Parsed operation in '{0}' milliseconds.", sw.ElapsedMilliseconds);

                    // If there is no timestamp, use the current time. Not too good but better than MinValue :-/
                    if (operation.Timestamp == DateTime.MinValue)
                    {
                        Logger.Instance.LogFormat(LogType.Warning, this, "Could not parse timestamp from the fax. Using the current time as the timestamp.");
                        operation.Timestamp = DateTime.Now;
                    }

                    Dictionary <string, object> ctxParameters = new Dictionary <string, object>();
                    ctxParameters["ArchivedFilePath"] = archivedFilePath;
                    ctxParameters["ImagePath"]        = file.FullName;

                    AlarmSourceEventArgs args = new AlarmSourceEventArgs(operation);
                    args.Parameters = ctxParameters;

                    // Raise event...
                    OnNewAlarm(args);
                }
            }
            catch (Exception ex)
            {
                sw.Stop();
                Logger.Instance.LogFormat(LogType.Warning, this, "An exception occurred while processing the alarmfax!");
                Logger.Instance.LogException(this, ex);
            }
        }
 string[] IOcrSoftware.ProcessImage(OcrProcessOptions options)
 {
     return(new string[0]);
 }