識別圖片中文字(百度AI)

   這個是百度官方的文檔         https://ai.baidu.com/docs#/OCR-API/topjson

   通用的文字識別,若是是其餘的含生僻字/含位置信息的版本,請參考官方的文檔,只須要在請求時發送不一樣的參數便可app

   根據文檔簡單的使用通常處理程序完成 百度 中文字識別的功能,下面是主方法:post

    private static String clientId = "**************"; // 百度雲中開通對應服務應用的 API Key 建議開通應用的時候多選服務
    // 百度雲中開通對應服務應用的 Secret Key
    private static String clientSecret = "***************";//注意不要加空格

    public void ProcessRequest(HttpContext context) {
        context.Response.ContentType = "text/plain";string token_ = getAccessToken();
        string return_font = general(token_, "123.jpg");
        //string json = JsonConvert.SerializeObject(return_font);
        context.Response.Write(return_font);
    }

    /// <summary>
    /// 獲取token
    /// </summary>
    /// <returns></returns>
    public static String getAccessToken()
    {
        String authHost = "https://aip.baidubce.com/oauth/2.0/token";
        HttpClient client = new HttpClient();
        List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
        paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
        paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
        paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));

        HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
        String result = response.Content.ReadAsStringAsync().Result;
        string token_ = result.Split(',')[0].Split(':')[1];
        token_ = token_.Substring(1, token_.Length-2);

        return token_;
    }
/// <summary>
    /// 獲取圖片中的文字
    /// </summary>
    /// <param name="token"></param>
    /// <param name="image_path">圖片路徑</param>
    /// <returns></returns>
    public static string general(string token,string image_path)
    {
        string strbaser64 = base64(image_path);//將圖片轉爲base64

        string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + token;
        Encoding encoding = Encoding.Default;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
        request.Method = "post";
        request.ContentType = "application/x-www-form-urlencoded";
        request.KeepAlive = true;
        String str = "image=" + HttpUtility.UrlEncode(strbaser64);
        byte[] buffer = encoding.GetBytes(str);
        request.ContentLength = buffer.Length;
        request.GetRequestStream().Write(buffer, 0, buffer.Length);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        string result = reader.ReadToEnd();
        return result;
    }

    /// <summary>
    /// 將圖片轉爲base64
    /// </summary>
    /// <param name="image_name"></param>
    /// <returns></returns>
    public static string base64(string image_name) {
        FileInfo file = new FileInfo(image_name);
        var stream = file.OpenRead();
        byte[] buffer = new byte[file.Length];
        //讀取圖片字節流
        stream.Read(buffer, 0, Convert.ToInt32(file.Length));
        return Convert.ToBase64String(buffer);

    }

     其中兩個公共變量分別是以下圖:url

    以上就是所有代碼了,返回值就是識別的圖片中的文字。spa

相關文章
相關標籤/搜索