当前位置:   article > 正文

第三方之百度AI使用总结_client.asr wav

client.asr wav

目录

一.为什么使用百度AI

二.使用方式

1.官网文档

2.创建应用

3.使用步骤2得到的三个参数APP_ID, API_KEY, SECRET_KEY,通过httpclient或集成各种语言的sdk请求,java文档位置

4.java sdk 2021

三、多个使用示例

1.语音识别

2.文字识别

3.图像识别

4.人脸人体识别

5.自然语言处理


一.为什么使用百度AI

开发时有时需要实现一些特殊的功能,如语音、图片、人脸识别等,需要较复杂的算法,需要机器学习,深度学习等AI技术,自己实现开发维护成本太高。百度AI有各种实现可拿来即用(通过restful http直接请求或通过sdk请求),一般都有免费配额,够测试甚至小公司生产用。主要包括以下几大类及下面的分类

二.使用方式

1.官网文档

2.创建应用

注意勾选需要的支持API,否则请求时权限报错。注意有些api如语音识别中的api,如下图,需要手动去领取免费限额,需要从“语音技术”菜单栏进入创建的应用才可领取

3.使用步骤2得到的三个参数APP_ID, API_KEY, SECRET_KEY,通过httpclient或集成各种语言的sdk请求,java文档位置

4.java sdk 2021

  1. <dependency>
  2. <groupId>com.baidu.aip</groupId>
  3. <artifactId>java-sdk</artifactId>
  4. <version>4.15.3</version>
  5. </dependency>

sdk中的封装各种api httpClient对象命名规则:Aip+上图包名,如ApiSpeech,ApiOcr,ApiFace,如可选参数使用默认的就不必去看文档了

三、多个使用示例,每个大类选了几个小类测试。基本都是直接使用,个别依赖自己建立在百度中的数据仓库


1.语音识别

中文普通话

  1. @Slf4j
  2. public class BaiduAiUtils {
  3. private static final String APP_ID = "23519622";
  4. private static final String API_KEY = "dYfw224xpXiqECxjqKxxxxxx";
  5. private static final String SECRET_KEY = "ns3fzwtcwP3r15ieyB0Fg04gxnxxxxx";
  6. private static final int connectionTimeoutInMillis = 2000;
  7. private static final int socketTimeoutInMillis = 60000;
  8. public static void main(String[] args) {
  9. AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
  10. client.setConnectionTimeoutInMillis(2000);
  11. client.setSocketTimeoutInMillis(60000);
  12. JSONObject res = client.asr("e:/test/2.wav", "wav", 16000, null);
  13. System.out.println(res.toString(2));
  14. }
  15. }

{
  "result": ["我不知道嗯嗯。"],
  "err_msg": "success.",
  "sn": "134323696901610196572",
  "corpus_no": "6915741618237033080",
  "err_no": 0
}

2.文字识别

身份证识别,大多数有较固定格式的如火车票,银行卡、身份证,营业执照,通用图片都能识别,入参支持网络图片url,图片byte[]

  1. public static void main(String[] args) {
  2. AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
  3. // 传入可选参数调用接口
  4. HashMap<String, String> options = new HashMap<>(16);
  5. // 检测朝向
  6. options.put("detect_direction", "true");
  7. // 不开启身份证风险类型
  8. options.put("detect_risk", "false");
  9. options.put("paragraph", "true");
  10. client.setConnectionTimeoutInMillis(connectionTimeoutInMillis);
  11. client.setSocketTimeoutInMillis(socketTimeoutInMillis);
  12. JSONObject jsonObject = client.idcard(FileUtil.readBytes("e:/test/idcard.png"), "front", options);
  13. System.out.println(jsonObject);
  14. }

{"words_result":{"姓名":{"words":"曹XXX","location":{"top":30,"left":57,"width":32,"height":15}},"民族":{"words":"汉","location":{"top":56,"left":112,"width":11,"height":13}},"住址":{"words":"安徽省桐城市","location":{"top":105,"left":50,"width":71,"height":13}},"公民身份号码":{"words":"XXXXXXXXXX","location":{"top":0,"left":0,"width":0,"height":0}},"出生":{"words":"","location":{"top":0,"left":0,"width":0,"height":0}},"性别":{"words":"男","location":{"top":57,"left":54,"width":11,"height":12}}},"log_id":1347894925546487808,"words_result_num":6,"idcard_number_type":0,"image_status":"unknown","direction":0}

3.图像识别

汽车识别(汽车车身照,非车标车牌)等,支持各种动植物、车、色情等图片识别,相同相似图片搜索依赖自己创建在百度AI的图片库

  1. public static void main(String[] args) {
  2. AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
  3. client.setConnectionTimeoutInMillis(connectionTimeoutInMillis);
  4. client.setSocketTimeoutInMillis(socketTimeoutInMillis);
  5. JSONObject jsonObject = client.carDetect(FileUtil.readBytes("e:/test/car.png"), null);
  6. System.out.println(jsonObject);
  7. }

