码迷,mamicode.com
首页 > Windows程序 > 详细

c#图文识别之百度api调用

时间:2018-01-11 11:40:21      阅读:543      评论:0      收藏:0      [点我收藏+]

标签:jpg   gid   ike   bapi   read   百度api   string   console   图片   

//百度图片识别api接口
  public static JObject GeneralBasic(string apikey,string secretkey,string path) { var client = new Baidu.Aip.Ocr.Ocr(apikey, secretkey); var image = File.ReadAllBytes(path); // 通用文字识别 var result = client.GeneralBasic(image, null); return result; }
//调用,方法,以及返回json 解析
  private void txbupload_Click(object sender, EventArgs e)
        {
            string apikey = txbapikey.Text;
            string secretkey = txbsercetkey.Text;
            string imagepath = txbpic.Text+ "\\yancaoxuke.jpg";
            JObject result =   OcrDemo.GeneralBasic(apikey, secretkey, imagepath);          
                var txts = (from obj in (JArray)result.Root["words_result"]
                         //   let phrase = (JObject)obj["phrase"]
                            select (string)obj["words"]);
            foreach (var r in txts)
                Console.WriteLine(r);
       
        }
//包结构
  log_id:3544105182,words_result_num:15,words_result:[
  {
    "words": "XX专卖零售许可证"
  },
  {
    "words": "许可证号xxx"
  },
  {
    "words": "XX有限公司"
  },
  {
    "words": "法定代表人xx"
  },
  {
    "words": "(负责人)"
  },
  {
    "words": "320507217865"
  },
  {
    "words": "企业型"
  },
  {
    "words": "有限责任公司"
  },
  {
    "words": "经營场xx室"
  },
  {
    "words": "卷烟,雪茄烟"
  },
  {
    "words": "许可范围"
  },
  {
    "words": "xxxx公司"
  },
  {
    "words": "供货单位"
  },
  {
    "words": "有效期服自"
  },

DEMO1
 
const string s = "{\"result\" : \"ok\",\"tuc\" : [ {\"authors\" : [ 13 ],\"meaningId\" : 2397164096556578585,\"phrase\" : {\"text\" : \"机场\",\"language\" : \"zho\" }},{\"authors\" : [ 3266 ],\"meaningId\" : 1269040436618011315,\"phrase\" : {\"text\" : \"機場\",\"language\" : \"zho\"}}]}";
var root = JObject.Parse(s);
var txts = (from obj in (JArray)root["tuc"]
            let phrase = (JObject)obj["phrase"]
            select (string)phrase["text"]);
foreach (var r in txts)
    Console.WriteLine(r);
DEMO2

const string s = "{\"result\" : \"ok\",\"tuc\" : [ {\"authors\" : [ 13 ],\"meaningId\" : 2397164096556578585,\"phrase\" : {\"text\" : \"机场\",\"language\" : \"zho\" }},{\"authors\" : [ 3266 ],\"meaningId\" : 1269040436618011315,\"phrase\" : {\"text\" : \"機場\",\"language\" : \"zho\"}}]}";
var root = JObject.Parse(s);
var txts = (from obj in (JArray)root["tuc"]
            select (string)obj["phrase"]["text"]);
foreach (var r in txts)
    Console.WriteLine(r);

c#图文识别之百度api调用

标签:jpg   gid   ike   bapi   read   百度api   string   console   图片   

原文地址:https://www.cnblogs.com/trdbk/p/8266993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!