Exemplo n.º 1
0
        private string Ocr(Bitmap image) //OCR Metotları
        {
            m_tesseract.Clear();
            m_tesseract.ClearAdaptiveClassifier();

            return(m_tesseract.Apply(image));
        }
Exemplo n.º 2
0
Arquivo: http.cs Projeto: antaluqi/web
        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));
        }
Exemplo n.º 3
0
        public static string Ocr(Bitmap image_s, bool isFull, TesseractProcessor full_tesseract, TesseractProcessor num_tesseract, TesseractProcessor ch_tesseract, bool isNum = false)
        {
            Image <Gray, byte> image = new Image <Gray, byte>(image_s);

            while (true)
            {
                if ((double)CvInvoke.cvCountNonZero((IntPtr)((UnmanagedObject)image)) / (double)(image.Width * image.Height) <= 0.5)
                {
                    image = image.Dilate(2);
                }
                else
                {
                    break;
                }
            }
            Bitmap             bitmap             = image.ToBitmap();
            TesseractProcessor tesseractProcessor = !isFull ? (!isNum ? ch_tesseract : num_tesseract) : full_tesseract;
            int num = 0;

            tesseractProcessor.Clear();
            tesseractProcessor.ClearAdaptiveClassifier();
            string str = tesseractProcessor.Apply((Image)bitmap);

            while (str.Length > 3)
            {
                bitmap = new Image <Gray, byte>(bitmap).Erode(2).ToBitmap();
                tesseractProcessor.Clear();
                tesseractProcessor.ClearAdaptiveClassifier();
                str = tesseractProcessor.Apply((Image)bitmap);
                ++num;
                if (num > 10)
                {
                    str = "";
                    break;
                }
            }
            return(str);
        }
Exemplo n.º 4
0
    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);
    }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            const string language = "eng";
            const string TessractData = @"C:\Users\Salman\Documents\GitHub\project-pinnacle\Research\Salman\OCRTest\tessdata\";
            const string Path = @"C:\Users\Salman\NUS\EE4001\Research\trial.tif";

            TesseractProcessor processor = new TesseractProcessor();
            processor.DoMonitor = true;

            System.Drawing.Image bmp = System.Drawing.Image.FromFile(Path);

            bool x = processor.Init(TessractData, language, (int)eOcrEngineMode.TESSERACT_CUBE_COMBINED);

            processor.Clear();
            processor.ClearAdaptiveClassifier();
            string result = processor.Apply(bmp);
            List<Word> detectedWords = processor.RetriveResultDetail();
            int a = 2;
            //this.UpdateImageViewer(detectedWords);

            //using (var bmp = Bitmap.FromFile(Path) as Bitmap)
            //{
            //    var success = processor.Init(TessractData, language, (int)eOcrEngineMode.DEFAULT);
            //    if (!success)
            //    {
            //        Console.WriteLine("Failed to initialize tesseract.");
            //    }
            //    else
            //    {
            //        string text = processor.Recognize(bmp);
            //        Console.WriteLine("Text:");
            //        Console.WriteLine("*****************************");
            //        Console.WriteLine(text);
            //        Console.WriteLine("*****************************");
            //    }
            //}

            //Console.WriteLine("Press any key to exit.");
            //Console.ReadKey();
        }
Exemplo n.º 6
0
 private string Ocr(Image image)
 {
     m_tesseract.Clear();
     m_tesseract.ClearAdaptiveClassifier();
     return(m_tesseract.Apply(image));
 }
