示例#1
0
        private List <string> run_tessract()
        {
            List <string>        re        = new List <string>();
            string               dataPath  = "./tessdata/";
            string               language  = 1 != 1 ? "eng" : "jpn";
            string               inputFile = "./_tmp.bmp";
            OcrEngineMode        oem       = OcrEngineMode.DEFAULT;
            PageSegmentationMode psm       = PageSegmentationMode.AUTO_OSD;

            TessBaseAPI tessBaseAPI = new TessBaseAPI();

            // Initialize tesseract-ocr
            if (!tessBaseAPI.Init(dataPath, language, oem))
            {
                throw new Exception("Could not initialize tesseract.");
            }

            // Set the Page Segmentation mode
            tessBaseAPI.SetPageSegMode(psm);

            // Set the input image
            Pix pix = tessBaseAPI.SetImage(inputFile);

            tessBaseAPI.SetVariable("number", "1234567890");

            // Recognize image
            tessBaseAPI.Recognize();

            ResultIterator resultIterator = tessBaseAPI.GetIterator();

            // extract text from result iterator
            StringBuilder     stringBuilder     = new StringBuilder();
            PageIteratorLevel pageIteratorLevel = PageIteratorLevel.RIL_PARA;

            do
            {
                string str = resultIterator.GetUTF8Text(pageIteratorLevel);


                if (str != null)
                {
                    str = Regex.Replace(str, @"\n", "\r\n");
                    re.Add(str);
                }
            } while (resultIterator.Next(pageIteratorLevel));

            tessBaseAPI.Dispose();
            pix.Dispose();
            return(re);
        }
示例#2
0
        public static string OcrProcess(string image, string language = "eng")
        {
            //training data
            string tessdataPath = ConfigurationManager.AppSettings["TessdataPath"];

            string      text;
            TessBaseAPI tEngine = new TessBaseAPI(tessdataPath, language, OcrEngineMode.DEFAULT);

            tEngine.Process(image);
            text = tEngine.GetUTF8Text().ToString();
            tEngine.Clear();
            tEngine.Dispose();
            return(text);
        }
示例#3
0
        static void example4()
        {
            string               dataPath  = "./tessdata/";
            string               language  = "eng";
            string               inputFile = "./input.png";
            OcrEngineMode        oem       = OcrEngineMode.DEFAULT;
            PageSegmentationMode psm       = PageSegmentationMode.AUTO_OSD;

            TessBaseAPI tessBaseAPI = new TessBaseAPI();

            // Initialize tesseract-ocr
            if (!tessBaseAPI.Init(dataPath, language, oem))
            {
                throw new Exception("Could not initialize tesseract.");
            }

            // Set the Page Segmentation mode
            tessBaseAPI.SetPageSegMode(psm);

            // Set the input image
            Pix pix = tessBaseAPI.SetImage(inputFile);

            // Recognize image
            tessBaseAPI.Recognize();

            //ensure input name is set
            tessBaseAPI.SetInputName(inputFile);

            var    fileInfo     = new System.IO.FileInfo(inputFile);
            string tessDataPath = tessBaseAPI.GetDatapath();
            string outputName   = fileInfo.FullName.Replace(fileInfo.Extension, string.Empty); //input name.pdf

            // call pdf renderer and export pdf
            using (var pdfRenderer = new PdfRenderer(outputName, tessDataPath, false))
            {
                pdfRenderer.BeginDocument("tesseract.net searchable Pdf generation");
                pdfRenderer.AddImage(tessBaseAPI);
                pdfRenderer.EndDocument();
            }

            tessBaseAPI.Dispose();
            pix.Dispose();
        }
