第三方之百度AI使用总结

目录

一.为什么使用百度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请求),一般都有免费配额,够测试甚至小公司生产用。主要包括以下几大类及下面的分类:

第三方之百度AI使用总结

二.使用方式

2.创建应用

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

第三方之百度AI使用总结

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

第三方之百度AI使用总结

4.java sdk 2021

<dependency>
    <groupid>com.baidu.aip</groupid>
    <artifactid>java-sdk</artifactid>
    <version>4.15.3</version>
</dependency>

第三方之百度AI使用总结

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

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

1.语音识别

中文普通话

@Slf4j
public class BaiduAiUtils {

    private static final String APP_ID = "23519622";
    private static final String API_KEY = "dYfw224xpXiqECxjqKxxxxxx";
    private static final String SECRET_KEY = "ns3fzwtcwP3r15ieyB0Fg04gxnxxxxx";
    private static final int connectionTimeoutInMillis = 2000;
    private static final int socketTimeoutInMillis = 60000;

    public static void main(String[] args) {
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        JSONObject res = client.asr("e:/test/2.wav", "wav", 16000, null);
        System.out.println(res.toString(2));
    }

}

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

2.文字识别

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

   public static void main(String[] args) {
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        // &#x4F20;&#x5165;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x8C03;&#x7528;&#x63A5;&#x53E3;
        HashMap<string, string> options = new HashMap<>(16);
        // &#x68C0;&#x6D4B;&#x671D;&#x5411;
        options.put("detect_direction", "true");
        // &#x4E0D;&#x5F00;&#x542F;&#x8EAB;&#x4EFD;&#x8BC1;&#x98CE;&#x9669;&#x7C7B;&#x578B;
        options.put("detect_risk", "false");
        options.put("paragraph", "true");
        client.setConnectionTimeoutInMillis(connectionTimeoutInMillis);
        client.setSocketTimeoutInMillis(socketTimeoutInMillis);
        JSONObject jsonObject = client.idcard(FileUtil.readBytes("e:/test/idcard.png"), "front", options);
        System.out.println(jsonObject);

    }</string,>

{“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的图片库

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

{“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等好玩的参数

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

{
“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
}

身份验证需要照片、身份证号、姓名等。

[En]

Identity authentication requires a picture, ID number, name, etc.

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

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

  public static void main(String[] args) {
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        MatchRequest req1 = new MatchRequest("xxxxx", "URL");
        MatchRequest req2 = new MatchRequest("xxxxx", "BASE64");
        ArrayList<matchrequest> requests = new ArrayList<matchrequest>();
        requests.add(req1);
        requests.add(req2);
        JSONObject res = client.match(requests);
        System.out.println(res.toString(2));

    }</matchrequest></matchrequest>

{
“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:百分之一误识率的阈值。误识率越底,准确率越高。

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

{
“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.自然语言处理

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

[En]

Word segmentation, similarity analysis of word meaning, emotion and short text

分词

 public static void main(String[] args) {
        AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
        String text = "&#x767E;&#x5EA6;&#x662F;&#x4E00;&#x5BB6;&#x9AD8;&#x79D1;&#x6280;&#x516C;&#x53F8;";
        JSONObject res = client.lexer(text, null);
        System.out.println(res.toString(2));
    }

{
“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”: “”
}
]
}

短文本相似度

public static void main(String[] args) {
        AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
        HashMap<string, object> options = new HashMap<string, object>();
        options.put("model", "CNN");
        JSONObject res = client.simnet("&#x6210;&#x90FD;", "&#x6210;&#x90FD;1", options);
        System.out.println(res.toString(2));
    }</string,></string,>

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

文本纠错

    public static void main(String[] args) {
        AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
        JSONObject res = client.ecnet("&#x75AB;&#x60C5;&#x5176;&#x95F4;&#x4E0D;&#x8981;&#x5BFC;&#x51FA;&#x8DD1;,&#x597D;&#x597D;&#x5446;&#x5728;2&#x52A0;&#x91CC;", null);
        System.out.println(res.toString(2));
    }

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

地址识别

  public static void main(String[] args) {
        AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
        JSONObject res = client.address("&#x56DB;&#x5DDD;&#x6210;&#x90FD;&#x5929;&#x5E9C;&#x5927;&#x9053;22", null);
        System.out.println(res.toString(2));
    }

{
“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”
}

   public static void main(String[] args) {
        AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
        JSONObject res = client.address("&#x56DB;&#x5DDD;&#x6210;&#x90FD;&#x90EB;&#x90FD;&#x533A;&#x963F;&#x65AF;&#x5927;&#x65F6;&#x4EE3;&#x554A;&#x554A;&#x554A;", null);
        System.out.println(res.toString(2));
    }

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

Original: https://blog.csdn.net/qq_39506978/article/details/112404366
Author: jwolf2
Title: 第三方之百度AI使用总结

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/515330/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球