Exemplo n.º 7
0
        private void Reconize(string link, out Image hinhbienso, out string bienso, out string bienso_text)
        {
            for (int i = 0; i < box.Length; i++)
            {
                this.Controls.Remove(box[i]);
            }

            hinhbienso  = null;
            bienso      = "";
            bienso_text = "";
            ProcessImage(link);
            if (PlateImagesList.Count != 0)
            {
                Image <Bgr, byte> src = new Image <Bgr, byte>(PlateImagesList[0].ToBitmap());
                Bitmap            grayframe;
                FindContours      con = new FindContours();
                Bitmap            color;
                int c = con.IdentifyContours(src.ToBitmap(), 50, false, out grayframe, out color, out listRect);
                pictureBox_BiensoVAO.Image = color;
                hinhbienso = Plate_Draw;
                pictureBox_BiensoVAO.Image = grayframe;
                Image <Gray, byte> dst = new Image <Gray, byte>(grayframe);
                grayframe = dst.ToBitmap();
                string zz = "";
                // lọc và sắp xếp số
                List <Bitmap>    bmp = new List <Bitmap>();
                List <int>       erode = new List <int>();
                List <Rectangle> up = new List <Rectangle>();
                List <Rectangle> dow = new List <Rectangle>();
                int  up_y = 0, dow_y = 0;
                bool flag_up = false;

                int di = 0;

                if (listRect == null)
                {
                    return;
                }

                for (int i = 0; i < listRect.Count; i++)
                {
                    Bitmap ch  = grayframe.Clone(listRect[i], grayframe.PixelFormat);
                    int    cou = 0;
                    full_tesseract.Clear();
                    full_tesseract.ClearAdaptiveClassifier();
                    string temp = full_tesseract.Apply(ch);
                    while (temp.Length > 3)
                    {
                        Image <Gray, byte> temp2 = new Image <Gray, byte>(ch);
                        temp2 = temp2.Erode(2);
                        ch    = temp2.ToBitmap();
                        full_tesseract.Clear();
                        full_tesseract.ClearAdaptiveClassifier();
                        temp = full_tesseract.Apply(ch);
                        cou++;
                        if (cou > 10)
                        {
                            listRect.RemoveAt(i);
                            i--;
                            di = 0;
                            break;
                        }
                        di = cou;
                    }
                }

                for (int i = 0; i < listRect.Count; i++)
                {
                    for (int j = i; j < listRect.Count; j++)
                    {
                        if (listRect[i].Y > listRect[j].Y + 100)
                        {
                            flag_up = true;
                            up_y    = listRect[j].Y;
                            dow_y   = listRect[i].Y;
                            break;
                        }
                        else if (listRect[j].Y > listRect[i].Y + 100)
                        {
                            flag_up = true;
                            up_y    = listRect[i].Y;
                            dow_y   = listRect[j].Y;
                            break;
                        }
                        if (flag_up == true)
                        {
                            break;
                        }
                    }
                }

                for (int i = 0; i < listRect.Count; i++)
                {
                    if (listRect[i].Y <up_y + 50 && listRect[i].Y> up_y - 50)
                    {
                        up.Add(listRect[i]);
                    }
                    else if (listRect[i].Y <dow_y + 50 && listRect[i].Y> dow_y - 50)
                    {
                        dow.Add(listRect[i]);
                    }
                }

                if (flag_up == false)
                {
                    dow = listRect;
                }

                for (int i = 0; i < up.Count; i++)
                {
                    for (int j = i; j < up.Count; j++)
                    {
                        if (up[i].X > up[j].X)
                        {
                            Rectangle w = up[i];
                            up[i] = up[j];
                            up[j] = w;
                        }
                    }
                }
                for (int i = 0; i < dow.Count; i++)
                {
                    for (int j = i; j < dow.Count; j++)
                    {
                        if (dow[i].X > dow[j].X)
                        {
                            Rectangle w = dow[i];
                            dow[i] = dow[j];
                            dow[j] = w;
                        }
                    }
                }

                int x   = 12;
                int c_x = 0;

                for (int i = 0; i < up.Count; i++)
                {
                    Bitmap ch = grayframe.Clone(up[i], grayframe.PixelFormat);
                    Bitmap o  = ch;
                    string temp;
                    if (i < 2)
                    {
                        temp = Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, true); // nhan dien so
                    }
                    else
                    {
                        temp = Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, false); // nhan dien chu
                    }

                    zz += temp;
                    box[i].Location = new Point(x + i * 50, 290);
                    box[i].Size     = new Size(50, 100);
                    box[i].SizeMode = PictureBoxSizeMode.StretchImage;
                    box[i].Image    = ch;
                    box[i].Update();
                    //this.Controls.Add(box[i]);
                    c_x++;
                }
                zz += "\r\n";
                for (int i = 0; i < dow.Count; i++)
                {
                    Bitmap ch = grayframe.Clone(dow[i], grayframe.PixelFormat);
                    //ch = con.Erodetion(ch);
                    string temp = Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, true); // nhan dien so
                    zz += temp;
                    box[i + c_x].Location = new Point(x + i * 50, 390);
                    box[i + c_x].Size     = new Size(50, 100);
                    box[i + c_x].SizeMode = PictureBoxSizeMode.StretchImage;
                    box[i + c_x].Image    = ch;
                    box[i + c_x].Update();
                }
                bienso      = zz.Replace("\n", "");
                bienso      = bienso.Replace("\r", "");
                bienso_text = zz;
            }
        }
