public static StringBuilder recognize(Bitmap bitmap)
        {
            var image = BitmapHelper.bitmap2Byte(bitmap);

            StringBuilder sb = new StringBuilder();

            var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);

            client.Timeout = 6000;

            var result = client.AccurateBasic(image);

            var options = new Dictionary <string, object> {
                { "language_type", "ENG" },     //语言
                { "detect_direction", "true" }, //图片方向
                { "probability", "true" }//图片识别成功可能性
            };

            result = client.AccurateBasic(image, options);

            string getJson = result.ToString();

            JsonImage.Root rt = JsonConvert.DeserializeObject <JsonImage.Root>(getJson);//JSON反序列化

            for (int i = 0; i < rt.words_result.Count; i++)
            {
                sb.AppendLine(rt.words_result[i].words).ToString();
            }

            return(sb);
        }
示例#2
0
        public string AdvancedGeneralDemo(Image url, int category = 0, string other = "front")
        {
            byte[] image  = Common.ImageHelper.ImageToBytes(url);
            var    client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);

            client.Timeout = 60000;  // 修改超时时间
            JObject result = null;

            switch (category)
            {
            case 0:    //通用文字识别
                result = client.AccurateBasic(image);
                break;

            case 1:    //生僻字识别
                result = client.GeneralEnhanced(image);
                break;

            case 2:    //身份证识别
                result = client.Idcard(image, other);
                break;

            case 3:    //银行卡识别
                result = client.Bankcard(image);
                break;

            case 4:    //驾驶证识别
                result = client.DrivingLicense(image);
                break;

            case 5:    //行驶证识别
                result = client.VehicleLicense(image);
                break;

            case 6:    //车牌识别
                result = client.LicensePlate(image);
                break;

            case 7:    //通用票据识别
                result = client.Receipt(image);
                break;

            case 8:    //营业执照识别
                result = client.BusinessLicense(image);
                break;

            default:    //通用文字识别
                result = client.AccurateBasic(image);
                break;
            }
            Console.WriteLine(result);
            if (result == null)
            {
                return("");
            }
            return(JsonConvert.SerializeObject(result));
        }
示例#3
0
 private void OcrMethod(byte[] base64Img)
 {
     try
     {
         var apiAuthConfig = Api_Auth.apiAuthConfig;
         var client        = new Baidu.Aip.Ocr.Ocr(apiAuthConfig.API_KEY, apiAuthConfig.SECRET_KEY);
         var result        = client.AccurateBasic(base64Img);
         var last          = result.Last.Last();
         this.rtbResult.ResetText();
         var items = result.First.First;
         foreach (var item in items)
         {
             var words     = item["words"];
             var str       = words.ToString();
             var outputStr = this.ckbReplaceComma.Checked ? ReplaceComma(str) : str;
             this.rtbResult.AppendText(outputStr);
             this.rtbResult.AppendText("\r\n");
         }
     }
     catch (Exception e)
     {
         LogHelper.WriteLog(e);
     }
     finally
     {
         this.Show();
         formCapture.Close();
     }
 }
示例#4
0
        private static JObject JudgeMethod(ApiVersion apiVersion, byte[] image)
        {
            JObject result;

            switch (apiVersion)
            {
            case ApiVersion.GeneralBasic:
                result = client.GeneralBasic(image);
                break;

            case ApiVersion.General:
                result = client.General(image);
                break;

            case ApiVersion.AccurateBasic:
                result = client.AccurateBasic(image);
                break;

            case ApiVersion.Accurate:
                result = client.Accurate(image);
                break;

            case ApiVersion.Numbers:
                result = client.Numbers(image);
                break;

            default:
                result = null;
                break;
            }
            return(result);
        }
示例#5
0
        public void AccurateBasicDemo()
        {
            var client = new Baidu.Aip.Ocr.Ocr(API_Key, SECRE_KEY);

            client.Timeout = 60000;
            var image = File.ReadAllBytes(ImagePath);

            OutResult = client.AccurateBasic(image);
        }
        private string OCR_AccurateBasic(Image image, bool GeneralOrAccurate)
        {
            if (image.Width > 4096 || image.Height > 4096)
            {
                return("图片尺寸太大了,图片尺寸改小些再试试.最大边长不能超过4096像素 Width:" + image.Width.ToString() + " Height:" + image.Height.ToString());
            }


            var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);

            client.Timeout = 60000;  // 修改超时时间
            var imageByte = ImageProcess.ImageToBytes(image);

            /*
             * // 如果有可选参数
             * var options = new Dictionary<string, object>{
             *  {"language_type", "CHN_ENG"},
             *  {"detect_direction", "true"},
             *  {"detect_language", "true"},
             *  {"probability", "true"}
             * };
             * // 带参数调用通用文字识别, 图片参数为本地图片
             * var result = client.GeneralBasic(image, options);
             */


            JObject result;

            if (GeneralOrAccurate)
            {
                result = client.AccurateBasic(imageByte);
            }
            else
            {
                // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获
                result = client.GeneralBasic(imageByte);
            }

            JToken words_result_num = result.SelectToken("words_result_num");

            if (Convert.ToInt32(words_result_num) > 0)
            {
                var    words = result.SelectToken("words_result").Select(p => p["words"]).ToList();
                string temp  = "";
                foreach (var word in words)
                {
                    temp = temp + word.ToString() + "\r\n";
                }
                return(temp);
            }
            else
            {
                return("");
            }
        }
