Ejemplo n.º 1
0
        /// <summary>
        /// 调用通用文字识别(高精度版)
        /// </summary>
        /// <param name="path">图片路径</param>
        /// <returns>list集合一行一个值</returns>
        public List <string> AccurateBasic(string path)
        {
            List <string> contents = new List <string>();

            // 修改超时时间
            client.Timeout = 60000;
            var image = File.ReadAllBytes(path);
            // 调用通用文字识别(高精度版),可能会抛出网络等异常,请使用try/catch捕获
            JObject result = client.AccurateBasic(image);
            // 如果有可选参数
            Dictionary <string, object> options = new Dictionary <string, object>();

            options.Add("detect_direction", "true");
            options.Add("probability", "true");
            // 带参数调用通用文字识别(高精度版)
            result = client.AccurateBasic(image, options);
            string content = result["words_result"].ToString();
            JArray jarray  = JArray.Parse(content);

            for (int i = 0; i < jarray.Count; i++)
            {
                JObject jobject = JObject.Parse(jarray[i].ToString());
                string  hang    = jobject["words"].ToString();
                contents.Add(hang);
            }
            return(contents);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 通用文字识别(高精度版)
        /// </summary>
        /// <param name="byteArr_Image"></param>
        /// <returns></returns>
        public static OCRResult Excute_AccurateBasic(byte[] byteArr_Image)
        {
            OCRResult r = null;

            try
            {
                var options = new Dictionary <string, object>
                {
                    { "detect_direction", "false" }, // 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:
                    { "probability", "false" } // 是否返回识别结果中每一行的置信度
                };

                Ocr client = new Ocr(ApiKey, SecretKey);
                Newtonsoft.Json.Linq.JObject ocrResult = client.AccurateBasic(image: byteArr_Image, options: options);
                r = getOCRResult(ocrResult);
            }
            catch (Exception ex)
            {
                r               = new OCRResult();
                r.IsComplete    = false;
                r.ExceptionInfo = ex.GetFullInfo();
                r.IsSuccess     = false;
            }

            return(r);
        }
Ejemplo n.º 3
0
        public static string Recognize(byte[] image)
        {
            try
            {
                if (OCR == null)
                {
                    InitBaiDuOCR(Config.OCR_API_KEY, Config.OCR_SECRET_KEY);
                }

                JObject res;
                if (Config.OCREnhance)
                {
                    res = OCR.AccurateBasic(image);
                }
                else
                {
                    res = OCR.GeneralBasic(image);
                }
                System.Diagnostics.Debug.WriteLine(res.ToString());

                JToken[]      arr = res["words_result"].ToArray();
                StringBuilder sb  = new StringBuilder();
                //合并结果
                foreach (JToken item in arr)
                {
                    sb.AppendLine(item["words"].ToString());
                }

                return(sb.ToString());
            }
            catch (ArgumentNullException)
            {
                throw new APIException("OCR文本识别错误,可能是API调用次数达到上限");
            }
        }
        protected override void Execute(CodeActivityContext context)
        {
            string path       = FileName.Get(context);
            string API_KEY    = APIKey.Get(context);
            string SECRET_KEY = SecretKey.Get(context);

            img = image.Get(context);
            try
            {
                if (path != null)
                {
                    by = SaveImage(path);
                }
                else
                {
                    by = ConvertImageToByte(img);
                }
                var client = new Ocr(API_KEY, SECRET_KEY);
                //修改超时时间
                client.Timeout = 60000;
                //参数设置
                var options = new Dictionary <string, object>
                {
                    { "detect_direction", detect_direction.ToString().ToLower() },
                    { "probability", probability.ToString().ToLower() }
                };
                //带参数调用通用文字识别(高精度版)
                string result = client.AccurateBasic(by, options).ToString();
                Result.Set(context, result);
            }
            catch (Exception e)
            {
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, Localize.LocalizedResources.GetString("msgErrorOccurred"), e.Message);
            }
        }