Exemplo n.º 8
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter           = "Image (*.bmp; *.jpg; *.jpeg; *.png) |*.bmp; *.jpg; *.jpeg; *.png|All files (*.*)|*.*||";
                dlg.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory + "\\ImageTest";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }
                string startupPath = dlg.FileName;

                ProcessImage(startupPath);
                if (PlateImagesList.Count != 0)
                {
                    Image <Bgr, byte> src = new Image <Bgr, byte>(PlateImagesList[0].ToBitmap());
                    Bitmap            grayframe;
                    Bitmap            color;
                    //Tìm biên
                    int c = clsBSoft.IdentifyContours(src.ToBitmap(), 50, false, out grayframe, out color, out listRect);
                    pic1.Image = color;
                    pic2.Image = grayframe;
                    Image <Gray, byte> dst = new Image <Gray, byte>(grayframe);
                    grayframe = dst.ToBitmap();
                    //tạo chuỗi rỗng để hứng
                    string upp  = "";
                    string down = "";

                    // lọc và sắp xếp số
                    List <Bitmap> bmp   = new List <Bitmap>();
                    List <int>    erode = new List <int>();
                    List <System.Drawing.Rectangle> up = new List <System.Drawing.Rectangle>();
                    List <System.Drawing.Rectangle> dow = new List <System.Drawing.Rectangle>();
                    int  up_y = 0, dow_y = 0;
                    bool flag_up = false;

                    int di = 0;

                    if (listRect == null)
                    {
                        return;
                    }

                    for (int i = 0; i < listRect.Count; i++)
                    {
                        Bitmap ch  = grayframe.Clone(listRect[i], grayframe.PixelFormat);
                        int    cou = 0;
                        full_tesseract.Clear();
                        full_tesseract.ClearAdaptiveClassifier();
                        string temp = full_tesseract.Apply(ch);
                        while (temp.Length > 3)
                        {
                            Image <Gray, byte> temp2 = new Image <Gray, byte>(ch);
                            temp2 = temp2.Erode(2);
                            ch    = temp2.ToBitmap();
                            full_tesseract.Clear();
                            full_tesseract.ClearAdaptiveClassifier();
                            temp = full_tesseract.Apply(ch);
                            cou++;
                            if (cou > 10)
                            {
                                listRect.RemoveAt(i);
                                i--;
                                di = 0;
                                break;
                            }
                            di = cou;
                        }
                    }

                    for (int i = 0; i < listRect.Count; i++)
                    {
                        for (int j = i; j < listRect.Count; j++)
                        {
                            if (listRect[i].Y > listRect[j].Y + 100)
                            {
                                flag_up = true;
                                up_y    = listRect[j].Y;
                                dow_y   = listRect[i].Y;
                                break;
                            }
                            else if (listRect[j].Y > listRect[i].Y + 100)
                            {
                                flag_up = true;
                                up_y    = listRect[i].Y;
                                dow_y   = listRect[j].Y;
                                break;
                            }
                            if (flag_up == true)
                            {
                                break;
                            }
                        }
                    }

                    for (int i = 0; i < listRect.Count; i++)
                    {
                        if (listRect[i].Y <up_y + 50 && listRect[i].Y> up_y - 50)
                        {
                            up.Add(listRect[i]);
                        }
                        else if (listRect[i].Y <dow_y + 50 && listRect[i].Y> dow_y - 50)
                        {
                            dow.Add(listRect[i]);
                        }
                    }

                    if (flag_up == false)
                    {
                        dow = listRect;
                    }

                    for (int i = 0; i < up.Count; i++)
                    {
                        for (int j = i; j < up.Count; j++)
                        {
                            if (up[i].X > up[j].X)
                            {
                                System.Drawing.Rectangle w = up[i];
                                up[i] = up[j];
                                up[j] = w;
                            }
                        }
                    }
                    for (int i = 0; i < dow.Count; i++)
                    {
                        for (int j = i; j < dow.Count; j++)
                        {
                            if (dow[i].X > dow[j].X)
                            {
                                System.Drawing.Rectangle w = dow[i];
                                dow[i] = dow[j];
                                dow[j] = w;
                            }
                        }
                    }

                    int x = 0;
                    int c_x = 0;

                    for (int i = 0; i < up.Count; i++)
                    {
                        Bitmap ch = grayframe.Clone(up[i], grayframe.PixelFormat);
                        Bitmap o  = ch;
                        string temp;
                        if (i < 2)
                        {
                            temp = clsBSoft.Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, true); // nhan dien so
                        }
                        else
                        {
                            temp = clsBSoft.Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, false); // nhan dien chu
                        }

                        upp            += temp;
                        box[i].Location = new System.Drawing.Point(x + i * 50, 0);
                        box[i].Size     = new System.Drawing.Size(50, 100);
                        box[i].SizeMode = PictureBoxSizeMode.StretchImage;
                        box[i].Image    = ch;
                        c_x++;
                    }

                    for (int i = 0; i < dow.Count; i++)
                    {
                        Bitmap ch   = grayframe.Clone(dow[i], grayframe.PixelFormat);
                        string temp = clsBSoft.Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, true); // nhan dien so

                        down += temp;
                        box[i + c_x].Location = new System.Drawing.Point(x + i * 50, 100);
                        box[i + c_x].Size     = new System.Drawing.Size(50, 100);
                        box[i + c_x].SizeMode = PictureBoxSizeMode.StretchImage;
                        box[i + c_x].Image    = ch;
                    }
                    tblNhandien.Text = upp.Replace("\n", "") + "\n" + down.Replace("\n", "");
                    tbNhandien.Text  = upp.Replace("\n", "") + "_" + down.Replace("\n", "");
                }
            }
            catch (Exception)
            {
                System.Windows.MessageBox.Show("tràn RAM");
            }
        }
