Ejemplo n.º 1
0
        /// <summary>
        /// 网络图片识别
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static List <string> WebImage(Image bmp)
        {
            string apiOrcKey    = ConfigUtil.GetConfigValue(YUConst.SETTING_BAIDUAPIORCKEY);
            string apiSecretKey = ConfigUtil.GetConfigValue(YUConst.SETTING_BAIDUORCSECRETKEY);

            if (apiOrcKey.IsNullOrEmptyOrWhiteSpace() || apiSecretKey.IsNullOrEmptyOrWhiteSpace())
            {
                throw new Exception("无法获取到Api Key和Secret Key,调用百度ORC识别失败。");
            }

            var client = new Baidu.Aip.Ocr.Ocr(apiOrcKey, apiSecretKey);
            var bytes  = ImageUtils.ImageToBytes(bmp);
            var result = client.WebImage(bytes);

            if (result.ContainsKey("error_code") && result.ContainsKey("error_msg"))
            {
                throw new Exception(string.Format("调用百度ORC识别失败,错误码:{0},错误信息:{1}", result["error_code"], result["error_msg"]));
            }

            List <string> orcResults = new List <string>();

            if (result.ContainsKey("words_result_num") && result.ContainsKey("words_result"))
            {
                var wordResults = result["words_result"];
                if (wordResults != null && wordResults.Any())
                {
                    foreach (var word in wordResults)
                    {
                        orcResults.Add(word.Value <string>("words"));
                    }
                }
            }
            return(orcResults);
        }
Ejemplo n.º 2
0
        public static void WebImage()
        {
            var client = new Baidu.Aip.Ocr.Ocr("Api Key", "Secret Key");
            var image  = File.ReadAllBytes("图片文件路径");

            // 网图识别
            var result = client.WebImage(image, null);
        }
Ejemplo n.º 3
0
        public static String WebImage(string imgpath)
        {
            var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);
            var image  = File.ReadAllBytes(imgpath);

            // 网图识别
            var result = client.WebImage(image, null);

            return(result.ToString());
        }