Exemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //抓取key
            var VisionApiKey = textBoxkey.Text.Trim();
            //讀取圖檔
            var openDlg = new OpenFileDialog();

            //圖檔過濾類型
            openDlg.Filter = "JPEG Image(*.jpg)|*.jpg|*.png|*.png";
            //沒選檔案
            if (openDlg.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            //取得選擇的檔案名稱
            string filePath = openDlg.FileName;

            //顯示原始圖片
            var FileStream = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);

            pictureBox1.Image = System.Drawing.Image.FromStream(FileStream);
            FileStream.Close();

            //OCR OcrResults
            OcrResults OcrResults;
            //建立VisionServiceClient
            var visionClient = new Microsoft.ProjectOxford.Vision.VisionServiceClient(VisionApiKey);

            using (var fs = new FileStream(filePath, FileMode.Open))
            {
                this.textBox.Text = "辨識中...";
                //以繁體中文辨識
                OcrResults        = visionClient.RecognizeTextAsync(fs, LanguageCodes.AutoDetect).Result;
                this.textBox.Text = "";
            }

            this.textBox.Text = "";
            //抓取每一區塊的辨識結果
            foreach (var Region in OcrResults.Regions)
            {
                //抓取每一行
                foreach (var line in Region.Lines)
                {
                    //抓取每一個字
                    foreach (var Word in line.Words)
                    {
                        //顯示辨識結果
                        this.textBox.Text += Word.Text;
                    }
                    //加換行
                    this.textBox.Text += "\n";
                }
            }
        }
Exemplo n.º 2
0
        //this code is revised from http://studyhost.blogspot.com/2016/08/microsoft-cognitive-services-2-vision.html
        public static string GetOcrResults(string subscriptionKey, string apiRoot, byte[] filebody, string languageCodes)
        {
            string result = string.Empty;

            OcrResults OcrResults = default(OcrResults);

            //建立VisionServiceClient
            var visionClient = new Microsoft.ProjectOxford.Vision.VisionServiceClient(subscriptionKey, apiRoot);

            using (var ms = new MemoryStream(filebody, true))
            {
                //以繁體中文辨識
                //OcrResults = visionClient.RecognizeTextAsync(ms, LanguageCodes.AutoDetect).Result;
                OcrResults = visionClient.RecognizeTextAsync(ms, languageCodes).Result;
            }

            //抓取每一區塊的辨識結果
            foreach (var Region in OcrResults.Regions)
            {
                //抓取每一行
                foreach (var line_loopVariable in Region.Lines)
                {
                    var     line  = line_loopVariable;
                    dynamic aline = "";
                    //抓取每一個字
                    foreach (var Word_loopVariable in line.Words)
                    {
                        var Word = Word_loopVariable;
                        //顯示辨識結果
                        aline += Word.Text;
                    }

                    //加換行
                    result += aline + "\n";
                }
            }

            return(result);
        }
        public async Task <string> GetHandwrittenTextFromImage(Stream picture)
        {
            try
            {
                var result = await _visionClient.RecognizeTextAsync(picture, "en");

                //return result.Regions[0].Lines.();
                if (result.Regions.Length > 0)
                {
                    if (result.Regions[0].Lines.Length > 0)
                    {
                        if (result.Regions[0].Lines[0].Words.Length > 0)
                        {
                            return(result.Regions[0].Lines[0].Words[0].Text);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
            }
            return("ERROR Recognizing");
        }