/// <summary> /// 调用通用文字识别(含位置高精度版),图片参数为本地图片 /// </summary> /// <param name="path">图片路径</param> /// <returns>list集合一行一个值</returns> public List <string> Accurate(string path) { List <string> contents = new List <string>(); byte[] image = File.ReadAllBytes("图片文件路径"); // 调用通用文字识别(含位置高精度版),可能会抛出网络等异常,请使用try/catch捕获 JObject result = client.Accurate(image); // 如果有可选参数 Dictionary <string, object> options = new Dictionary <string, object>(); options.Add("recognize_granularity", "big"); options.Add("detect_direction", "true"); options.Add("vertexes_location", "true"); options.Add("probability", "true"); // 带参数调用通用文字识别(含位置高精度版) result = client.Accurate(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); }
/// <summary> /// 通用文字识别(含位置高精度版) /// </summary> /// <param name="byteArr_Image"></param> /// <returns></returns> public static OCRResult Excute_Accurate(byte[] byteArr_Image) { OCRResult r = null; try { var options = new Dictionary <string, object> { { "recognize_granularity", "big" }, // 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置 { "detect_direction", "false" }, // 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括: { "vertexes_location", "false" }, // 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false { "probability", "false" } // 是否返回识别结果中每一行的置信度 }; Ocr client = new Ocr(ApiKey, SecretKey); Newtonsoft.Json.Linq.JObject ocrResult = client.Accurate(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); }
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 }, { "detect_direction", detect_direction.ToString().ToLower() }, { "vertexes_location", vertexes_location.ToString().ToLower() }, { "probability", probability.ToString().ToLower() } }; //带参数调用通用文字识别(含位置信息高精度版) string result = client.Accurate(by, options).ToString(); Result.Set(context, result); } catch (Exception e) { SharedObject.Instance.Output(SharedObject.enOutputType.Error, "有一个错误产生", e.Message); } }
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; }