示例#7
0
        public JObject AccurateBasicDemo(string img)
        {
            // 调用通用文字识别(高精度版),可能会抛出网络等异常,请使用try/catch捕获
            var image     = File.ReadAllBytes(img);
            var ApiKey    = "ApiKey";
            var SecretKey = "SecretKey";
            var client    = new Baidu.Aip.Ocr.Ocr(ApiKey, SecretKey);
            var result    = client.AccurateBasic(image);

            return(result);
        }
示例#8
0
        /// <summary>
        /// 图片上文字识别方式
        /// </summary>
        /// <param name="imgUrl">图片路径</param>
        /// <param name="way">GeneralBasic(普通识别);AccurateBasic(高精度识别)</param>
        private void RecognitionWay(string imgUrl, string way)
        {
            var image  = File.ReadAllBytes(imgUrl);
            var client = new Baidu.Aip.Ocr.Ocr(ApiMessage.API_KEY, ApiMessage.SECRET_KEY);

            client.Timeout = 60000;  // 修改超时时间

            var result = client.AccurateBasic(image);
            // 如果有可选参数
            var options = new Dictionary <string, object> {
                { "language_type", "CHN_ENG" }, //语言
                { "detect_direction", "true" }, //图片方向
                { "probability", "true" }//图片识别成功可能性
            };

            // 带参数调用通用文字识别,识别方式有很多种,我只是取了其中常用的两种,其它方式可以查看官网帮助文档
            switch (way)
            {
            case "AccurateBasic":
                result = client.AccurateBasic(image, options);
                break;

            case "GeneralBasic":
                result = client.GeneralBasic(image, options);
                break;
            }
            string getJson = result.ToString();                                          //关键:result返回的是一个一串Json格式的数据,具体大家可以单独输出查看;       //所以要解析这个JSON,还需一个帮助类JsonImage,然后用JSON反序列化,最后StringBuilder拼接

            JsonImage.Root rt = JsonConvert.DeserializeObject <JsonImage.Root>(getJson); //JSON反序列化
            StringBuilder  sb = new StringBuilder();

            for (int i = 0; i < rt.words_result.Count; i++)
            {
                textBox1.Text = sb.AppendLine(rt.words_result[i].words).ToString();
            }
        }
示例#9
0
        /// <summary>
        /// 高精度文字识别
        /// </summary>
        /// <param name="imgPath"></param>
        /// <returns></returns>
        public string Ocr_Acc_BaiDu(string imgPath)
        {
            var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);

            client.Timeout = 60000;  // 修改超时时间

            var image = File.ReadAllBytes(imgPath);

            // 调用通用文字识别(高精度版),可能会抛出网络等异常,请使用try/catch捕获
            var result = client.AccurateBasic(image);

            //Console.WriteLine(result);
            // 如果有可选参数
            var options = new Dictionary <string, object> {
                //是否检测图像朝向,默认不检测,- true:检测朝向;
                { "detect_direction", "true" },
                //是否返回识别结果中每一行的置信度
                { "probability", "true" }
            };

            // 带参数调用通用文字识别(高精度版)
            result = client.AccurateBasic(image, options);
            //Console.WriteLine(result);

            string str        = result.ToString();
            var    StrJOb     = JObject.Parse(str);
            var    text       = from obj in (JArray)StrJOb["words_result"] select(string) obj["words"];
            string coordinate = "";

            foreach (var r in text)
            {
                coordinate = r;
            }

            return(coordinate);
        }
示例#10
0
        private void button6_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.AccurateBasic(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);
                }
            }
        }
示例#11
0
 /// <summary>
 /// 高精度版本
 /// </summary>
 /// <param name="bitmap"></param>
 public static void AccurateBasicDemo(Bitmap bitmap)
 {
     var image = Bitmap2Byte(bitmap);// File.ReadAllBytes(imgFile);
     // 调用通用文字识别(高精度版),可能会抛出网络等异常,请使用try/catch捕获
     var result = client.AccurateBasic(image);
 }