public static new PageOcrData FromXml(String xmlFilePath) #endif { try { try { PageOcrData res = FromXml(xmlFilePath, typeof(PageOcrData)) as PageOcrData; if (res != null) { return(res); } } catch { } //-- Try a collection ocr data --\\ CollectionOcrData coll = FromXml(xmlFilePath, typeof(CollectionOcrData)) as CollectionOcrData; if (coll != null && coll.Pages.Length > 0) { return(coll.Pages[0]); } } catch (Exception ex) { ILog.LogError(ex); } return(null); }
public static CollectionOcrData FromCollection(ITisCollectionData collection) #endif { try { if (collection != null) { CollectionOcrData res = new CollectionOcrData(); res.CollectionID = collection.Name; res.FlowType = collection.FlowType; List <PageOcrData> pgs = new List <PageOcrData>(); //-- Iterate collection pages and add them to the class Pages property. --\\ foreach (ITisPageData page in collection.Pages) { pgs.Add(PageOcrData.FromPRD(page)); } if (pgs != null && pgs.Count > 0) { res.pages = pgs.ToArray(); return(res); } } else { throw new Exception(String.Format("ITisCollectionData specified in: [{0}] is not valid", MethodBase.GetCurrentMethod().Name)); } } catch (Exception ex) { ILog.LogError(ex); } return(null); }
public static TPage ToPRD(PageOcrData ocrData, String saveToPrdPath) #endif { try { if (ocrData != null) { TPage res = new TPage(); //-- iterate data lines --\\ foreach (LineOcrData lineOcr in ocrData.Lines) { TLine currentLine = new TLine(); //-- Itarate the line's words --\\ foreach (WordOcrData wordOcr in lineOcr.Words) { //-- Iterate the word's chars --\\ TWord wordPrd = new TWord(); foreach (CharOcrData charOcr in wordOcr.GetAsChars(true)) { wordPrd.AddChar(new TChar(charOcr.Value, (short)charOcr.Confidence, new TOCRRect(charOcr.Rect))); } currentLine.AddWord(wordPrd); } if (currentLine != null && currentLine.Words != null && currentLine.Words.Count > 0) { res.AddLine(currentLine); } } //-- Save the contents to a PRD file if 'saveToPrdPath' is specified --\\ if (!String.IsNullOrEmpty(saveToPrdPath)) { if (!Directory.Exists(Path.GetDirectoryName(saveToPrdPath))) { Directory.CreateDirectory(Path.GetDirectoryName(saveToPrdPath)); } TPage.SaveToPRD(res, saveToPrdPath); } return(res); } else { throw new Exception(String.Format("Invalid input PRD object in: [{0}]", MethodBase.GetCurrentMethod().Name)); } } catch (Exception ex) { ILog.LogError(ex); } return(null); }
/// <summary> /// Show an open file dialog for source PRD and save File dialog for atrget XML, then convert source PRD to XML. /// when multiple are selected they will be inserted into one XML file representing a collection /// </summary>> public static bool PRDToXml(string targetXML, params string[] sourcePRDs) { try { if (File.Exists(targetXML)) { File.Delete(targetXML); } if (sourcePRDs != null && sourcePRDs.Length == 1) { if (PageOcrData.FromPRD(sourcePRDs[0]).ToXml(targetXML) && File.Exists(targetXML)) { ILog.LogDebug(string.Format("Done converting PRD file [{0}] to XML file [{1}]", sourcePRDs[0], targetXML)); return(true); } } else if (sourcePRDs != null && sourcePRDs.Length > 1) { CollectionOcrData collOcr = new CollectionOcrData(); List <PageOcrData> pages = new List <PageOcrData>(); foreach (string srcPrd in sourcePRDs) { if (File.Exists(srcPrd)) { pages.Add(PageOcrData.FromPRD(srcPrd)); } } collOcr.pages = pages.ToArray(); collOcr.CollectionID = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fffff"); return(collOcr.ToXml(targetXML)); } } catch (Exception ex) { ILog.LogError(ex); } return(false); }
public static PageOcrData FromPRD(ITisPageData pageData) #endif { try { if (pageData != null) { PageOcrData res = FromPRD(pageData.GetAttachmentFileName("PRD"), true); res.Index = pageData.OrderInCollection - 1; return(res); } else { throw new Exception(String.Format("ITisPageData specified in: [{0}] is not valid", MethodBase.GetCurrentMethod().Name)); } } catch (Exception ex) { ILog.LogError(ex); } return(null); }
public static PageOcrData FromPRD(String prdPath, bool doNotParseName) #endif { try { if (File.Exists(prdPath)) { //-- Load from file -\\ PageOcrData res = FromPRD(TPage.LoadFromPRD(prdPath)); res.PrdFilePath = prdPath; #region //-- Try and get the page index from the file name --\\ if (!doNotParseName) { try { int prs = -1; String[] splt = Path.GetFileNameWithoutExtension(prdPath).Split('_'); prs = Convert.ToInt32(splt[splt.Length - 1].Trim('p', 'P'), 16); } catch { } } #endregion return(res); } else { throw new Exception(String.Format("File [{0}] could not be found, in: [{1}]", prdPath ?? String.Empty, MethodBase.GetCurrentMethod().Name)); } } catch (Exception ex) { ILog.LogError(ex); } return(null); }
/// <summary> /// Convert XML file\s to PRD . /// </summary> /// <param name="targetPRD">The target PRD file to create</param> /// <param name="sourceXMLs">Source XML files.</param> /// <returns>true when successfull.</returns> public static bool XMLToPRD(string targetPRD, params string[] sourceXMLs) { bool result = false; try { foreach (string sourceXML in sourceXMLs) { if (!File.Exists(sourceXML)) { continue; } //-- Try loading as a single page --\\ PageOcrData pod = PageOcrData.FromXml(sourceXML); if (pod != null && pod.ToPRD(targetPRD) != null) { if (File.Exists(targetPRD)) { File.Delete(targetPRD); } ILog.LogDebug(string.Format("Done converting XML file [{0}] to PRD file [{1}]", sourceXML, targetPRD)); continue; } //-- Try as a whole collection in a single XML --\\ if (pod == null) { CollectionOcrData cod = CollectionOcrData.FromXml(sourceXML); if (cod != null) { if (cod.Pages != null) { int lastIndex = -1; int maxIndex = 0; foreach (PageOcrData pd in cod.Pages) { lastIndex = pd.Index; while (lastIndex < 0 || lastIndex == pd.Index || lastIndex <= maxIndex) { lastIndex++; } maxIndex = lastIndex; //-- Create a PRD file per page --\\ string newPageName = Path.Combine(Path.GetDirectoryName(targetPRD), string.Format("{0}_P{1}.{2}", Path.GetFileNameWithoutExtension(targetPRD), (lastIndex).ToString("X").PadLeft(3, '0'), Path.GetExtension(targetPRD).TrimStart('.'))); if (File.Exists(newPageName)) { File.Delete(newPageName); } pd.ToPRD(newPageName); } } ILog.LogDebug(string.Format("Done converting XML file [{0}] to PRD file [{1}]", sourceXML, targetPRD)); result = true; } } } } catch (Exception ex) { ILog.LogError(ex); } return(result); }
/// <summary> /// Convert a <see cref="PageOcrData"/> to <see cref="TPage"/> object. /// </summary> /// <param name="ocrData">The <see cref="PageOcrData"/> to convert to PRD.</param> /// <param name="saveToPrdPath">A path (optionla) to save the PRD file to.</param> /// <returns>TPage when successfull, null when not.</returns> #if INTERNAL internal static TPage ToPRD(PageOcrData ocrData, String saveToPrdPath)
public static TPage ToPRD(PageOcrData ocrData) #endif { return(ToPRD(ocrData, null)); }
/// <summary> /// Convert a <see cref="PageOcrData"/> to <see cref="TPage"/> object. /// </summary> /// <param name="ocrData">The <see cref="PageOcrData"/> to convert to PRD.</param> /// <returns>TPage when successfull, null when not.</returns> #if INTERNAL internal static TPage ToPRD(PageOcrData ocrData)
public static PageOcrData FromPRD(TPage pagePRD) #endif { try { if (pagePRD != null) { PageOcrData res = new PageOcrData(); res.Index = -1; res.Rect = pagePRD.Rectangle; int li = 0; List <LineOcrData> linesData = new List <LineOcrData>(); //-- Iterate PRD data lines --\\ foreach (TLine linePRD in pagePRD.Lines) { linesData.Add(new LineOcrData()); List <WordOcrData> wordsData = new List <WordOcrData>(); int wi = 0; //-- Iterate each line's words --\\ foreach (TWord wordPRD in linePRD.Words) { wordsData.Add(new WordOcrData()); if (wordPRD.Chars != null && wordPRD.Chars.Count > 0) { List <CharOcrData> charsData = new List <CharOcrData>(); int ci = 0; //-- Iterate each word's chars --\\ foreach (TChar charPRD in wordPRD.Chars) { //-- Add and define char data --\\ charsData.Add(new CharOcrData()); charsData[charsData.Count - 1].Index = ci; charsData[charsData.Count - 1].Confidence = charPRD.Confidance; charsData[charsData.Count - 1].Rect = charPRD.Rectangle; charsData[charsData.Count - 1].Value = charPRD.CharData; ci++; } if (charsData != null && charsData.Count > 0) { wordsData[wordsData.Count - 1].Chars = charsData.ToArray(); } } //-- Define word data --\\ wordsData[wordsData.Count - 1].Confidence = wordPRD.Confidance; wordsData[wordsData.Count - 1].Index = wi; wordsData[wordsData.Count - 1].Rect = wordPRD.Rectangle; wordsData[wordsData.Count - 1].Style = wordPRD.Style; wordsData[wordsData.Count - 1].Value = wordPRD.WordData; wi++; } if (wordsData != null && wordsData.Count > 0) { //-- Define line data --\\ linesData[linesData.Count - 1].Confidence = linePRD.Confidance; linesData[linesData.Count - 1].Index = li; linesData[linesData.Count - 1].Rect = linePRD.Rectangle; linesData[linesData.Count - 1].Words = wordsData.ToArray(); } li++; } //-- Add the recognized lines to the result --\\ if (linesData != null) { res.Lines = linesData.ToArray(); } if (res.Lines != null) { return(res); } } else { throw new Exception(String.Format("TPage specified in: [{0}] is not valid", MethodBase.GetCurrentMethod().Name)); } } catch (Exception ex) { ILog.LogError(ex); } return(null); }