百度AI的图片识别文字

百度AI的图片识别文字_第1张图片
话不多说上代码
private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = “图片(.png;.jpg;.bmp;.jpeg)|.png;.jpg;.bmp;.jpeg”;
if (dialog.ShowDialog() == DialogResult.OK)
{
GeneralBasicDemo(dialog.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show(“报错,原因:” + ex.Message, “错误”, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

    }
 
    public void GeneralBasicDemo(string  url)
    {
        // 设置APPID/AK/SK
        var APP_ID = "你的 App ID";
            var API_KEY = "你的 Api Key";
           var SECRET_KEY = "你的 Secret Key";

        var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);
        client.Timeout = 60000;  // 修改超时时间

        var image = File.ReadAllBytes(url);
        // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获
        var result = client.GeneralBasic(image);
        Console.WriteLine(result);
        // 如果有可选参数
        var options = new Dictionary{
    {"language_type", "CHN_ENG"},
    {"detect_direction", "true"},
    {"detect_language", "true"},
    {"probability", "true"}
};
        // 带参数调用通用文字识别, 图片参数为本地图片
        result = client.GeneralBasic(image, options);
        string strcs = "";
        foreach (var line in result["words_result"])
        {
            strcs += line["words"] + "\n";
        }
        richTextBox1.Text = strcs.ToString();
       
       
    }

你可能感兴趣的:(C#)