public TesseractOCREngine(Image image) { this.image = image; m_tesseract = new TesseractProcessor(); bool succeed = m_tesseract.Init(m_path, m_lang, (int)TesseractEngineMode.DEFAULT); m_tesseract.SetVariable("tessedit_char_whitelist", "0123456789-.eExXpP/\\"); m_tesseract.SetVariable("tessedit_pageseg_mode", ((int)TesseractPageSegMode.PSM_SINGLE_LINE).ToString()); System.Environment.CurrentDirectory = System.IO.Path.GetFullPath(m_path); }
public Form1() { InitializeComponent(); m_tesseract = new TesseractProcessor(); m_tesseract.Init(m_path, m_lang, (int)TesseractEngineMode.DEFAULT); m_tesseract.SetVariable("tessedit_pageseg_mode", TesseractPageSegMode.PSM_AUTO.ToString()); }
private const string m_lang = "eng"; // 识别的语言文件名称 public string read2dig(Image image) { TesseractProcessor m_tesseract = new TesseractProcessor(); ; bool succeed = m_tesseract.Init(m_path, m_lang, (int)TesseractEngineMode.DEFAULT); if (!succeed) { return("tesseract初始化失败"); } m_tesseract.SetVariable("tessedit_char_whitelist", "0123456789"); //设置识别变量,当前只能识别数字。 image.Save("./img/原图.bmp", System.Drawing.Imaging.ImageFormat.Bmp); Bitmap bmp = ToGray(image); bmp.Save("./img/灰度图.bmp", System.Drawing.Imaging.ImageFormat.Bmp); bmp = ConvertTo1Bpp1(bmp, 110); bmp.Save("./img/二值图.bmp", System.Drawing.Imaging.ImageFormat.Bmp); bmp = Zoom(bmp, 0.25); bmp.Save("./img/放大缩小后.bmp", System.Drawing.Imaging.ImageFormat.Bmp); m_tesseract.Clear(); m_tesseract.ClearAdaptiveClassifier(); return(m_tesseract.Apply(bmp)); }
public static string Recognize(string url) { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream st = response.GetResponseStream(); if (st == null) { return(string.Empty); } try { Bitmap bitmap = ((Bitmap)Bitmap.FromStream(st)).toGray().biLinear(2); //var newBitmap= KiCut(bitmap, 12, 22, bitmap.Width - 12, 22); string tessdata = Environment.CurrentDirectory + "\\tessdata\\"; string language = "eng";//设置训练文件的名称,后缀traineddata之前的名称 int oem = 3; // http://www.lixin.me/blog/2012/05/26/29536 训练 using (TesseractProcessor processor = new TesseractProcessor()) { //初始化 bool initFlage = processor.Init(tessdata, language, oem); //processor.GetTesseractEngineVersion();//获取版本号 /* * Set the current page segmentation mode. Defaults to PSM_SINGLE_BLOCK. * The mode is stored as an IntParam so it can also be modified by ReadConfigFile or SetVariable("tessedit_pageseg_mode", mode as string). */ //processor.SetPageSegMode(ePageSegMode.PSM_SINGLE_BLOCK); //设置ROI(图像的感兴趣区域) processor.UseROI = true; processor.ROI = new Rectangle(24, 0, bitmap.Width - 24, bitmap.Height); //设置识别的变量 如果是自定义培训的文件 可以不用设置 //必须在初始化后调用 processor.SetVariable("tessedit_char_whitelist", "0123456789."); //processor.SetVariable("tessedit_thresholding_method", "1"); //图像处理阀值是否打开 //processor.SetVariable("save_best_choices", "T"); using (Bitmap bmp = bitmap) { int i = 3; oem = i; string text = processor.Recognize(bmp); char[] charsToTrim = { '\\', 'n', '\\', 'n' }; return(text.TrimEnd(charsToTrim)); //Console.WriteLine( // string.Format( // "RecognizeMode: {1}\nText:\n{0}\n++++++\n", text, ((eOcrEngineMode)oem).ToString())); } } } catch (Exception) { return(string.Empty); } }
private void OCR_Load(object sender, EventArgs e) { m_tesseract = new TesseractProcessor(); bool succeed = m_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Tesseract initialization failed. The application will exit."); Application.Exit(); } m_tesseract.SetVariable("tessedit_char_whitelist", "ABCDEFHKLMNPQRSTUVXYZ-.1234567890").ToString(); //Training Sets //abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ-.1234567890 System.Environment.CurrentDirectory = System.IO.Path.GetFullPath(m_path); }
private void MainForm_Load(object sender, EventArgs e) { int soluong; int tong; tong = int.Parse(txt_tong.Text); soluong = int.Parse(bss.so_luong_xe()); txt_conlai.Text = (tong - soluong).ToString(); dataGridView1.DataSource = bss.Danhsachxe(); capture = new Emgu.CV.Capture(); timer1.Enabled = true; full_tesseract = new TesseractProcessor(); bool succeed = full_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Tesseract initialization failed. The application will exit."); Application.Exit(); } full_tesseract.SetVariable("tessedit_char_whitelist", "ABCDEFHKLMNPRSTVXY1234567890").ToString(); ch_tesseract = new TesseractProcessor(); succeed = ch_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Tesseract initialization failed. The application will exit."); Application.Exit(); } ch_tesseract.SetVariable("tessedit_char_whitelist", "ABCDEFHKLMNPRSTUVXY").ToString(); num_tesseract = new TesseractProcessor(); succeed = num_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Tesseract initialization failed. The application will exit."); Application.Exit(); } num_tesseract.SetVariable("tessedit_char_whitelist", "1234567890").ToString(); m_path = System.Environment.CurrentDirectory + "\\"; string[] ports = SerialPort.GetPortNames(); for (int i = 0; i < box.Length; i++) { box[i] = new PictureBox(); } }
void CheckInPage_Loaded(object sender, RoutedEventArgs e) { full_tesseract = new TesseractProcessor(); bool succeed = full_tesseract.Init(m_path, m_lang, 3); if (!succeed) { System.Windows.MessageBox.Show("Lỗi thư viện Tesseract. Chương trình cần kết thúc."); } full_tesseract.SetVariable("tessedit_char_whitelist", "ACDFHKLMNPRSTVXY1234567890").ToString(); ch_tesseract = new TesseractProcessor(); succeed = ch_tesseract.Init(m_path, m_lang, 3); if (!succeed) { System.Windows.MessageBox.Show("Lỗi thư viện Tesseract. Chương trình cần kết thúc."); } ch_tesseract.SetVariable("tessedit_char_whitelist", "ACDEFHKLMNPRSTUVXY").ToString(); num_tesseract = new TesseractProcessor(); succeed = num_tesseract.Init(m_path, m_lang, 3); if (!succeed) { System.Windows.MessageBox.Show("Lỗi thư viện Tesseract. Chương trình cần kết thúc."); } num_tesseract.SetVariable("tessedit_char_whitelist", "1234567890").ToString(); System.Environment.CurrentDirectory = System.IO.Path.GetFullPath(m_path); for (int i = 0; i < box.Length; i++) { box[i] = new PictureBox(); } string folder = System.AppDomain.CurrentDomain.BaseDirectory + "\\ImageTest"; foreach (string fileName in Directory.GetFiles(folder, "*.bmp", SearchOption.TopDirectoryOnly)) { lstimages.Add(System.IO.Path.GetFullPath(fileName)); } foreach (string fileName in Directory.GetFiles(folder, "*.jpg", SearchOption.TopDirectoryOnly)) { lstimages.Add(System.IO.Path.GetFullPath(fileName)); } pic1 = (hostPic1.Child as System.Windows.Forms.PictureBox); pic2 = (hostPic2.Child as System.Windows.Forms.PictureBox); img1 = (hostImg1.Child as Emgu.CV.UI.ImageBox); }
//private string _Caption = "http://www.laptrinhvb.net"; #endregion private void FrmMain_Load(object sender, EventArgs e) { full_tesseract = new TesseractProcessor(); bool succeed = full_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Lỗi thư viện Tesseract. Chương trình cần kết thúc."); Application.Exit(); } full_tesseract.SetVariable("tessedit_char_whitelist", "ACDFHKLMNPRSTVXY1234567890").ToString(); ch_tesseract = new TesseractProcessor(); succeed = ch_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Lỗi thư viện Tesseract. Chương trình cần kết thúc."); Application.Exit(); } ch_tesseract.SetVariable("tessedit_char_whitelist", "ACDEFHKLMNPRSTUVXY").ToString(); num_tesseract = new TesseractProcessor(); succeed = num_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Lỗi thư viện Tesseract. Chương trình cần kết thúc."); Application.Exit(); } num_tesseract.SetVariable("tessedit_char_whitelist", "1234567890").ToString(); System.Environment.CurrentDirectory = System.IO.Path.GetFullPath(m_path); for (int i = 0; i < box.Length; i++) { box[i] = new PictureBox(); } string folder = Application.StartupPath + "\\ImageTest"; foreach (string fileName in Directory.GetFiles(folder, "*.bmp", SearchOption.TopDirectoryOnly)) { lstimages.Add(Path.GetFullPath(fileName)); } foreach (string fileName in Directory.GetFiles(folder, "*.jpg", SearchOption.TopDirectoryOnly)) { lstimages.Add(Path.GetFullPath(fileName)); } }
/* private void button3_Click(object sender, EventArgs e) * { * if (capture != null) * { * timer1.Enabled = false; * pictureBox_XeRA.Image = null; * IF.pictureBox2.Image = null; * capture.QueryFrame().Save("aa.bmp"); * FileStream fs = new FileStream(m_path + "aa.bmp", FileMode.Open, FileAccess.Read); * Image temp = Image.FromStream(fs); * fs.Close(); * pictureBox_XeRA.Image = temp; * IF.pictureBox2.Image = temp; * pictureBox_XeRA.Update(); * IF.pictureBox2.Update(); * Image temp1; * string temp2, temp3; * /*Reconize(m_path + "aa.bmp", out temp1, out temp2, out temp3); * pictureBox_XeVAO.Image = temp1; * if(temp3 == "") * text_BiensoVAO.Text = "ko nhận dạng dc biển số"; * else * text_BiensoVAO.Text = temp3; * * timer1.Enabled = true; * } * * }*/ private void MainForm_Load_1(object sender, EventArgs e) { capture = new Emgu.CV.Capture(); timer2.Enabled = true; timer4.Start(); IF = new ImageForm(); full_tesseract = new TesseractProcessor(); bool succeed = full_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Tesseract initialization failed. The application will exit."); Application.Exit(); } full_tesseract.SetVariable("tessedit_char_whitelist", "ABCDEFHKLMNPRSTVXY1234567890").ToString(); ch_tesseract = new TesseractProcessor(); succeed = ch_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Tesseract initialization failed. The application will exit."); Application.Exit(); } ch_tesseract.SetVariable("tessedit_char_whitelist", "ABCDEFHKLMNPRSTUVXY").ToString(); num_tesseract = new TesseractProcessor(); succeed = num_tesseract.Init(m_path, m_lang, 3); if (!succeed) { MessageBox.Show("Tesseract initialization failed. The application will exit."); Application.Exit(); } num_tesseract.SetVariable("tessedit_char_whitelist", "1234567890").ToString(); m_path = System.Environment.CurrentDirectory + "\\"; string[] ports = SerialPort.GetPortNames(); for (int i = 0; i < box.Length; i++) { box[i] = new PictureBox(); } }
protected void Page_Load(object sender, EventArgs e) { var image = System.Drawing.Image.FromFile(@"D:\Image\Capture1T.tif"); m_tesseract = new TesseractProcessor(); bool succeed = m_tesseract.Init(m_path, m_lang, (int)TesseractEngineMode.DEFAULT); if (!succeed) { } m_tesseract.SetVariable("tessedit_pageseg_mode", ((int)TesseractPageSegMode.PSM_SINGLE_LINE).ToString()); m_tesseract.Clear(); m_tesseract.ClearAdaptiveClassifier(); string outValue = m_tesseract.Apply(image); Response.Write(outValue); }
static oTesseractRequest __ocrExecute(oTesseractRequest req, Bitmap bitmap) { using (TesseractProcessor processor = new TesseractProcessor()) { // call SetVariable() method before passing image(api->SetImage(image)) // 0: otsu // 1: isodata local adaptive // 2: sauvola local adaptive => not implement yet //processor.SetVariable("tessedit_thresholding_method", "0"); processor.SetVariable("tessedit_thresholding_method", "1"); //processor.InitForAnalysePage(); //processor.SetPageSegMode(ePageSegMode.PSM_AUTO_ONLY); //var success = processor.Init(req.data_path, req.lang, (int)eOcrEngineMode.OEM_DEFAULT); //var imageColor = new Emgu.CV.Mat(); //var imageCV = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>(bitmap); //Image Class from Emgu.CV ////var imageCV = new Emgu.CV.Image<Emgu.CV.Structure.Gray, byte>(bitmap); //Image Class from Emgu.CV //var image = imageCV.Mat; //This is your Image converted to Mat //if (image.NumberOfChannels == 1) // Emgu.CV.CvInvoke.CvtColor(image, imageColor, Emgu.CV.CvEnum.ColorConversion.Gray2Bgr); //else // image.CopyTo(imageColor); using (var m0 = new MemoryStream()) { bitmap.Save(m0, ImageFormat.Jpeg); //using (Bitmap bmp = Bitmap.FromFile(@"C:\temp\1.jpg") as Bitmap) using (Bitmap bmp = new Bitmap(m0)) { DocumentLayout doc = null; switch (req.command) { case TESSERACT_COMMAND.GET_TEXT: //string s = tes.GetText().Trim(); //req.output_text = s; //req.output_count = s.Length; req.ok = 1; break; default: unsafe { doc = processor.AnalyseLayout(bmp); } if (doc != null) { var bs = new List <string>(); if (doc.Blocks.Count > 0) { for (int i = 0; i < doc.Blocks.Count; i++) { for (int j = 0; j < doc.Blocks[i].Paragraphs.Count; j++) { bs.AddRange(doc.Blocks[j].Paragraphs[j].Lines .Select(x => string.Format( "{0}_{1}_{2}_{3}", x.Left, x.Top, x.Right, x.Bottom))); } } } req.output_format = "left_top_right_bottom"; req.output_text = string.Join("|", bs.ToArray()); req.output_count = bs.Count; req.ok = 1; } break; } } } } return(req); }
public static string Recognize(string url,Rectangle rectangle) { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream st = response.GetResponseStream(); if (st == null) { return string.Empty; } Bitmap bitmap = ((Bitmap)Bitmap.FromStream(st)).toGray().biLinear(2); //var newBitmap= KiCut(bitmap, 12, 22, bitmap.Width - 12, 22); string tessdata = Environment.CurrentDirectory + "\\tessdata\\"; string language = "eng";//设置训练文件的名称,后缀traineddata之前的名称 int oem = 3; // http://www.lixin.me/blog/2012/05/26/29536 训练 using (TesseractProcessor processor = new TesseractProcessor()) { //初始化 bool initFlage = processor.Init(tessdata, language, oem); //processor.GetTesseractEngineVersion();//获取版本号 /* Set the current page segmentation mode. Defaults to PSM_SINGLE_BLOCK. The mode is stored as an IntParam so it can also be modified by ReadConfigFile or SetVariable("tessedit_pageseg_mode", mode as string). */ //processor.SetPageSegMode(ePageSegMode.PSM_SINGLE_BLOCK); //设置ROI(图像的感兴趣区域) if (rectangle != null) { processor.UseROI = true; processor.ROI = rectangle; } //设置识别的变量 如果是自定义培训的文件 可以不用设置 //必须在初始化后调用 processor.SetVariable("tessedit_char_whitelist", "0123456789."); //processor.SetVariable("tessedit_thresholding_method", "1"); //图像处理阀值是否打开 //processor.SetVariable("save_best_choices", "T"); using (Bitmap bmp = bitmap) { string text = processor.Recognize(bmp); char[] charsToTrim = { '\\', 'n', '\\', 'n' }; return text.TrimEnd(charsToTrim); } } }
public Form1() { InitializeComponent(); this.PSN = new Person(); this.imagePath = Application.StartupPath + "/image"; if (Directory.Exists(this.imagePath) == false) Directory.CreateDirectory(this.imagePath); this.imagePath += "/"; Init(); this.F = new mPublic(); this.CFG = new Config(); this.MAPP = new Mapping(); this.RMap = CFG.rect_map_now; this.thKill = new Thread(new ThreadStart(AutoKill)); this.stopKill = true; this.ocrNumber = new OCR(); /* //初始化数字识别引擎 ocrNumber = new TesseractProcessor(); bInitN = ocrNumber.Init(@".\", "chi_sim", 3); if (!bInitN) { MessageBox.Show("智能识别引擎初始化失败!"); return; } ocrNumber.SetVariable("tessedit_char_whitelist", ".,2134567890 "); * */ //初始化汉字引擎 ocrText = new TesseractProcessor(); bInitT = ocrText.Init(@".\", "chi_sim", 3); if (!bInitT) { MessageBox.Show("智能识别引擎初始化失败!"); return; } ocrText.SetVariable("tessedit_char_blacklist", "fi蜘fi'£郜QWERTYUIOPASDFGHJKLZXCVBNM1234567890μqwertyuiopasdfghjklzxcvbnm"); }
/* private string GetNumber(Bitmap image) { UnCodebase ud = new UnCodebase(image); ud.DealMap2(); image = ud.bmpobj; pictureBox1.Image = image; if (!bInitT) { ocrNumber = new TesseractProcessor(); bool inited = ocrNumber.Init(@".\", "chi_sim", 3); if (!inited) { MessageBox.Show("智能识别引擎初始化失败!"); return ""; } ocrNumber.SetVariable("tessedit_char_whitelist", ".,3214567890 "); } ocrNumber.Clear(); ocrNumber.ClearAdaptiveClassifier(); string result = ocrNumber.Recognize(image); return result; } * */ private string GetChiSim(Bitmap image) { UnCodebase ud = new UnCodebase(image); ud.DealText(); image = ud.image; //pictureBox1.Image = image; textBox3.Text = ud.txt; if (!bInitT) { ocrText = new TesseractProcessor(); bInitT = ocrText.Init(@".\", "chi_sim", 3); if (!bInitT) { MessageBox.Show("智能识别引擎初始化失败!"); return ""; } ocrText.SetVariable("tessedit_char_blacklist", "1234567890μqwertyuiopasdfghjklzxcvbnm'I flU]M.H flO"); } ocrText.Clear(); ocrText.ClearAdaptiveClassifier(); string result = ocrText.Recognize(image); return result; }
private void DoOCR(IImage image) { ClearRenderingData(); string variable = "tessedit_pageseg_mode"; int storedOSD = 0; _ocrProcessor.GetIntVariable(variable, ref storedOSD); try { // Fully automatic page segmentation int fully_psm_auto = 3; _ocrProcessor.SetVariable(variable, fully_psm_auto.ToString()); ///// DEMO ONLY using (Image bitmap = ToImage(image)) { ///// DEMO ONLY _ocrProcessor.Clear(); _ocrProcessor.ClearAdaptiveClassifier(); BlockList blocks = _ocrProcessor.DetectBlocks(bitmap); this.UpdateImageViewer(blocks); ///// DEMO ONLY _ocrProcessor.Clear(); _ocrProcessor.ClearAdaptiveClassifier(); string result = _ocrProcessor.Apply(bitmap); List <Word> detectedWords = _ocrProcessor.RetriveResultDetail(); this.UpdateImageViewer(detectedWords); if (!string.IsNullOrEmpty(result)) { MessageBox.Show(result); } } } catch { } finally { _ocrProcessor.SetVariable(variable, storedOSD.ToString()); } //_ocrProcessor.Clear(); //_ocrProcessor.ClearAdaptiveClassifier(); //using (Image bitmap = ToImage(image)) //{ // //string result = _ocrProcessor.Apply(_fileName); // string result = _ocrProcessor.Apply(bitmap); // List<Word> detectedWords = _ocrProcessor.RetriveResultDetail(); // this.UpdateImageViewer(detectedWords); // if (!string.IsNullOrEmpty(result)) // MessageBox.Show(result); //} }
private void button2_Click(object sender, EventArgs e) { Image mg = Image.FromFile(Application.StartupPath + "\\1.jpg"); ////Image mg = Image.FromFile("e:\\test24.tif"); imageViewer1.Initialize(new OCRRenderingData(), new OCRAnalysisRender(imageViewer1)); imageViewer1.Image = mg; TesseractProcessor _ocrProcessor = null; _ocrProcessor = new TesseractProcessor(); _ocrProcessor.UseROI = false; imageViewer1.RectA = new Rectangle(0, 0, 0, 0); //string _tessData = "E:\\tesseract\\tesseract-ocr-dotnet-3.01\\tessdata\\"; string _tessData = Application.StartupPath + "\\tessdata\\"; string _lang = "eng"; int _ocrEngineMode = 3; bool status = _ocrProcessor.Init(_tessData, _lang, _ocrEngineMode); Console.WriteLine(string.Format("[DEBUG] Init status: {0}", status)); //string text = _ocrProcessor.Recognize(mg); //MessageBox.Show(text); ////**************************************************** converts 1bpp to 24bpp //Bitmap image = new Bitmap(@"e:\\test11.tif"); //Bitmap bitmap = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); //for (int i = 0; i < image.Width; i++) //{ // for (int j = 0; j < image.Height; j++) // { // Color temp = image.GetPixel(i, j); // bitmap.SetPixel(i, j, temp); // } //} //bitmap.Save(@"e:\\test24.tif", System.Drawing.Imaging.ImageFormat.Bmp); //Image mg1 = Image.FromFile("e:\\test24.tif"); ////****************************************************************************************************** string variable = "tessedit_pageseg_mode"; // Fully automatic page segmentation int fully_psm_auto = 3; _ocrProcessor.SetVariable(variable, fully_psm_auto.ToString()); ///// DEMO ONLY //_ocrProcessor.Clear(); //_ocrProcessor.ClearAdaptiveClassifier(); //BlockList blocks = _ocrProcessor.DetectBlocks(mg); //this.UpdateImageViewer(blocks); _ocrProcessor.Clear(); _ocrProcessor.ClearAdaptiveClassifier(); string result = _ocrProcessor.Apply(mg); List <Word> detectedWords = _ocrProcessor.RetriveResultDetail(); this.UpdateImageViewer(detectedWords); //MessageBox.Show(result); button3.Enabled = true; button4.Enabled = true; }