赞
踩
目录
3.使用步骤2得到的三个参数APP_ID, API_KEY, SECRET_KEY,通过httpclient或集成各种语言的sdk请求,java文档位置
开发时有时需要实现一些特殊的功能,如语音、图片、人脸识别等,需要较复杂的算法,需要机器学习,深度学习等AI技术,自己实现开发维护成本太高。百度AI有各种实现可拿来即用(通过restful http直接请求或通过sdk请求),一般都有免费配额,够测试甚至小公司生产用。主要包括以下几大类及下面的分类:
注意勾选需要的支持API,否则请求时权限报错。注意有些api如语音识别中的api,如下图,需要手动去领取免费限额,需要从“语音技术”菜单栏进入创建的应用才可领取
- <dependency>
- <groupId>com.baidu.aip</groupId>
- <artifactId>java-sdk</artifactId>
- <version>4.15.3</version>
- </dependency>
sdk中的封装各种api httpClient对象命名规则:Aip+上图包名,如ApiSpeech,ApiOcr,ApiFace,如可选参数使用默认的就不必去看文档了
三、多个使用示例,每个大类选了几个小类测试。基本都是直接使用,个别依赖自己建立在百度中的数据仓库
中文普通话
- @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
}
身份证识别,大多数有较固定格式的如火车票,银行卡、身份证,营业执照,通用图片都能识别,入参支持网络图片url,图片byte[]
- public static void main(String[] args) {
- AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
- // 传入可选参数调用接口
- HashMap<String, String> options = new HashMap<>(16);
- // 检测朝向
- options.put("detect_direction", "true");
- // 不开启身份证风险类型
- 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);
-
- }
{"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}
汽车识别(汽车车身照,非车标车牌)等,支持各种动植物、车、色情等图片识别,相同相似图片搜索依赖自己创建在百度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}
人脸搜索依赖自己创建在百度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));
- }
{
"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
}
身份认证,需要图片,身份证号,姓名等入参
- 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","马哥",options);
- System.out.println(res.toString(2));
- }
人脸对比,返回相似度评分
- 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));
-
- }
{
"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));
- }
{
"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
}
分词,词义、情感、短文本相似度分析
分词
- public static void main(String[] args) {
- AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
- String text = "百度是一家高科技公司";
- 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("成都", "成都1", options);
- System.out.println(res.toString(2));
- }
{
"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("疫情其间不要导出跑,好好呆在2加里", 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("四川成都天府大道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("四川成都郫都区阿斯大时代啊啊啊", 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": "四川成都郫都区阿斯大时代啊啊啊"
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。