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 (Process proc = new Process()) { proc.EnableRaisingEvents = false; proc.StartInfo.UseShellExecute = false; proc.StartInfo.CreateNoWindow = true; // If there is no custom directory if (string.IsNullOrEmpty(options.SoftwarePath)) { proc.StartInfo.WorkingDirectory = Path.Combine(Utilities.GetWorkingDirectory(), "tesseract"); } else { proc.StartInfo.WorkingDirectory = options.SoftwarePath; } proc.StartInfo.FileName = Path.Combine(proc.StartInfo.WorkingDirectory, "tesseract.exe"); proc.StartInfo.Arguments = options.ImagePath + " " + options.AnalyzedFileDestinationPath + " -psm 6 quiet"; proc.Start(); proc.WaitForExit(); } // Correct txt path for tesseract (it will append .txt under windows always) string analyzedFile = options.AnalyzedFileDestinationPath + ".txt"; return File.ReadAllLines(analyzedFile); }
private string[] AnalyzeSinglepageTiff(string singlepageTiffFileName, OcrProcessOptions options) { using (Process proc = new Process()) { proc.EnableRaisingEvents = false; proc.StartInfo.UseShellExecute = false; proc.StartInfo.CreateNoWindow = true; // If there is no custom directory if (string.IsNullOrEmpty(options.SoftwarePath)) { proc.StartInfo.WorkingDirectory = Path.Combine(Utilities.GetWorkingDirectory(), "cuneiform"); } else { proc.StartInfo.WorkingDirectory = options.SoftwarePath; } proc.StartInfo.FileName = Path.Combine(proc.StartInfo.WorkingDirectory, "cuneiform.exe"); string singlepageTiffAnalyzedFile = Path.Combine(Path.GetDirectoryName(options.AnalyzedFileDestinationPath), Path.GetFileName(singlepageTiffFileName) + ".txt"); proc.StartInfo.Arguments = "-l ger --singlecolumn -o " + singlepageTiffAnalyzedFile + " " + singlepageTiffFileName; proc.Start(); proc.WaitForExit(); string[] lines = File.ReadAllLines(singlepageTiffAnalyzedFile); TryDeleteFile(singlepageTiffFileName); TryDeleteFile(singlepageTiffAnalyzedFile); return lines; } }
string[] IOcrSoftware.ProcessImage(OcrProcessOptions options) { List<string> analyzedLines = new List<string>(); foreach (string singlepageTiffFileName in SplitMultipageTiff(options.ImagePath)) { analyzedLines.AddRange(AnalyzeSinglepageTiff(singlepageTiffFileName, options)); } // Finally write all analyzed lines to the desired path File.WriteAllLines(options.AnalyzedFileDestinationPath + ".txt", analyzedLines); return analyzedLines.ToArray(); }
string[] IOcrSoftware.ProcessImage(OcrProcessOptions options) { return new string[0]; }
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); } }
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 (!_serviceProvider.GetService<IAlarmFilter>().QueryAcceptSource(string.Join(" ", lines))) { 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); } }