示例#4
0
        private void ConvertImageToText()
        {
            try{
                string               dataPath  = "./tessdata";
                string               language  = "eng";
                string               inputFile = "./imageSaved.bmp";
                OcrEngineMode        oem       = OcrEngineMode.DEFAULT;
                PageSegmentationMode psm       = PageSegmentationMode.AUTO_OSD;

                TessBaseAPI tessBaseAPI = new TessBaseAPI();

                if (!tessBaseAPI.Init(dataPath, language, oem))
                {
                    throw new Exception("Could not initialize tesseract.");
                }

                // Set the Page Segmentation mode
                tessBaseAPI.SetPageSegMode(psm);

                // Set the input image
                tessBaseAPI.SetImage(inputFile);

                // Recognize image
                tessBaseAPI.Recognize();

                ResultIterator resultIterator = tessBaseAPI.GetIterator();

                // Extract text from result iterator
                StringBuilder     stringBuilder     = new StringBuilder();
                PageIteratorLevel pageIteratorLevel = PageIteratorLevel.RIL_WORD;// RIL_PARA;

                do
                {
                    stringBuilder.Append(resultIterator.GetUTF8Text(pageIteratorLevel) + " ");
                } while (resultIterator.Next(pageIteratorLevel));

                tessBaseAPI.Dispose();
                textParsed = stringBuilder.ToString().Trim();
                //textParsedBox.Text = stringBuilder.ToString().Trim();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
示例#5
0
        static void example3()
        {
            string               dataPath  = "./tessdata/";
            string               language  = "eng";
            string               inputFile = "./input.png";
            OcrEngineMode        oem       = OcrEngineMode.DEFAULT;
            PageSegmentationMode psm       = PageSegmentationMode.AUTO_OSD;

            TessBaseAPI tessBaseAPI = new TessBaseAPI();

            // Initialize tesseract-ocr
            if (!tessBaseAPI.Init(dataPath, language, oem))
            {
                throw new Exception("Could not initialize tesseract.");
            }

            // Set the Page Segmentation mode
            tessBaseAPI.SetPageSegMode(psm);

            // Set the input image
            Pix pix = tessBaseAPI.SetImage(inputFile);

            // Recognize image
            tessBaseAPI.Recognize();

            ResultIterator resultIterator = tessBaseAPI.GetIterator();

            // extract text from result iterator
            StringBuilder     stringBuilder     = new StringBuilder();
            PageIteratorLevel pageIteratorLevel = PageIteratorLevel.RIL_PARA;

            do
            {
                stringBuilder.Append(resultIterator.GetUTF8Text(pageIteratorLevel));
            } while (resultIterator.Next(pageIteratorLevel));

            tessBaseAPI.Dispose();
            pix.Dispose();
        }
        public void /*Image*/ GetTextFromImage(IWebElement element, string uniqueName)
        {
            Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();

            string pth       = Assembly.GetCallingAssembly().CodeBase;
            string finalpth  = pth.Substring(0, pth.LastIndexOf("bin")) + "Screenshots/" + uniqueName + ".jpeg";
            string localpath = new Uri(finalpth).LocalPath;

            screenshot.SaveAsFile(localpath, ScreenshotImageFormat.Jpeg);

            Image     img  = Image.FromFile(localpath /*uniqueName*/);
            Rectangle rect = new Rectangle();

            if (element != null)
            {
                // Get the Width and Height of the WebElement using
                int width  = element.Size.Width;
                int height = element.Size.Height;

                // Get the Location of WebElement in a Point.
                // This will provide X & Y co-ordinates of the WebElement
                Point p = element.Location;

                // Create a rectangle using Width, Height and element location
                rect = new Rectangle(p.X, p.Y, width, height);
            }

            //croping the image based on rect.
            Bitmap bmpImage   = new Bitmap(img);
            var    cropedImag = bmpImage.Clone(rect, bmpImage.PixelFormat);


            string dataPath = @"C:\Betsold\AutomationTesting\Tests\testdata\";
            string language = "eng";
            string imgPath  = @"C:\Betsold\AutomationTesting\Tests\Screenshots\logo-test.jpeg";

            OcrEngineMode        oem = OcrEngineMode.LSTM_ONLY;
            PageSegmentationMode psm = PageSegmentationMode.AUTO;

            TessBaseAPI tessBaseAPI = new TessBaseAPI(dataPath, language, oem, psm);

            // Set the input image
            tessBaseAPI.SetImage(imgPath);

            var processedImage = tessBaseAPI.GetThresholdedImage();

            processedImage.Write(@"C:\Users\ibozhinovski\Desktop\", ImageFileFormatTypes.IFF_JFIF_JPEG);


            // Recognize image
            tessBaseAPI.Recognize();

            ResultIterator resultIterator = tessBaseAPI.GetIterator();

            // Extract text from result iterator
            StringBuilder     text = new StringBuilder();
            PageIteratorLevel pageIteratorLevel = PageIteratorLevel.RIL_PARA;

            do
            {
                text.Append(resultIterator.GetUTF8Text(pageIteratorLevel));
            } while (resultIterator.Next(pageIteratorLevel));

            tessBaseAPI.Dispose();

            Console.Read();

            /*
             * // croping the image based on rect.
             * Bitmap bmpImage = new Bitmap(img);
             * var cropedImag = bmpImage.Clone(rect, bmpImage.PixelFormat);
             *
             * var ocr = new TesseractEngine("./testdata", "eng");
             *
             * var page = ocr.Process(cropedImag);
             *
             * var result = page.GetText();
             *
             * Console.WriteLine(result);
             */
        }
示例#7
0
 public void TestDispose()
 {
     api.Dispose();
 }
示例#8
0
        public new void KeyDown(KeyboardHookEventArgs e)
        {
            // Use Singleton design to store location information
            PartLocation currentLocation = PartLocation._instance;

            // Print Screen key was pressed
            if (e.Key == Keys.PrintScreen)
            {
                // Grab image from screen and convert to black and white using a threshold
                PrintScreenThreshold();

                // Process text from image
                try
                {
                    string               dataPath  = @"./tessdata";
                    string               language  = "eng";
                    string               inputFile = "rewards.jpg";
                    OcrEngineMode        oem       = OcrEngineMode.DEFAULT;
                    PageSegmentationMode psm       = PageSegmentationMode.SINGLE_LINE;

                    TessBaseAPI tessBaseAPI = new TessBaseAPI();

                    // Initialize tesseract-ocr
                    if (!tessBaseAPI.Init(dataPath, language, oem))
                    {
                        throw new Exception("Could not initialize tesseract");
                    }

                    // Set the Page Segmentation mode
                    tessBaseAPI.SetPageSegMode(psm);

                    // Warframe Relics are displayed using A-Z, a-z, and &
                    tessBaseAPI.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&");

                    // Set input file
                    Pix pix = tessBaseAPI.SetImage(inputFile);

                    TB_Part1.Text = GetText(tessBaseAPI, currentLocation.PartLoc1, 580);
                    TB_Part2.Text = GetText(tessBaseAPI, currentLocation.PartLoc2, 580);
                    if (currentLocation.NumPeople >= 3)
                    {
                        TB_Part3.Text = GetText(tessBaseAPI, currentLocation.PartLoc3, 580);
                    }
                    if (currentLocation.NumPeople == 4)
                    {
                        TB_Part4.Text = GetText(tessBaseAPI, currentLocation.PartLoc4, 580);
                    }

                    tessBaseAPI.Dispose();
                    pix.Dispose();

                    // Grab latest order list for parts
                    // TODO: Try multiple threads! If that's too fast, cache prices for a duration
                    using (var client = new WebClient())
                    {
                        GetPriceJson(client, TB_Part1, TB_Plat1);
                        GetPriceJson(client, TB_Part2, TB_Plat2);
                        if (currentLocation.NumPeople >= 3)
                        {
                            GetPriceJson(client, TB_Part3, TB_Plat3);
                        }
                        if (currentLocation.NumPeople == 4)
                        {
                            GetPriceJson(client, TB_Part4, TB_Plat4);
                        }

                        // These get cached
                        GetDucatsJson(client, TB_Part1, TB_Ducats1);
                        GetDucatsJson(client, TB_Part2, TB_Ducats2);
                        if (currentLocation.NumPeople >= 3)
                        {
                            GetDucatsJson(client, TB_Part3, TB_Ducats3);
                        }
                        if (currentLocation.NumPeople == 4)
                        {
                            GetDucatsJson(client, TB_Part4, TB_Ducats4);
                        }
                    }

                    List <PartWorth> sortPartWorth = new List <PartWorth>();
                    sortPartWorth.Add(new PartWorth(TB_Part1, TB_Ducats1));
                    sortPartWorth.Add(new PartWorth(TB_Part2, TB_Ducats2));
                    if (currentLocation.NumPeople >= 3)
                    {
                        sortPartWorth.Add(new PartWorth(TB_Part3, TB_Ducats3));
                    }
                    if (currentLocation.NumPeople == 4)
                    {
                        sortPartWorth.Add(new PartWorth(TB_Part4, TB_Ducats4));
                    }

                    // Sort by Plat, then Ducats
                    sortPartWorth = sortPartWorth.OrderByDescending(o => Int32.Parse(o.platinum.Tag.ToString())).ThenByDescending(o => Int32.Parse(o.ducats.Tag.ToString())).ToList();

                    // If max Plat is low, sort by the reverse
                    if (Int32.Parse(sortPartWorth[0].platinum.Tag.ToString()) < 15)
                    {
                        sortPartWorth = sortPartWorth.OrderByDescending(o => Int32.Parse(o.ducats.Tag.ToString())).ThenByDescending(o => Int32.Parse(o.platinum.Tag.ToString())).ToList();
                    }

                    // Show best option
                    TB_Pick.Text = sortPartWorth[0].platinum.Text;

                    // Show current amount of Plat (and Ducats) made in the current session
                    if (Int32.Parse(sortPartWorth[0].platinum.Tag.ToString()) != -1)
                    {
                        //GlobalVar.PLAT += Int32.Parse(platDuc[0].plat.Tag.ToString());
                        LB_Plat.Tag = Int32.Parse(LB_Plat.Tag.ToString()) + Int32.Parse(sortPartWorth[0].platinum.Tag.ToString());
                    }
                    if (Int32.Parse(sortPartWorth[0].ducats.Tag.ToString()) != -1)
                    {
                        //GlobalVar.DUCATS += Int32.Parse(platDuc[0].ducats.Tag.ToString());
                        LB_Ducs.Tag = Int32.Parse(LB_Ducs.Tag.ToString()) + Int32.Parse(sortPartWorth[0].ducats.Tag.ToString());
                    }

                    LB_Plat.Text = LB_Plat.Tag.ToString() + " p";
                    LB_Ducs.Text = LB_Ducs.Tag.ToString() + " d";
                }
                catch (Exception ex)
                {
                    LogError("KeyDown: " + ex.Message);
                }
            }
            else if (e.Key == Keys.NumPad2)
            {
                Debug.WriteLine("Switched to 2 people");

                ClearBox();
                VisibleRow(false, LB_Part3, TB_Part3, TB_Plat3, TB_Ducats3);
                VisibleRow(false, LB_Part4, TB_Part4, TB_Plat4, TB_Ducats4);

                currentLocation.SetPeople2();
            }
            else if (e.Key == Keys.NumPad3)
            {
                Debug.WriteLine("Switched to 3 people");

                ClearBox();
                VisibleRow(true, LB_Part3, TB_Part3, TB_Plat3, TB_Ducats3);
                VisibleRow(false, LB_Part4, TB_Part4, TB_Plat4, TB_Ducats4);

                currentLocation.SetPeople3();
            }
            else if (e.Key == Keys.NumPad4)
            {
                Debug.WriteLine("Switched to 4 people");

                ClearBox();
                VisibleRow(true, LB_Part3, TB_Part3, TB_Plat3, TB_Ducats3);
                VisibleRow(true, LB_Part4, TB_Part4, TB_Plat4, TB_Ducats4);

                currentLocation.SetPeople4();
            }
            else if (e.Key == Keys.Pause)
            {
                Environment.Exit(0);
            }
        }
示例#9
0
        //public static void  clearFaceFlag() { faceFlag = 0; }
        public string RecognizeText(int id)
        {
            DBService dbs = new DBService();
            Document  doc = dbs.FindDocumentById(id);

            Image <Gray, Byte> img = scale(doc);

            //var image = PixConverter.ToPix(img.ToBitmap()))

            Tesseract.Native.DllImports.TesseractDirectory = System.Web.HttpContext.Current.Server.MapPath("~/Tesseract/bin/Debug/DLLS/");
            TessBaseAPI tessBaseAPI = new TessBaseAPI();

            System.Diagnostics.Debug.WriteLine("The current version is {0}", tessBaseAPI.GetVersion());

            string dataPath = System.Web.HttpContext.Current.Server.MapPath("~/tessdata/");
            string language = "eng";

            string        inputFile = doc.Path;
            OcrEngineMode oem       = OcrEngineMode.DEFAULT;
            //OcrEngineMode oem = OcrEngineMode.DEFAULT;
            PageSegmentationMode psm = PageSegmentationMode.AUTO_OSD;

            // Initialize tesseract-ocr
            if (!tessBaseAPI.Init(dataPath, language, oem))
            {
                throw new Exception("Could not initialize tesseract.");
            }

            // Set the Page Segmentation mode
            tessBaseAPI.SetPageSegMode(psm);

            // Set the input image
            Pix pix = tessBaseAPI.SetImage(inputFile);

            // Recognize image
            tessBaseAPI.Recognize();

            ResultIterator resultIterator = tessBaseAPI.GetIterator();

            // extract text from result iterator
            StringBuilder stringBuilder = new StringBuilder();
            int           top, bottom, left, right, i = 0;

            List <OCRText> forJson = new List <OCRText>();

            PageIteratorLevel pageIteratorLevel = PageIteratorLevel.RIL_TEXTLINE;

            do
            {
                string textContent = resultIterator.GetUTF8Text(pageIteratorLevel);
                resultIterator.BoundingBox(pageIteratorLevel, out left, out top, out right, out bottom);
                string coordsString = "" + left + "," + top + "," + right + "," + bottom;

                forJson.Add(new OCRText()
                {
                    Coords = coordsString, Text = textContent
                });
            } while (resultIterator.Next(pageIteratorLevel));

            tessBaseAPI.Dispose();
            pix.Dispose();

            var textForReturn = JsonConvert.SerializeObject(forJson);

            dbs.UpdateDocument(textForReturn, id);

            if (HasFace(img) == true)
            {
                FaceFlag = 1;
            }
            else
            {
                FaceFlag = 0;
            }

            return(textForReturn);
        }