java实现人脸识别(使用百度云V3版本)
2017年,開發(fā)了第一個(gè)版本的人臉識(shí)別,當(dāng)時(shí)費(fèi)時(shí)有5天之久終于寫出來了,但是只適用于火狐瀏覽器,別的瀏覽器都打不開攝像頭。
2018年,將人臉識(shí)別重新完善,可以支持360、火狐、谷歌等主流瀏覽器,版本都是使用的百度云V2版,當(dāng)時(shí)已經(jīng)出來了V3版,只是一直沒時(shí)間去看。
2019年,重新拿出來以前寫的項(xiàng)目,將百度云V3版人臉識(shí)別寫出來分享給大家,以下是源碼分享(只是一個(gè)簡單的demo,不如V2版本的全面):
V2版本的人臉識(shí)別:java實(shí)現(xiàn)人臉識(shí)別源碼【含測試效果圖】——前期準(zhǔn)備工作及訪問提示
V3版本的jar包下載:人臉識(shí)別百度API,V3的jar包
V3版本的人臉檢測:
/*** * @Description: 該方法的主要作用:* @Title: 人臉檢測* @param @param client 設(shè)定文件 * @return 返回類型:void * @throws* 個(gè)人博客:https://blog.csdn.net/qq_34137397*/public static void face_jiance(AipFace client) { // 傳入可選參數(shù)調(diào)用接口HashMap<String, String> options = new HashMap<String, String>();options.put("face_field", "age");options.put("max_face_num", "2");options.put("face_type", "LIVE");File directory = new File("");// 參數(shù)為空String courseFile="";try {courseFile = directory.getCanonicalPath();//獲取當(dāng)前項(xiàng)目的根路徑//System.out.println(courseFile);String image = courseFile+"/WebRoot/picture/111.jpg";String imageType = "BASE64";//轉(zhuǎn)換格式String strImageToBase64 =ImageToBase64(image);//輸出base64圖像數(shù)據(jù)//System.out.println("本地圖片轉(zhuǎn)換Base64:"+strImageToBase64);// 人臉檢測JSONObject res = client.detect(strImageToBase64,imageType, options);System.out.println(res.toString(2));} catch (IOException e) {// TODO 異常執(zhí)行塊!e.printStackTrace();}}V3版本的人臉注冊:
/*** * @Description: 該方法的主要作用:人臉注冊* @Title: face_reg* @param @param client 設(shè)定文件 * @return 返回類型:void * @throws* 個(gè)人博客:https://blog.csdn.net/qq_34137397*/public static void face_reg(AipFace client) {// 傳入可選參數(shù)調(diào)用接口HashMap<String, String> options = new HashMap<String, String>();options.put("user_info", "user's info");String groupId = "group1";String userId = "user1";File directory = new File("");// 參數(shù)為空String courseFile="";try {courseFile = directory.getCanonicalPath();//獲取當(dāng)前項(xiàng)目的根路徑//System.out.println(courseFile);String image = courseFile+"/WebRoot/picture/111.jpg";String imageType = "BASE64";//轉(zhuǎn)換格式String strImageToBase64 =ImageToBase64(image);//輸出base64圖像數(shù)據(jù)//System.out.println("本地圖片轉(zhuǎn)換Base64:"+strImageToBase64);// 人臉注冊JSONObject res = client.addUser(strImageToBase64, imageType, groupId, userId, options);System.out.println(res.toString(2));} catch (IOException e) {// TODO 異常執(zhí)行塊!e.printStackTrace();}}V3版本的人臉登陸:
/*** * @Description: 該方法的主要作用:人臉登陸* @Title: face_login* @param @param client 設(shè)定文件 * @return 返回類型:void * @throws* 個(gè)人博客:https://blog.csdn.net/qq_34137397*/public static void face_login(AipFace client) {// 傳入可選參數(shù)調(diào)用接口HashMap<String, String> options = new HashMap<String, String>();options.put("user_id", "user1");File directory = new File("");// 參數(shù)為空String courseFile="";String groupIdList = "group1";try {courseFile = directory.getCanonicalPath();//獲取當(dāng)前項(xiàng)目的根路徑//System.out.println(courseFile);String image = courseFile+"/WebRoot/picture/222.jpg";String imageType = "BASE64";//轉(zhuǎn)換格式String strImageToBase64 =ImageToBase64(image);//輸出base64圖像數(shù)據(jù)//System.out.println("本地圖片轉(zhuǎn)換Base64:"+strImageToBase64);// 人臉?biāo)阉鱆SONObject res = client.search(strImageToBase64, imageType, groupIdList, options);System.out.println(res.toString(2));} catch (IOException e) {// TODO 異常執(zhí)行塊!e.printStackTrace();}}V3版本的本地圖片轉(zhuǎn)換成Base64圖像格式的方法:
/*** * @Description: 該方法的主要作用:本地圖片轉(zhuǎn)換成Base64圖像格式的方法* @Title: ImageToBase64* @param @param imgPath* @param @return 設(shè)定文件 * @return 返回類型:String * @throws* 個(gè)人博客:https://blog.csdn.net/qq_34137397*/public static String ImageToBase64(String imgPath){InputStream inputStream = null;byte[] data = null;try {inputStream = new FileInputStream(imgPath); data = new byte[inputStream.available()];inputStream.read(data);inputStream.close();}catch (Exception e) {e.printStackTrace();}//進(jìn)行編碼BASE64Encoder encoder = new BASE64Encoder();//返回編碼完成的base64圖像數(shù)據(jù)return encoder.encode(data);}另外附上其他人臉識(shí)別相關(guān)的博文:
1.關(guān)于人臉和指紋識(shí)別共同交流方案
2.人臉識(shí)別活體檢測之眨眨眼和張張嘴
3.C#winforms實(shí)現(xiàn)windows窗體人臉識(shí)別
4.java實(shí)現(xiàn)人臉識(shí)別源碼【含測試效果圖】——前期準(zhǔn)備工作及訪問提示
5.多功能語音播放器上線啦~
6.純js實(shí)現(xiàn)人臉識(shí)別眨眨眼張張嘴案例
7.人臉識(shí)別活體檢測測試案例
8.青鳥IT匯微信公眾號(hào)新增智能機(jī)器人
9.使用阿里云智能翻譯接口案例
10.使用阿里云火車票查詢接口案例
11.使用阿里云身份證掃描識(shí)別接口案例
12.調(diào)用阿里云接口實(shí)現(xiàn)短信消息的發(fā)送源碼
總結(jié)
以上是生活随笔為你收集整理的java实现人脸识别(使用百度云V3版本)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果手机左上方屏幕有模糊的一块
- 下一篇: 重要说明,粉丝必看【java人脸识别说明