Exemplo n.º 9
0
        private void metroButton2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter           = "Image (*.bmp; *.jpg; *.jpeg; *.png) |*.bmp; *.jpg; *.jpeg; *.png|All files (*.*)|*.*||";
            dlg.InitialDirectory = Application.StartupPath + "\\ImageTest";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            string startupPath = dlg.FileName;

            ProcessImage1(startupPath);
            if (PlateImagesList.Count != 0)
            {
                Image <Bgr, byte> src = new Image <Bgr, byte>(PlateImagesList[0].ToBitmap());
                Bitmap            grayframe;
                Bitmap            color;
                int c = clsBSoft.IdentifyContours(src.ToBitmap(), 50, false, out grayframe, out color, out listRect);
                pictureBox1.Image = color;
                pictureBox2.Image = grayframe;
                textBox2.Text     = c.ToString();
                Image <Gray, byte> dst = new Image <Gray, byte>(grayframe);
                grayframe = dst.ToBitmap();
                string zz = "";

                // lọc và sắp xếp số
                List <Bitmap>    bmp = new List <Bitmap>();
                List <int>       erode = new List <int>();
                List <Rectangle> up = new List <Rectangle>();
                List <Rectangle> dow = new List <Rectangle>();
                int  up_y = 0, dow_y = 0;
                bool flag_up = false;

                int di = 0;

                if (listRect == null)
                {
                    return;
                }

                for (int i = 0; i < listRect.Count; i++)
                {
                    Bitmap ch  = grayframe.Clone(listRect[i], grayframe.PixelFormat);
                    int    cou = 0;
                    full_tesseract.Clear();
                    full_tesseract.ClearAdaptiveClassifier();
                    string temp = full_tesseract.Apply(ch);
                    while (temp.Length > 3)
                    {
                        Image <Gray, byte> temp2 = new Image <Gray, byte>(ch);
                        temp2 = temp2.Erode(2);
                        ch    = temp2.ToBitmap();
                        full_tesseract.Clear();
                        full_tesseract.ClearAdaptiveClassifier();
                        temp = full_tesseract.Apply(ch);
                        cou++;
                        if (cou > 10)
                        {
                            listRect.RemoveAt(i);
                            i--;
                            di = 0;
                            break;
                        }
                        di = cou;
                    }
                }

                for (int i = 0; i < listRect.Count; i++)
                {
                    for (int j = i; j < listRect.Count; j++)
                    {
                        if (listRect[i].Y > listRect[j].Y + 100)
                        {
                            flag_up = true;
                            up_y    = listRect[j].Y;
                            dow_y   = listRect[i].Y;
                            break;
                        }
                        else if (listRect[j].Y > listRect[i].Y + 100)
                        {
                            flag_up = true;
                            up_y    = listRect[i].Y;
                            dow_y   = listRect[j].Y;
                            break;
                        }
                        if (flag_up == true)
                        {
                            break;
                        }
                    }
                }

                for (int i = 0; i < listRect.Count; i++)
                {
                    if (listRect[i].Y <up_y + 50 && listRect[i].Y> up_y - 50)
                    {
                        up.Add(listRect[i]);
                    }
                    else if (listRect[i].Y <dow_y + 50 && listRect[i].Y> dow_y - 50)
                    {
                        dow.Add(listRect[i]);
                    }
                }

                if (flag_up == false)
                {
                    dow = listRect;
                }

                for (int i = 0; i < up.Count; i++)
                {
                    for (int j = i; j < up.Count; j++)
                    {
                        if (up[i].X > up[j].X)
                        {
                            Rectangle w = up[i];
                            up[i] = up[j];
                            up[j] = w;
                        }
                    }
                }
                for (int i = 0; i < dow.Count; i++)
                {
                    for (int j = i; j < dow.Count; j++)
                    {
                        if (dow[i].X > dow[j].X)
                        {
                            Rectangle w = dow[i];
                            dow[i] = dow[j];
                            dow[j] = w;
                        }
                    }
                }

                int x   = 0;
                int c_x = 0;

                for (int i = 0; i < up.Count; i++)
                {
                    Bitmap ch = grayframe.Clone(up[i], grayframe.PixelFormat);
                    Bitmap o  = ch;
                    string temp;
                    if (i < 2)
                    {
                        temp = clsBSoft.Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, true);                         // nhan dien so
                    }
                    else
                    {
                        temp = clsBSoft.Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, false);                         // nhan dien chu
                    }

                    zz += temp;
                    box[i].Location = new Point(x + i * 50, 0);
                    box[i].Size     = new Size(50, 100);
                    box[i].SizeMode = PictureBoxSizeMode.StretchImage;
                    box[i].Image    = ch;
                    panel1.Controls.Add(box[i]);
                    c_x++;
                }
                zz += "\r\n";
                for (int i = 0; i < dow.Count; i++)
                {
                    Bitmap ch   = grayframe.Clone(dow[i], grayframe.PixelFormat);
                    string temp = clsBSoft.Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, true); // nhan dien so

                    zz += temp;
                    box[i + c_x].Location = new Point(x + i * 50, 100);
                    box[i + c_x].Size     = new Size(50, 100);
                    box[i + c_x].SizeMode = PictureBoxSizeMode.StretchImage;
                    box[i + c_x].Image    = ch;
                    panel1.Controls.Add(box[i + c_x]);
                }
                textBox1.Text = zz;
            }
        }
