Esempio n. 1
0
        /// <summary>
        /// 调用通用文字识别(含位置信息版), 图片参数为本地图片
        /// </summary>
        /// <param name="path">图片路径</param>
        /// <returns>list集合一行一个值</returns>
        public List <string> General(string path)
        {
            List <string> contents = new List <string>();

            byte[] image = File.ReadAllBytes(path);
            // 调用通用文字识别(含位置信息版), 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获
            JObject result = client.General(image);
            // 如果有可选参数
            Dictionary <string, object> options = new Dictionary <string, object>();

            options.Add("recognize_granularity", "big");
            options.Add("language_type", "CHN_ENG");
            options.Add("detect_direction", "true");
            options.Add("detect_language", "true");
            options.Add("vertexes_location", "true");
            options.Add("probability", "true");
            // 带参数调用通用文字识别(含位置信息版), 图片参数为本地图片
            result = client.General(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);
        }
Esempio n. 2
0
        /// <summary>
        /// 通用文字识别(含位置信息版)
        /// </summary>
        /// <param name="byteArr_Image"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static OCRResult Excute_General(byte[] byteArr_Image)
        {
            OCRResult r = null;

            try
            {
                var options = new Dictionary <string, object>
                {
                    { "recognize_granularity", "big" }, // 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
                    { "language_type", "CHN_ENG" },     // 识别语言类型,默认为CHN_ENG。可选值包括:
                    { "detect_direction", "false" },    // 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:
                    { "detect_language", "false" },     // 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
                    { "vertexes_location", "false" },   // 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false
                    { "probability", "false" } // 是否返回识别结果中每一行的置信度
                };

                Ocr client = new Ocr(ApiKey, SecretKey);
                Newtonsoft.Json.Linq.JObject ocrResult = client.General(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);
        }
Esempio n. 3
0
    /*
     * 百度OCR接口部分
     * */
    public static string BaiduOCR(object imageObject)
    {                                         //文字识别,使用百度。
        baiduOCRapi.Timeout = 60000;          // 修改OCR超时时间
        byte[] image   = (byte[])imageObject; // 拆箱
        var    options = new Dictionary <string, object> {
            { "detect_direction", "true" }
        };                                                            // 增加识别方向参数,识别单字结果
        var    ocrResultJson   = baiduOCRapi.General(image, options); // 带参数调用通用文字识别(含位置信息版)
        string ocrResultString = ocrResultJson.ToString();            // ocrResultJson为OCR结果json格式

        Debug.Log(ocrResultString);
        return(ocrResultString);
    }
    // 线程函数,获取OCR结果存储到全局变量 OCR_RESULT 中
    private void GetOCR(object imageObj)
    {
        byte[] image = (byte[])imageObj;    // 拆箱

        var result = ocrAPI.General(image); // 调用通用文字识别

        var options = new Dictionary <string, object> {
            { "detect_direction", "true" }
        };                                       // 增加识别方向参数,识别单字结果

        result = ocrAPI.General(image, options); // 带参数调用通用文字识别(含位置信息版)
        string ocrResult = result.ToString();    // ocrResultw为OCR结果

        // 开始请求API1获得追踪位置坐标与关键词及其位置坐标
        string         api1Url     = "http://yotta.xjtushilei.com:8000/crystal/get_location/?OCR_result=" + ocrResult;
        HttpWebRequest api1Request = (HttpWebRequest)WebRequest.Create(api1Url);

        api1Request.Method      = "GET";
        api1Request.ContentType = "application/json";

        // 捕获请求失败的异常
        try
        {
            HttpWebResponse api1Response = (HttpWebResponse)api1Request.GetResponse();
            StreamReader    reader       = new StreamReader(api1Response.GetResponseStream(), Encoding.Default);
            OCR_RESULT    = reader.ReadToEnd();
            ocrThreadFlag = true;  // 线程结束标志位
        }
        catch (WebException e)
        {
            WebResponse  wenReq = (HttpWebResponse)e.Response;
            StreamReader reader = new StreamReader(wenReq.GetResponseStream(), Encoding.Default);
            tipsText.text = "网络请求出现异常,请检查网络链接";
        }

        api1Request.Abort();    // 结束网络请求
    }
        protected void Button1_ServerClick(object sender, EventArgs e)
        {
            var image = File.ReadAllBytes("C:\\Users\\admin\\Desktop\\捕获.JPG");
            // 如果有可选参数
            var options = new Dictionary <string, object> {
                { "language_type", "CHN_ENG" },
                { "recognize_granularity", "big" }
            };
            // 带参数调用通用文字识别, 图片参数为本地图片//
            var result = client.General(image, options);
            var res    = result["words_result"];

            foreach (var item in res)
            {
                var a = item["location"];
                //divs.InnerHtml+= @"<p style='width: "+ a["width"] + "px; height: " + a["height"] + "px; padding - top:" + a["top"] + "px; padding - left:" + a["left"] + "px'>"+ item["words"] + "</p>";
                divs.InnerHtml += item["words"] + "<br />";
            }
        }
        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>
                {
                    { "recognize_granularity", recognize_granularity },
                    { "language_type", language_type },
                    { "detect_direction", detect_direction.ToString().ToLower() },
                    { "detect_language", detect_language.ToString().ToLower() },
                    { "vertexes_location", vertexes_location.ToString().ToLower() },
                    { "probability", probability.ToString().ToLower() }
                };
                //带参数调用通用文字识别(含位置信息版)
                string result = client.General(by, options).ToString();
                Result.Set(context, result);
            }
            catch (Exception e)
            {
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, "有一个错误产生", e.Message);
            }
        }
Esempio 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;
        }