Пример #1
0
        public void TestOCREngGrayText()
        {
            using (Tesseract ocr = GetTesseract())
                using (Image <Gray, Byte> img = new Image <Gray, byte>(480, 200))
                {
                    ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,");
                    IntPtr oclDevice = new IntPtr();
                    int    deviceId  = ocr.GetOpenCLDevice(ref oclDevice);

                    String message = "Hello, World";
                    CvInvoke.PutText(img, message, new Point(50, 100), CvEnum.FontFace.HersheySimplex, 1.0, new MCvScalar(255));

                    //
                    //ocr.Recognize(img);
                    using (Image <Gray, Byte> rotatedImg = img.Rotate(10, new Gray(), false))
                    {
                        ocr.PageSegMode = PageSegMode.AutoOsd;
                        ocr.SetImage(rotatedImg);
                        ocr.Recognize();
                        using (PageIterator pi = ocr.AnalyseLayout())
                        {
                            Orientation   or       = pi.Orientation;
                            LineSegment2D?baseLine = pi.GetBaseLine(PageIteratorLevel.Textline);
                            if (baseLine.HasValue)
                            {
                                CvInvoke.Line(rotatedImg, baseLine.Value.P1, baseLine.Value.P2, new MCvScalar(255));
                                //Emgu.CV.UI.ImageViewer.Show(rotatedImg);
                            }
                        }


                        String messageOcr = ocr.GetUTF8Text().TrimEnd('\n', '\r'); // remove end of line from ocr-ed text
                        //EmguAssert.AreEqual(message, messageOcr,
                        //   String.Format("'{0}' is not equal to '{1}'", message, messageOcr));

                        Tesseract.Character[] results = ocr.GetCharacters();

                        String s1 = ocr.GetBoxText();
                        //String s2 = ocr.GetOsdText();
                        String s3 = ocr.GetTSVText();
                        String s4 = ocr.GetUNLVText();

                        using (PDFRenderer pdfRenderer = new PDFRenderer("abc.pdf", "./", false))
                            using (Pix imgPix = new Pix(img.Mat))
                            {
                                //bool success = ocr.ProcessPage(imgPix, 1, "img", null, 100000, pdfRenderer);
                                //EmguAssert.IsTrue(success, "failed to export pdf");
                            }
                    }
                }
        }
Пример #2
0
        public void TestOCREngGrayText()
        {
            using (Tesseract ocr = GetTesseract())
                using (Image <Gray, Byte> img = new Image <Gray, byte>(480, 200))

                {
                    ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,");
                    IntPtr oclDevice = new IntPtr();
                    int    deviceId  = ocr.GetOpenCLDevice(ref oclDevice);

                    String message = "Hello, World";
                    CvInvoke.PutText(img, message, new Point(50, 100), CvEnum.FontFace.HersheySimplex, 1.0, new MCvScalar(255));

                    //
                    //ocr.Recognize(img);
                    using (Image <Gray, Byte> rotatedImg = img.Rotate(10, new Gray(), false))
                    {
                        ocr.PageSegMode = PageSegMode.AutoOsd;

                        ocr.Recognize(rotatedImg);
                        using (PageIterator pi = ocr.AnalyseLayout())
                        {
                            Orientation   or       = pi.Orientation;
                            LineSegment2D?baseLine = pi.GetBaseLine(PageIteratorLevel.Textline);
                            if (baseLine.HasValue)
                            {
                                CvInvoke.Line(rotatedImg, baseLine.Value.P1, baseLine.Value.P2, new MCvScalar(255));
                                //Emgu.CV.UI.ImageViewer.Show(rotatedImg);
                            }
                        }


                        /*
                         * String messageOcr = ocr.GetText().TrimEnd('\n', '\r'); // remove end of line from ocr-ed text
                         * EmguAssert.AreEqual(message, messageOcr,
                         * String.Format("'{0}' is not equal to '{1}'", message, messageOcr));
                         *
                         * Tesseract.Character[] results = ocr.GetCharacters();*/
                    }
                }
        }