百度ai官網地址:http://ai.baidu.com/java
STEP1:點擊百度AI開放平臺導航右側的控制檯,點擊須要使用的AI服務項,使用百度帳號登陸。如還未持有百度帳戶,能夠點擊此處註冊百度帳戶。
STEP2:首次使用,登陸後將會進入開發者認證頁面,請填寫相關信息完成開發者認證。注:(如您以前已是百度雲用戶或百度開發者中心用戶,此步可略過。)
STEP3:進入具體AI服務項的控制面板(如文字識別、人臉識別),進行相關業務操做。web
建立好帳號後,在正式調用AI能力以前,須要您建立一下應用,這個是調用服務的基礎能力單元,不可或缺哦。在百度雲控制檯,已默認爲您免費開通百度語音、文字識別、人臉識別、天然語言處理、圖像審覈、知識圖譜服務六項服務。您能夠選擇您須要的服務,進入對應服務的控制檯中。進入某個服務的控制檯後,您將首先見到此服務的控制檯概覽頁(以人臉識別爲例),以下圖所示,點擊建立應用:express
點擊上圖中的「建立應用」,便可進入應用建立界面,以下圖所示:json
點擊建立就會生成一個應用,包含API Key和SecretKey,這兩個字段是獲取accessToken的依據,是調用api接口的必備字段:api
三、獲取簽名數組
獲取accessToken:經過調用接口:https://aip.baidubce.com/oauth/2.0/token?
grant_type=client_credentials&
client_id=Va5yQRHlA4Fq4eR3LT0vuXV4&
client_secret= 0rDSjzQ20XUj5itV7WRtznPQSzr5pVw2&獲取;緩存
四、進行開發服務器
首先須要下載開發必須用到的jar文件,引入項目:網絡
而後編寫調用接口的編碼,其中用到的文件操做,base64編碼處理以及http請求相關的util文件,百度都已經封裝好,能夠直接使用:socket
package com.hengyunsoft.face.aip; import java.net.URLEncoder; import com.baidu.aip.util.Base64Util; import com.hengyunsoft.face.util.AuthService; import com.hengyunsoft.face.util.FileUtil; import com.hengyunsoft.face.util.HttpUtil; /** * 人臉探測 */ public class FaceDetect { /** * 重要提示代碼中所需工具類 * FileUtil,Base64Util,HttpUtil,GsonUtils請從 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3 * 下載 */ public static String detect(String filePath) { // 請求url String url = "https://aip.baidubce.com/rest/2.0/face/v1/detect"; try { // 本地文件路徑 byte[] imgData = FileUtil.readFileByBytes(filePath); String imgStr = Base64Util.encode(imgData); String imgParam = URLEncoder.encode(imgStr, "UTF-8"); String param = "max_face_num=" + 5 + "&face_fields=" + "age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities" + "&image=" + imgParam; // 注意這裏僅爲了簡化編碼每一次請求都去獲取access_token,線上環境access_token有過時時間, 客戶端可自行緩存,過時後從新獲取。 String accessToken = AuthService.getAuth(); String result = HttpUtil.post(url, accessToken, param); System.out.println(result); return result; } catch (Exception e) { e.printStackTrace(); } return null; } }
調用測試接口,查看輸出結果,測試一張圖片中的人臉檢測個數:
public static void main(String[] args) { //testDetect1(); //測試人臉檢測 String path = System.getProperty("user.dir")+"\\image\\person3.jpg"; FaceDetect.detect(path); }
原圖:
檢測結果:
{"result_num":3,"result":[{"location":{"left":270,"top":48,"width":42,"height":39},"face_probability":1,"rotation_angle":-12,"yaw":-9.428524017334,"pitch":-1.9272558689117,"roll":-14.768548965454,"landmark":[{"x":288,"y":50},{"x":305,"y":46},{"x":300,"y":58},{"x":302,"y":69}],"landmark72":[{"x":272,"y":57},{"x":274,"y":63},{"x":278,"y":70},{"x":283,"y":76},{"x":290,"y":80},{"x":298,"y":81},{"x":305,"y":80},{"x":311,"y":77},{"x":314,"y":71},{"x":315,"y":65},{"x":315,"y":58},{"x":314,"y":52},{"x":313,"y":46},{"x":283,"y":52},{"x":285,"y":50},{"x":287,"y":49},{"x":290,"y":49},{"x":292,"y":51},{"x":290,"y":51},{"x":288,"y":52},{"x":286,"y":52},{"x":288,"y":50},{"x":279,"y":50},{"x":281,"y":47},{"x":285,"y":45},{"x":289,"y":45},{"x":292,"y":46},{"x":289,"y":47},{"x":285,"y":47},{"x":282,"y":48},{"x":301,"y":48},{"x":303,"y":46},{"x":305,"y":45},{"x":307,"y":45},{"x":309,"y":46},{"x":307,"y":47},{"x":305,"y":48},{"x":303,"y":48},{"x":305,"y":46},{"x":299,"y":44},{"x":301,"y":42},{"x":304,"y":41},{"x":307,"y":41},{"x":310,"y":42},{"x":307,"y":42},{"x":305,"y":43},{"x":302,"y":44},{"x":294,"y":50},{"x":295,"y":54},{"x":295,"y":57},{"x":295,"y":62},{"x":298,"y":61},{"x":303,"y":60},{"x":305,"y":60},{"x":303,"y":56},{"x":301,"y":52},{"x":299,"y":49},{"x":300,"y":58},{"x":295,"y":71},{"x":298,"y":68},{"x":302,"y":66},{"x":305,"y":66},{"x":308,"y":68},{"x":306,"y":70},{"x":303,"y":71},{"x":299,"y":71},{"x":298,"y":69},{"x":302,"y":68},{"x":305,"y":68},{"x":305,"y":68},{"x":302,"y":69},{"x":299,"y":70}],"age":38.027809143066,"beauty":24.773380279541,"expression":0,"expression_probablity":0.99999582767487,"faceshape":[{"type":"square","probability":0.53995013237},{"type":"triangle","probability":0.003084430238232},{"type":"oval","probability":0.35069698095322},{"type":"heart","probability":0.0041946368291974},{"type":"round","probability":0.10207384079695}],"gender":"male","gender_probability":0.99982494115829,"glasses":0,"glasses_probability":0.99990594387054,"race":"yellow","race_probability":0.99971705675125,"qualities":{"occlusion":{"left_eye":0,"right_eye":0,"nose":0,"mouth":0,"left_cheek":0,"right_cheek":0,"chin":0},"blur":0,"illumination":0,"completeness":0,"type":{"human":0.99693930149078,"cartoon":0.0030607103835791}}},{"location":{"left":44,"top":50,"width":40,"height":42},"face_probability":1,"rotation_angle":-4,"yaw":13.170437812805,"pitch":2.0879020690918,"roll":-2.8054466247559,"landmark":[{"x":51,"y":55},{"x":70,"y":54},{"x":58,"y":67},{"x":61,"y":78}],"landmark72":[{"x":45,"y":58},{"x":45,"y":64},{"x":47,"y":70},{"x":49,"y":76},{"x":52,"y":82},{"x":56,"y":88},{"x":62,"y":91},{"x":69,"y":89},{"x":77,"y":85},{"x":82,"y":77},{"x":85,"y":69},{"x":85,"y":62},{"x":85,"y":54},{"x":47,"y":56},{"x":49,"y":55},{"x":50,"y":54},{"x":53,"y":54},{"x":55,"y":56},{"x":53,"y":56},{"x":51,"y":57},{"x":49,"y":57},{"x":51,"y":55},{"x":45,"y":52},{"x":47,"y":50},{"x":49,"y":50},{"x":52,"y":50},{"x":55,"y":52},{"x":52,"y":52},{"x":49,"y":52},{"x":47,"y":52},{"x":65,"y":55},{"x":67,"y":53},{"x":69,"y":52},{"x":71,"y":53},{"x":74,"y":54},{"x":72,"y":55},{"x":69,"y":55},{"x":67,"y":55},{"x":70,"y":54},{"x":62,"y":51},{"x":65,"y":49},{"x":68,"y":48},{"x":72,"y":48},{"x":76,"y":50},{"x":72,"y":50},{"x":69,"y":50},{"x":65,"y":51},{"x":57,"y":56},{"x":56,"y":60},{"x":55,"y":65},{"x":54,"y":69},{"x":57,"y":70},{"x":62,"y":69},{"x":65,"y":69},{"x":64,"y":64},{"x":63,"y":60},{"x":62,"y":56},{"x":58,"y":67},{"x":55,"y":78},{"x":57,"y":76},{"x":60,"y":75},{"x":64,"y":76},{"x":68,"y":78},{"x":64,"y":80},{"x":60,"y":81},{"x":57,"y":80},{"x":57,"y":78},{"x":60,"y":77},{"x":64,"y":77},{"x":64,"y":78},{"x":60,"y":78},{"x":57,"y":78}],"age":33.576271057129,"beauty":24.980932235718,"expression":0,"expression_probablity":0.99999821186066,"faceshape":[{"type":"square","probability":0.21030895411968},{"type":"triangle","probability":0.00028864710475318},{"type":"oval","probability":0.69324284791946},{"type":"heart","probability":0.038888353854418},{"type":"round","probability":0.057271171361208}],"gender":"male","gender_probability":0.99993813037872,"glasses":0,"glasses_probability":0.99995875358582,"race":"yellow","race_probability":0.99995911121368,"qualities":{"occlusion":{"left_eye":0,"right_eye":0,"nose":0,"mouth":0,"left_cheek":0,"right_cheek":0,"chin":0},"blur":0,"illumination":0,"completeness":0,"type":{"human":0.9932644367218,"cartoon":0.0067355595529079}}},{"location":{"left":163,"top":54,"width":37,"height":42},"face_probability":1,"rotation_angle":-1,"yaw":-7.0411062240601,"pitch":-8.2431039810181,"roll":-2.4302639961243,"landmark":[{"x":176,"y":63},{"x":192,"y":62},{"x":186,"y":72},{"x":185,"y":82}],"landmark72":[{"x":163,"y":64},{"x":164,"y":71},{"x":165,"y":77},{"x":167,"y":84},{"x":172,"y":90},{"x":179,"y":94},{"x":186,"y":95},{"x":192,"y":93},{"x":196,"y":87},{"x":199,"y":81},{"x":200,"y":75},{"x":201,"y":69},{"x":200,"y":63},{"x":172,"y":63},{"x":174,"y":62},{"x":176,"y":61},{"x":178,"y":62},{"x":180,"y":63},{"x":178,"y":63},{"x":176,"y":64},{"x":174,"y":64},{"x":176,"y":63},{"x":169,"y":58},{"x":171,"y":54},{"x":174,"y":53},{"x":178,"y":53},{"x":181,"y":55},{"x":178,"y":55},{"x":175,"y":55},{"x":172,"y":56},{"x":189,"y":63},{"x":191,"y":61},{"x":193,"y":61},{"x":194,"y":61},{"x":196,"y":62},{"x":194,"y":63},{"x":193,"y":63},{"x":191,"y":63},{"x":192,"y":62},{"x":189,"y":55},{"x":192,"y":52},{"x":194,"y":52},{"x":197,"y":54},{"x":198,"y":57},{"x":196,"y":55},{"x":194,"y":55},{"x":192,"y":55},{"x":183,"y":63},{"x":182,"y":67},{"x":181,"y":70},{"x":180,"y":74},{"x":183,"y":74},{"x":189,"y":74},{"x":191,"y":73},{"x":189,"y":70},{"x":188,"y":66},{"x":187,"y":63},{"x":186,"y":72},{"x":176,"y":81},{"x":181,"y":79},{"x":186,"y":79},{"x":190,"y":79},{"x":193,"y":80},{"x":190,"y":84},{"x":186,"y":86},{"x":180,"y":84},{"x":181,"y":80},{"x":186,"y":80},{"x":190,"y":80},{"x":190,"y":83},{"x":186,"y":84},{"x":181,"y":83}],"age":48.371246337891,"beauty":20.235898971558,"expression":1,"expression_probablity":0.98125779628754,"faceshape":[{"type":"square","probability":0.043237078934908},{"type":"triangle","probability":0.0095938919112086},{"type":"oval","probability":0.30381575226784},{"type":"heart","probability":0.072911836206913},{"type":"round","probability":0.57044142484665}],"gender":"male","gender_probability":0.99851101636887,"glasses":0,"glasses_probability":0.99994230270386,"race":"yellow","race_probability":0.99906295537949,"qualities":{"occlusion":{"left_eye":0,"right_eye":0,"nose":0,"mouth":0,"left_cheek":0,"right_cheek":0,"chin":0},"blur":0,"illumination":0,"completeness":0,"type":{"human":0.96879607439041,"cartoon":0.031203903257847}}}],"log_id":3532732135}
關於人臉識別的一些接口:註冊,更新,刪除,檢測,識別,驗證等:
package com.hengyunsoft.face.aip; import java.util.HashMap; import org.json.JSONObject; import com.baidu.aip.face.AipFace; public class FaceManage { /** * 註冊用戶信息 * @param client * @param imagePath */ public static void addUser(AipFace client,String imagePath) { // 傳入可選參數調用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("action_type", "replace"); String uid = "user1"; String userInfo = "user's info"; String groupId = "group1,group2"; JSONObject res = client.addUser(uid, userInfo, groupId, imagePath, options); System.out.println(res.toString(2)); // 參數爲本地圖片二進制數組 // byte[] file = readImageFile(image); //res = client.addUser(file, uid, userInfo, groupId, options); //System.out.println(res.toString(2)); } /** * 更新用戶信息,包括圖片 * @param client * @param imagePath */ public static void updateUser(AipFace client,String imagePath) { // 傳入可選參數調用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("action_type", "replace"); String uid = "user1"; String userInfo = "user's info"; String groupId = "group1"; JSONObject res = client.updateUser(uid, userInfo, groupId, imagePath, options); System.out.println(res.toString(2)); // 參數爲本地圖片二進制數組 // byte[] file = readImageFile(image); // res = client.updateUser(file, uid, userInfo, groupId, options); // System.out.println(res.toString(2)); } /** * 刪除用戶信息 * @param client */ public static void deleteUser(AipFace client) { // 傳入可選參數調用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("group_id", "group1"); String uid = "user1"; // 人臉刪除 JSONObject res = client.deleteUser(uid, options); System.out.println(res.toString(2)); } /** * 根據用戶id和group信息獲取用戶詳情 * @param client */ public static void getUserById(AipFace client) { // 傳入可選參數調用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("group_id", "group1"); String uid = "user1"; // 用戶信息查詢 JSONObject res = client.getUser(uid, options); System.out.println(res.toString(2)); } /** * 用戶組查詢 * @param client */ public static void getGroupList(AipFace client) { // 傳入可選參數調用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("start", "0"); options.put("end", "50"); // 組列表查詢 JSONObject res = client.getGroupList(options); System.out.println(res.toString(2)); } /** * 用戶組內用戶列表查詢 * @param client */ public static void getGroupUsers(AipFace client) { // 傳入可選參數調用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("start", "0"); options.put("end", "50"); String groupId = "group1"; // 組內用戶列表查詢 JSONObject res = client.getGroupUsers(groupId, options); System.out.println(res.toString(2)); } /** * 人臉驗證,用於登陸,簽到等 * @param client */ public static void verifyUser(AipFace client,String imagePath) { // 傳入可選參數調用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("top_num", "1"); options.put("ext_fields", "faceliveness"); String uid = "user1"; String groupId = "group1,group2"; JSONObject res = client.verifyUser(uid, groupId, imagePath, options); System.out.println(res.toString(2)); // // 參數爲本地圖片二進制數組 // byte[] file = readImageFile(image); // res = client.verifyUser(file, uid, groupId, options); // System.out.println(res.toString(2)); } /** * 人臉識別,用於閘機,簽到 * @param client */ public static void identifyUser(AipFace client,String imagePath) { // 傳入可選參數調用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("ext_fields", "faceliveness"); options.put("user_top_num", "3"); String groupId = "group1,group2"; JSONObject res = client.identifyUser(groupId, imagePath, options); System.out.println(res.toString(2)); // // 參數爲本地圖片二進制數組 // byte[] file = readImageFile(image); // res = client.identifyUser(file, groupId, options); // System.out.println(res.toString(2)); } }
測試代碼:
package com.hengyunsoft.face.main; import java.util.HashMap; import org.json.JSONObject; import com.baidu.aip.face.AipFace; import com.hengyunsoft.face.aip.FaceDetect; import com.hengyunsoft.face.aip.FaceManage; public class TestFace { //設置APPID/AK/SK public static final String APP_ID = "10580034"; public static final String API_KEY = "你的api key"; public static final String SECRET_KEY = "你的secret key"; private static String basePath = System.getProperty("user.dir")+"\\image\\"; public static void main(String[] args) { AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY); String path = System.getProperty("user.dir")+"\\image\\person3.jpg"; String image = basePath; //測試人臉檢測 //FaceDetect.detect(path); //測試添加 // 參數爲本地圖片路徑 // image = basePath + "liushishi.jpg"; // FaceManage.addUser(client,image); //測試更新 // 參數爲本地圖片路徑 // image = basePath + "zhoubichang.jpg"; // FaceManage.updateUser(client,image); //測試刪除 // FaceManage.deleteUser(client); // //用戶查詢,根據id查詢 // FaceManage.getUserById(client); // // //用戶組列表查詢 // FaceManage.getGroupList(client); // //用戶組內用戶列表查詢 // FaceManage.getGroupUsers(client); //測試人臉驗證 image = basePath + "liushishi.jpg"; FaceManage.verifyUser(client, image); // //測試人臉識別 // image = basePath + "liushishi.jpg"; // FaceManage.identifyUser(client, image); } private static void testDetect1() { // 初始化一個AipFace AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY); // 可選:設置網絡鏈接參數 client.setConnectionTimeoutInMillis(2000); client.setSocketTimeoutInMillis(60000); // 可選:設置代理服務器地址, http和socket二選一,或者均不設置 //client.setHttpProxy("proxy_host", proxy_port); // 設置http代理 //client.setSocketProxy("proxy_host", proxy_port); // 設置socket代理 // 調用接口 String path = System.getProperty("user.dir")+"\\image\\test.png"; JSONObject res = client.detect(path, new HashMap<String, String>()); System.out.println(res.toString(2)); } }