{"result":[{"score":0.1309162527322769,"year":"2010","name":"Jeep指挥官"},{"score":0.09497345983982086,"year":"2018","name":"日产途乐(Patrol)"},{"score":0.09022821485996246,"year":"2015","name":"Jeep自由人"},{"score":0.02996397763490677,"year":"2018","name":"Jeep牧马人"},{"score":0.02824835106730461,"year":"2017","name":"道奇Ram"}],"log_id":3795400147214215593,"color_result":"红色","location_result":{"top":3.619544267654419,"left":9.585105895996094,"width":466.8215637207031,"height":268.023590878906}

4.人脸人体识别

人脸搜索依赖自己创建在百度AI的人脸库

人脸识别,可传age,beauty等好玩的参数

  1. public static void main(String[] args) {
  2. AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
  3. client.setConnectionTimeoutInMillis(2000);
  4. client.setSocketTimeoutInMillis(60000);
  5. HashMap<String, String> map = new HashMap<>();
  6. map.put("face_field","age,beauty");
  7. JSONObject res = client.detect("http://qiniu.construn.cn/test%2Fuser_icon%2Fe3d3db8321313c9c0aa418ae1fb6f566%2F1602308257773..jpg%21thumbnail?e=1610237594&token=0b9qELo6MsZX3vfh_8HCysUF4-piqJkB45ZGRXLi:HZXedesZfETz3NsZSeAy4MYg3JQ=","URL",map);
  8. System.out.println(res.toString(2));
  9. }

{
  "result": {
    "face_num": 1,
    "face_list": [{
      "beauty": 27.02,
      "angle": {
        "roll": 3.41,
        "pitch": 12.36,
        "yaw": -0.72
      },
      "face_token": "d34d0d737f6368a6f3388aca1fe78d77",
      "location": {
        "top": 17.78,
        "left": 83.01,
        "rotation": 2,
        "width": 22,
        "height": 19
      },
      "face_probability": 0.96,
      "age": 3
    }]
  },
  "log_id": 7945896520145,
  "error_msg": "SUCCESS",
  "cached": 0,
  "error_code": 0,
  "timestamp": 1610202249
}

身份认证,需要图片,身份证号,姓名等入参

  1. public static void main(String[] args) {
  2. AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
  3. client.setConnectionTimeoutInMillis(2000);
  4. client.setSocketTimeoutInMillis(60000);
  5. HashMap<String, String> options = new HashMap<String, String>();
  6. options.put("quality_control", "NORMAL");
  7. options.put("liveness_control", "LOW");
  8. JSONObject res = client.personVerify("http://qxxx%2Fuser_icon%2Fe3d3db8321313c9c0aa418ae1fb6f566%2F1602308257773..jpg%21thumbnail?e=1610237594&token=0b9qELo6MsZX3vfh_8HCysUF4-piqJkB45ZGRXLi:HZXedesZfETz3NsZSeAy4MYg3JQ=","URL","5xxxxxxxxxx","马哥",options);
  9. System.out.println(res.toString(2));
  10. }

人脸对比,返回相似度评分

  1. public static void main(String[] args) {
  2. AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
  3. client.setConnectionTimeoutInMillis(2000);
  4. client.setSocketTimeoutInMillis(60000);
  5. MatchRequest req1 = new MatchRequest("xxxxx", "URL");
  6. MatchRequest req2 = new MatchRequest("xxxxx", "BASE64");
  7. ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
  8. requests.add(req1);
  9. requests.add(req2);
  10. JSONObject res = client.match(requests);
  11. System.out.println(res.toString(2));
  12. }

{
  "result": {
    "score": 100,
    "face_list": [
      {"face_token": "d34d0d737f6368a6f3388aca1fe78d77"},
      {"face_token": "d34d0d737f6368a6f3388aca1fe78d77"}
    ]
  },
  "log_id": 9900115756525,
  "error_msg": "SUCCESS",
  "cached": 0,
  "error_code": 0,
  "timestamp": 1610203191
}

活体检测,返回参数说明: frr_1e-4:万分之一误识率的阈值;frr_1e-3:千分之一误识率的阈值;frr_1e-2:百分之一误识率的阈值。误识率越底,准确率越高。

  1. public static void main(String[] args) {
  2. AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
  3. client.setConnectionTimeoutInMillis(2000);
  4. client.setSocketTimeoutInMillis(60000);
  5. FaceVerifyRequest req = new FaceVerifyRequest("http://qiniu.cxxxxx=", "URL");
  6. ArrayList<FaceVerifyRequest> list = new ArrayList<FaceVerifyRequest>();
  7. list.add(req);
  8. JSONObject res = client.faceverify(list);
  9. System.out.println(res.toString(2));
  10. }


{
  "result": {
    "thresholds": {
      "frr_1e-3": 0.3,
      "frr_1e-2": 0.9,
      "frr_1e-4": 0.05
    },
    "face_liveness": 0.4679185666,
    "face_list": [{
      "liveness": {"livemapscore": 0.4679185666},
      "angle": {
        "roll": 3.41,
        "pitch": 12.36,
        "yaw": -0.72
      },
      "face_token": "d34d0d737f6368a6f3388aca1fe78d77",
      "location": {
        "top": 17.78,
        "left": 83.01,
        "rotation": 2,
        "width": 22,
        "height": 19
      },
      "face_probability": 0.96
    }]
  },
  "log_id": 535001001101,
  "error_msg": "SUCCESS",
  "cached": 0,
  "error_code": 0,
  "timestamp": 1610203532
}
 

5.自然语言处理

分词,词义、情感、短文本相似度分析

分词

  1. public static void main(String[] args) {
  2. AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
  3. String text = "百度是一家高科技公司";
  4. JSONObject res = client.lexer(text, null);
  5. System.out.println(res.toString(2));
  6. }

{
  "log_id": 2953324902004609033,
  "text": "百度是一家高科技公司",
  "items": [
    {
      "formal": "",
      "loc_details": [],
      "item": "百度",
      "pos": "",
      "ne": "ORG",
      "basic_words": ["百度"],
      "byte_length": 4,
      "byte_offset": 0,
      "uri": ""
    },
    {
      "formal": "",
      "loc_details": [],
      "item": "是",
      "pos": "v",
      "ne": "",
      "basic_words": ["是"],
      "byte_length": 2,
      "byte_offset": 4,
      "uri": ""
    },
    {
      "formal": "",
      "loc_details": [],
      "item": "一家",
      "pos": "m",
      "ne": "",
      "basic_words": [
        "一",
        "家"
      ],
      "byte_length": 4,
      "byte_offset": 6,
      "uri": ""
    },
    {
      "formal": "",
      "loc_details": [],
      "item": "高科技",
      "pos": "n",
      "ne": "",
      "basic_words": [
        "高",
        "科技"
      ],
      "byte_length": 6,
      "byte_offset": 10,
      "uri": ""
    },
    {
      "formal": "",
      "loc_details": [],
      "item": "公司",
      "pos": "n",
      "ne": "",
      "basic_words": ["公司"],
      "byte_length": 4,
      "byte_offset": 16,
      "uri": ""
    }
  ]
}

短文本相似度

  1. public static void main(String[] args) {
  2. AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
  3. HashMap<String, Object> options = new HashMap<String, Object>();
  4. options.put("model", "CNN");
  5. JSONObject res = client.simnet("成都", "成都1", options);
  6. System.out.println(res.toString(2));
  7. }

{
  "log_id": 1273403813567969129,
  "score": 0.794843,
  "texts": {
    "text_1": "成都",
    "text_2": "成都1"
  }
}

文本纠错

  1. public static void main(String[] args) {
  2. AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
  3. JSONObject res = client.ecnet("疫情其间不要导出跑,好好呆在2加里", null);
  4. System.out.println(res.toString(2));
  5. }

{
  "log_id": 9017850856820090857,
  "item": {
    "vec_fragment": [{
      "correct_frag": "期间",
      "end_pos": 8,
      "ori_frag": "其间",
      "begin_pos": 4
    }],
    "score": 0.524812,
    "correct_query": "疫情期间不要导出跑,好好呆在2加里"
  },
  "text": "疫情其间不要导出跑,好好呆在2加里"
}

地址识别

  1. public static void main(String[] args) {
  2. AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
  3. JSONObject res = client.address("四川成都天府大道22", null);
  4. System.out.println(res.toString(2));
  5. }


{
  "log_id": 1347928665547079680,
  "town": "桂溪街道",
  "city": "成都市",
  "county_code": "510107",
  "county": "武侯区",
  "city_code": "510100",
  "phonenum": "",
  "province_code": "510000",
  "town_code": "510107021",
  "province": "四川省",
  "person": "",
  "detail": "天府大道22",
  "text": "四川成都天府大道22"
}

  1. public static void main(String[] args) {
  2. AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
  3. JSONObject res = client.address("四川成都郫都区阿斯大时代啊啊啊", null);
  4. System.out.println(res.toString(2));
  5. }

{
  "log_id": 1347929066937778176,
  "town": "三道堰镇",
  "city": "成都市",
  "county_code": "510117",
  "county": "郫都区",
  "city_code": "510100",
  "phonenum": "",
  "province_code": "510000",
  "town_code": "510117106",
  "province": "四川省",
  "person": "阿斯",
  "detail": "大时代啊啊啊",
  "text": "四川成都郫都区阿斯大时代啊啊啊"
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/94813
推荐阅读
相关标签
  

闽ICP备14008679号