Ejemplo n.º 5
0
        private void button5_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems[0].Index < 0)
            {
                return;
            }

            if (List_images[listView1.SelectedItems[0].Index] != null)
            {
                textBox1.Text = string.Empty;

                Ocr ocr = new Ocr(ApiKey, SecretToken);

                var _bytes = imageToByte(List_images[listView1.SelectedItems[0].Index]);

                var jObj = this.Text.IndexOf("数学") < 0?ocr.GeneralBasic(_bytes):ocr.AccurateBasic(_bytes);

                var Answer = jObj.ToObject <Answer_Root>();


                if (!Directory.Exists(Path.Combine(tmpFilePath, listView1.SelectedItems[0].Index.ToString())))
                {
                    Directory.CreateDirectory(tmpFilePath);
                }
                using (StreamWriter streamWriter = new StreamWriter(Path.Combine(tmpFilePath, listView1.SelectedItems[0].Index.ToString())))
                {
                    foreach (var item in Answer.words_result)
                    {
                        streamWriter.Write(item.words + '\n');
                    }
                }
                foreach (var item in Answer.words_result)
                {
                    textBox1.Text += (item.words + '\n');
                }
                button5.Enabled = false;
            }
            else
            {
                MessageBox.Show("没有选择照片");
            }
        }
        protected override void Execute(CodeActivityContext context)
        {
            string path       = FileName.Get(context);
            string API_KEY    = APIKey.Get(context);
            string SECRET_KEY = SecretKey.Get(context);

            img = image.Get(context);
            try
            {
                if (path != null)
                {
                    by = SaveImage(path);
                }
                else
                {
                    by = ConvertImageToByte(img);
                }
                var client = new Ocr(API_KEY, SECRET_KEY);
                //修改超时时间
                client.Timeout = 60000;
                //参数设置
                var options = new Dictionary <string, object>
                {
                    { "detect_direction", detect_direction.ToString().ToLower() },
                    { "probability", probability.ToString().ToLower() }
                };
                //带参数调用通用文字识别(高精度版)
                var json = client.AccurateBasic(by, options).ToString();
                Console.WriteLine(json);
                var result = JsonConvert.DeserializeObject <BaiDuOCRHighPrecisionResult>(json);
                Result.Set(context, result);
            }
            catch (Exception e)
            {
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, "有一个错误产生", e.Message);
            }
        }
Ejemplo n.º 7
0
        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == null || textBox1.Text == "")
            {
                MessageBox.Show("请选择图片");
                return;
            }
            var image = File.ReadAllBytes(textBox1.Text);
            // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获
            var     options = new Dictionary <string, object>();
            JObject result  = null;

            if (checkBox2.Checked)
            {
                options.Add("detect_direction", "true");
            }


            if (radioButton2.Checked)                         //判断精度
            {
                if (checkBox1.Checked)                        //判断位置
                {
                    result = client.Accurate(image, options); //高精度有位置
                }
                else
                {
                    result = client.AccurateBasic(image, options);//高精度没有位置
                }
            }
            else
            {
                if (checkBox1.Checked)                       //判断位置
                {
                    result = client.General(image, options); //普通精度有位置
                }
                else
                {
                    result = client.GeneralBasic(image, options);   //普通精度没有位置
                }
            }
            String printresult = "";

            //输出结果
            JArray arr = (JArray)result["words_result"];

            if (result["direction"] != null)
            {
                printresult = printresult + "direction:" + result["direction"] + "\n";
            }

            foreach (var item in arr)  //循环获取值
            {
                var jo = (JObject)item;
                if (jo["location"] != null)
                {
                    printresult = printresult + "left:" + Convert.ToString(jo["location"]["left"])
                                  + "|top:" + Convert.ToString(jo["location"]["top"])
                                  + "|width:" + Convert.ToString(jo["location"]["width"])
                                  + "|height:" + Convert.ToString(jo["location"]["height"]) + "\n";
                }
                if (jo["words"] != null)
                {
                    printresult = printresult + Convert.ToString(jo["words"]) + "\n";
                }
                if (jo["chars"] != null)
                {
                    JArray chararr = (JArray)jo["chars"];
                    foreach (var chars in chararr)   //循环获取值
                    {
                        if (chars["location"] != null)
                        {
                            printresult = "\t\t" + printresult + "left:" + Convert.ToString(chars["location"]["left"])
                                          + "|top:" + Convert.ToString(chars["location"]["top"])
                                          + "|width:" + Convert.ToString(chars["location"]["width"])
                                          + "|height:" + Convert.ToString(chars["location"]["height"]) + "\n";
                        }
                        printresult = "\t\t" + printresult + Convert.ToString(chars["char"]) + "\n";
                    }
                }
            }
            richTextBox1.Text = printresult;
        }