當(dāng)前位置:首頁 > IT技術(shù) > Windows編程 > 正文

C#用百度OCR識(shí)別圖片文字
2021-09-08 16:53:04

百度有直接的Nuget包引用非常的簡(jiǎn)單調(diào)用:

具體怎么用看說明文檔就夠了:

?

?

static void Main(string[] args)
        {
            GeneralBasicDemo();
        }

        public static void GeneralBasicDemo()
        {
           
            var API_KEY = "********";
            var SECRET_KEY = "*************";

            var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);
            client.Timeout = 60000;  // 修改超時(shí)時(shí)間
            string img = @"E:imgFile15.jpg";

            var image = File.ReadAllBytes(img);
            // 調(diào)用通用文字識(shí)別(高精度版),可能會(huì)拋出網(wǎng)絡(luò)等異常,請(qǐng)使用try/catch捕獲
            // 如果有可選參數(shù)
            var options = new Dictionary<string, object>{
                                                             {"detect_direction", "true"},
                                                             {"probability", "true"}
                                                         };
            // 帶參數(shù)調(diào)用通用文字識(shí)別(高精度版)
            var result = client.AccurateBasic(image, options);
            string str = string.Empty;
            var txts = (from obj in (JArray)result.Root["words_result"]
                        select (string)obj["words"]);
            //foreach (var r in txts)
            //{
            //    str += r+"
";
            //}
            str = string.Join("
", txts);
            Console.WriteLine(str);
            Console.ReadKey();
        }

需要注意的是返回的數(shù)據(jù)是JObject格式,搞了大半天后才拿到數(shù)據(jù),類似鍵值對(duì)的方式,直接用linq獲取即可;

本文摘自 :https://www.cnblogs.com/

開通會(huì)員,享受整站包年服務(wù)立即開通 >