Exemplo n.º 10
0
        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);
            //}
        }
Exemplo n.º 11
0
        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                if (imgInput == null)
                {
                    throw new Exception("Chon anh hoac chup anh !");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            Image <Bgr, byte>  imgInputClone = imgInput.Clone();
            Image <Gray, byte> grayInput     = imgInput.Convert <Gray, byte>().Clone();

            var faces = grayInput.DetectHaarCascade(new HaarCascade(Application.StartupPath + "\\vn2best.xml"), 1.1, 8, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(0, 0))[0];

            foreach (var face in faces)
            {
                imgInput.ROI = face.rect;

                imgInputClone.Draw(face.rect, new Bgr(Color.Blue), 2);
                pictureBox1.Image = imgInputClone.Bitmap;
                pictureBox2.Image = imgInput.Bitmap;
            }

            Image <Bgr, byte> color1 = imgInput.Resize(450, 300, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
            Bitmap            src    = color1.ToBitmap();
            Bitmap            grayframe;

            FindContours     con = new FindContours();
            Bitmap           color;
            List <Rectangle> listR = new List <Rectangle>();
            int c = con.IdentifyContours(src, 50, false, out grayframe, out color, out listRect);

            pictureBox2.Image    = color;
            IF.pictureBox1.Image = grayframe;
            IF.pictureBox2.Image = imgInputClone.Bitmap;

            Image <Gray, byte> dst = new Image <Gray, byte>(grayframe);

            grayframe = dst.ToBitmap();
            string zz    = "";
            string sothe = "";
            // lọc và sắp xếp số
            List <Bitmap>    bmp = new List <Bitmap>();
            List <int>       erode = new List <int>();
            List <Rectangle> up = new List <Rectangle>();
            List <Rectangle> dow = new List <Rectangle>();
            int  up_y = 0, dow_y = 0;
            bool flag_up = false;

            int di = 0;

            if (listRect == null)
            {
                return;
            }

            for (int i = 0; i < listRect.Count; i++)
            {
                Bitmap ch  = grayframe.Clone(listRect[i], grayframe.PixelFormat);
                int    cou = 0;
                full_tesseract.Clear();
                full_tesseract.ClearAdaptiveClassifier();
                string temp = full_tesseract.Apply(ch);
                while (temp.Length > 3)
                {
                    Image <Gray, byte> temp2 = new Image <Gray, byte>(ch);
                    temp2 = temp2.Erode(2);
                    ch    = temp2.ToBitmap();
                    full_tesseract.Clear();
                    full_tesseract.ClearAdaptiveClassifier();
                    temp = full_tesseract.Apply(ch);
                    cou++;
                    if (cou > 10)
                    {
                        listRect.RemoveAt(i);
                        i--;
                        di = 0;
                        break;
                    }
                    di = cou;
                }
            }

            for (int i = 0; i < listRect.Count; i++)
            {
                for (int j = i; j < listRect.Count; j++)
                {
                    if (listRect[i].Y > listRect[j].Y + 100)
                    {
                        flag_up = true;
                        up_y    = listRect[j].Y;
                        dow_y   = listRect[i].Y;
                        break;
                    }
                    else if (listRect[j].Y > listRect[i].Y + 100)
                    {
                        flag_up = true;
                        up_y    = listRect[i].Y;
                        dow_y   = listRect[j].Y;
                        break;
                    }
                    if (flag_up == true)
                    {
                        break;
                    }
                }
            }

            for (int i = 0; i < listRect.Count; i++)
            {
                if (listRect[i].Y <up_y + 50 && listRect[i].Y> up_y - 50)
                {
                    up.Add(listRect[i]);
                }
                else if (listRect[i].Y <dow_y + 50 && listRect[i].Y> dow_y - 50)
                {
                    dow.Add(listRect[i]);
                }
            }

            if (flag_up == false)
            {
                dow = listRect;
            }

            for (int i = 0; i < up.Count; i++)
            {
                for (int j = i; j < up.Count; j++)
                {
                    if (up[i].X > up[j].X)
                    {
                        Rectangle w = up[i];
                        up[i] = up[j];
                        up[j] = w;
                    }
                }
            }
            for (int i = 0; i < dow.Count; i++)
            {
                for (int j = i; j < dow.Count; j++)
                {
                    if (dow[i].X > dow[j].X)
                    {
                        Rectangle w = dow[i];
                        dow[i] = dow[j];
                        dow[j] = w;
                    }
                }
            }

            int x   = 12;
            int c_x = 0;

            for (int i = 0; i < up.Count; i++)
            {
                Bitmap ch = grayframe.Clone(up[i], grayframe.PixelFormat);
                Bitmap o  = ch;
                //ch = con.Erodetion(ch);
                string temp;
                if (i < 2)
                {
                    temp = Ocr(ch, false, true); // nhan dien so
                }
                else
                {
                    temp = Ocr(ch, false, false);// nhan dien chu
                }

                zz += temp; c_x++; sothe += temp;
            }

            zz += " - ";
            for (int i = 0; i < dow.Count; i++)
            {
                Bitmap ch = grayframe.Clone(dow[i], grayframe.PixelFormat);
                //ch = con.Erodetion(ch);
                string temp = Ocr(ch, false, true); // nhan dien so
                zz += temp;
            }
            sothe = dateTimePicker1.Value.Second + dateTimePicker1.Value.Hour + " - " + sothe;
            if (zz == " - ")
            {
                textBox1.Text = "khong nhan dien duoc bien so";
            }
            else
            {
                textBox1.Text = zz;
            }
            IF.textBox6.Text = zz;
            textBox3.Text    = sothe;
            textBox4.Text    = label18.Text + " - " + dateTimePicker1.Text;
        }
Exemplo n.º 12
0
        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;
        }