private void Button1_Click(object sender, EventArgs e) { OpenFileDialog d = new OpenFileDialog(); if (d.ShowDialog() != DialogResult.OK) { return; } string filePath = d.FileName; //OCR OcrResults OcrResults OcrResults = default(OcrResults); //建立VisionServiceClient var visionClient = new Microsoft.ProjectOxford.Vision.VisionServiceClient( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!換成你的key!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ", "https://southeastasia.api.cognitive.microsoft.com/vision/v1.0"); using (var fs = new FileStream(filePath, FileMode.Open)) { // this.textBox.Text = "辨識中..."; //以繁體中文辨識 // this.textBox.Text = ""; OcrResults = visionClient.RecognizeTextAsync(fs, LanguageCodes.AutoDetect).Result; } string 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"; } } this.TextBox1.Text = result; //load picture this.PictureBox1.Image = Image.FromFile(filePath); }
private void button3_Click(object sender, EventArgs e) { var msg = ""; //取得圖片檔案FileStream byte[] file = System.IO.File.ReadAllBytes(this.textBox1.Text); Stream MemStream1 = new MemoryStream(file); Stream MemStream2 = new MemoryStream(file); //繪圖用 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(MemStream2); Graphics g = Graphics.FromImage(bmp); //ComputerVision instance var visionClient = new Microsoft.ProjectOxford.Vision.VisionServiceClient( ComputerVisionServiceKey, ComputerVisionServiceEndpoint); //分析用 using (MemStream1) { //分析圖片 var OcrResults = visionClient.RecognizeTextAsync( MemStream1, LanguageCodes.AutoDetect).Result; //抓取每一區塊的辨識結果 foreach (var Region in OcrResults.Regions) { //抓取每一行 foreach (var line in Region.Lines) { //畫框 g.DrawRectangle( new Pen(Brushes.Red, 3), new System.Drawing.Rectangle(line.Rectangle.Left, line.Rectangle.Top, line.Rectangle.Width, line.Rectangle.Height)); var aline = ""; //抓取每一個字 foreach (var Word in line.Words) { //顯示辨識結果 aline += Word.Text; } //加換行 msg += aline + " , "; } } } this.textBox2.Text = msg; this.pictureBox2.Image = bmp; }
public static OcrResults AnalyzeText(Bitmap sourceImage) { Microsoft.ProjectOxford.Vision.VisionServiceClient visionServiceClient = ComputerVisionService.GetClient(); using (MemoryStream memoryStream = new MemoryStream()) { sourceImage.SaveAsPng(memoryStream); memoryStream.Position = 0; Console.WriteLine("Calling VisionServiceClient.RecognizeTextAsyncs()..."); return(visionServiceClient.RecognizeTextAsync(memoryStream).GetAwaiter().GetResult()); } }
private async Task <LiveAnalyzeResult> TextDetectAnalyzeFunction(VideoFrame frame) { // Encode image. var jpg = frame.Image.ToMemoryStream(".jpg", s_jpegParams); var detectResult = await _visionClient.RecognizeTextAsync(jpg, "unk"); if (detectResult.Regions.Any()) { return(new LiveAnalyzeResult() { Regions = detectResult.Regions }); } return(default(LiveAnalyzeResult)); }