private void button1_Click(object sender, EventArgs e) { var ApiKey = "Sd5OH4BMmAqwKZvG6O56oqed"; var SecretKey = "UX70FFACkOGb0at4q3ljzkYTaKC87vgG"; var client = new Baidu.Aip.Ocr.Ocr(ApiKey, SecretKey); // var tuPian = @"F:\Project\C#\百度通用文字识别\Baidu-OCR-API-master\Baidu-OCR-API-master\test images\chi-scan-300dpi - TOO LARGE.jpg"; // var image = File.ReadAllBytes(tuPian); var image = ImageToBase64(pictureBox.BackgroundImage, System.Drawing.Imaging.ImageFormat.Jpeg); if (image == null) { MessageBox.Show("请先加载图片"); return; } // 如果有可选参数 var options = new Dictionary <string, object> { { "language_type", "CHN_ENG" }, { "detect_direction", "true" }, { "detect_language", "true" }, { "probability", "true" } }; // 通用文字识别 var result = client.GeneralBasic(image, options); OcrResult ocrResult = result.ToObject <OcrResult>(); foreach (var item in ocrResult.words_result) { if (cbLine.Checked) { txtResult.AppendText(item.words + "\n"); } else { txtResult.AppendText(item.words); } } }
/// <summary> /// 车牌识别 /// </summary> public void LicensePlateDemo() { var ApiKey = "Sd5OH4BMmAqwKZvG6O56oqed"; var SecretKey = "UX70FFACkOGb0at4q3ljzkYTaKC87vgG"; var client = new Baidu.Aip.Ocr.Ocr(ApiKey, SecretKey); var image = ImageToBase64(pictureBox.BackgroundImage, System.Drawing.Imaging.ImageFormat.Jpeg); if (image == null) { MessageBox.Show("请先加载图片"); return; } // 调用车牌识别,可能会抛出网络等异常,请使用try/catch捕获 var result = client.LicensePlate(image); Console.WriteLine(result); // 如果有可选参数 var options = new Dictionary <string, object> { { "multi_detect", "true" } }; // 带参数调用车牌识别 result = client.LicensePlate(image, options); OcrResult ocrResult = result.ToObject <OcrResult>(); if (ocrResult.words_result == null) { MessageBox.Show("未检测到车牌"); return; } foreach (var item in ocrResult.words_result) { txtResult.AppendText(item.color + "牌" + item.number + "\n"); } // Console.WriteLine(result); }