Strabo.Core.OCR.ABBYYSingleStringResultParser.ReadOCRResults C# (CSharp) Method

ReadOCRResults() public method

public ReadOCRResults ( string dir ) : void
dir string
return void
        public void ReadOCRResults(string dir)
        {
            DirectoryInfo dirinfo = new DirectoryInfo(dir);

            foreach (FileInfo NextFile in dirinfo.GetFiles()) //
            {
                if (NextFile.Extension == ".docx"
                    && !NextFile.Name.Contains('~'))
                {
                    string fn = Path.GetFileNameWithoutExtension(NextFile.Name);
                    string[] token = fn.Split('_');

                    int num = Convert.ToInt16(token[0]);
                    int ch = Convert.ToInt16(token[2]);
                    int x = Convert.ToInt16(token[3]);
                    int y = Convert.ToInt16(token[4]);

                    double slope = Convert.ToInt16(token[6]);
                    int bbxx = Convert.ToInt16(token[7]);
                    int bbxy = Convert.ToInt16(token[8]);
                    int bbxw = Convert.ToInt16(token[9]);
                    int bbxh = Convert.ToInt16(token[10]);

                    textlabel_list.Add(new TextLabel(num,ch,x,y,slope,fn, bbxx,bbxy,bbxw,bbxh));
                }
            }
            for (int i = 0; i < textlabel_list.Count; i++)
            {
                TextLabel textlabel = textlabel_list[i];
                DocxToText dtt = new DocxToText(dir + textlabel.fn + ".docx");
                textlabel.text = dtt.ExtractText();
                textlabel.susp_char_count = dtt.susp_char_count;
                //if (textlabel.text == "" || textlabel.text == null ||suscharratio(textlabel))// ||  line_counter > 1)
                //{
                //    textlabel_list.RemoveAt(i);
               //     i--;
                //}
            }
            ResultMerger();
        }

Usage Example

        public void ApplyABBYYOCR(string dir, string fn)
        {
            try
            {
                ABBYYSingleStringResultParser assrp = new ABBYYSingleStringResultParser();
                StreamWriter sw = new StreamWriter(dir + "OCR_results.txt");
                assrp.ReadOCRResults(dir);
                //sw.WriteLine("Text ID" + ";"
                //             + "Recognized Text" + ";" + "Suspicious Text" + ";" + "BoundingBox Top-Left X" + ";" + "BoundingBox Top-Left Y" + ";" + "BoundingBox Width" + ";" + "BoundingBox Height" + ";" + "Position X" + ";" + "Position Y" + ";" + "Orientation");
                sw.WriteLine("ID" + ","
                            + "Text" + "," + "BoundingBoxTopLeftX" + "," + "BoundingBoxTopLeftY" + "," + "BoundingBoxWidth" + "," + "BoundingBoxHeight");
                Bitmap srcimg = new Bitmap(dir+fn);
                srcimg = ImageUtils.AnyToFormat24bppRgb(srcimg);

                Graphics g = Graphics.FromImage(srcimg);
                for (int i = 0; i < assrp.textlabel_list.Count; i++)
                {
                    TextLabel textlabel = assrp.textlabel_list[i];
                    Font font = new Font("Arial", 10);
                    try
                    {
                        SolidBrush sb = new SolidBrush(Color.FromArgb(50, 0, 0, 0));
                        g.DrawRectangle(
                            new Pen(Color.Black, 3), new Rectangle(textlabel.bbxx, textlabel.bbxy, textlabel.bbxw, textlabel.bbxh));
                        g.FillRectangle(
                           sb, new Rectangle(textlabel.bbxx, textlabel.bbxy, textlabel.bbxw, textlabel.bbxh));

                        //g.DrawString(textlabel.id.ToString(), font, Brushes.Black, textlabel.bbxx, textlabel.bbxy);
                        g.DrawString(textlabel.text, font, Brushes.Black, textlabel.bbxx, textlabel.bbxy + textlabel.bbxh);
                    }
                    catch { }
                    if (i < assrp.textlabel_list.Count - 1)
                        sw.WriteLine(textlabel.id + ","
                        + textlabel.text + "," + textlabel.bbxx + "," + -1 * textlabel.bbxy + "," + textlabel.bbxw + "," + textlabel.bbxh);
                    else
                        sw.Write(textlabel.id + ","
                       + textlabel.text + "," + textlabel.bbxx + "," + -1 * textlabel.bbxy + "," + textlabel.bbxw + "," + textlabel.bbxh);
                    //srcimg.Save(CurrentState.output_dir + CurrentState.text_recognition_results_fn, ImageFormat.Png);
                }
                sw.Close();
                g.Dispose();

                //ShapefileUtils sut = new ShapefileUtils();
                //sut.WrtieText2Shp(assrp, dir, "OCR_results");
            }
            catch (Exception exception)
            {
                Log.WriteLine("GenerateOCRResults: " + exception.Message);
            }
        }