首先感謝虹軟,是你們提供這么好的SDK支撐了我們的想象力!
這是一個用javav編寫的可視化應用,用戶通過自己的臉和計算機進行交互,計算機則通過萌萌女孩的語音和用戶對話。
核心程序就是利用ArcFace2.0識別性別、年齡,但是為了獲得正面臉,會根據ArcFace2.0的人臉3D角度、用語音提醒用戶,這是一個的互動環節。最后,程序會幽默的、萌萌的告訴用戶他的性別、年齡。
獲取SDK 請戳這里
完整的項目源碼、可執行程序,放在百度網盤:鏈接: https://pan.baidu.com/s/1eHF66l111S3Rs0VaS7v_LA
提取碼: ffag
其中主要的3個java文件,代碼如下:
===================================== HowOldAreU.java ===================================== package app; import java.awt.EventQueue; import javax.swing.JFrame; import java.io.File; import java.io.FileNotFoundException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Timer; import java.util.TimerTask; import java.awt.BorderLayout; import com.alibaba.fastjson.JSONArray; import com.arcsoft.face.FaceEngine; import com.github.sarxos.webcam.Webcam; import com.github.sarxos.webcam.WebcamPanel; import tools.MyFunc; import javax.swing.JOptionPane; /*這是一個用javav編寫的可視化應用,用戶通過自己的臉和計算機進行交互,計算機則通過萌萌女孩的語音和用戶對話。 核心程序就是利用ArcFace2.0識別性別、年齡,但是為了獲得正面臉,會根據ArcFace2.0的人臉3D角度、用語音提醒用戶,這是一個的互動環節。 最后,程序會幽默的、萌萌的告訴用戶他的性別、年齡。 作者:huanghua8080@126.com */ public class HowOldAreU { //應用根目錄 public static String fs = File.separator; public final static String localPath = System.getProperty("user.dir")+fs; public final static String soundDir = localPath+"sound"+fs; // public static Webcam camera = null; private JFrame frame; // public static FaceEngine faceEngine = null; @SuppressWarnings("rawtypes") public static List FaceFeature = new ArrayList<Map<String, String>>(); public static JSONArray aryFFTime = new JSONArray(); public static JSONArray aryFFCnt = new JSONArray(); public static String lastTime = "2019-01-09 13:30:00"; public static int faceCnt = 0; /** * Launch the application. */ public static void main(String[] args) { //判斷程序是否已經運行 String s = localPath+"lockApp.txt"; // RandomAccessFile raf = null; try { raf = new RandomAccessFile(new File(s), "rws"); } catch (FileNotFoundException e1) { JOptionPane.showMessageDialog(null, "獨占文件時發生異常。"+e1, "錯誤",JOptionPane.ERROR_MESSAGE); System.exit(0); } FileChannel fcin = raf.getChannel(); FileLock flin = null; try { flin = fcin.tryLock(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "鎖文件時發生異常:"+e, "錯誤",JOptionPane.ERROR_MESSAGE); System.exit(0); } if (flin == null) { JOptionPane.showMessageDialog(null, "程序已在運行,不可重復。", "錯誤",JOptionPane.ERROR_MESSAGE); System.exit(0); } s = "D:\\Dev\\ec_workspace\\cs1914age"; if(!s.equals(System.getProperty("user.dir"))) { if(args.length == 0) { JOptionPane.showMessageDialog(null, "沒有入參,程序將終止。", "錯誤",JOptionPane.ERROR_MESSAGE); System.exit(0); return; } if(!"age".equals(MyFunc.strTrim(args[0]).toLowerCase())) { JOptionPane.showMessageDialog(null, "入參錯誤,程序將終止。", "錯誤",JOptionPane.ERROR_MESSAGE); System.exit(0); return; } } //獲取攝像頭 camera = Webcam.getDefault(); if (camera == null) { JOptionPane.showMessageDialog(null, "攝像頭獲取失敗。", "錯誤",JOptionPane.ERROR_MESSAGE); return; } //初始化人臉引擎 s = HowOldAreUAs.initEngine(); if(!"".equals(s)) { JOptionPane.showMessageDialog(null, s, "錯誤",JOptionPane.ERROR_MESSAGE); System.exit(0); return; } //啟動窗體 EventQueue.invokeLater(new Runnable() { public void run() { try { HowOldAreU window = new HowOldAreU(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public HowOldAreU() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { // frame = new JFrame(); frame.setTitle("猜年齡"); frame.setBounds(100, 100, 610, 370); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout(0, 0)); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setUndecorated(true);//去邊框 //攝像頭加載到面板 WebcamPanel panel = new WebcamPanel(camera); frame.getContentPane().add(panel, BorderLayout.CENTER); //啟動聲音 HowOldAreUAs.playSound(100); //線程(識別頻率:毫秒) Timer timerMain = new Timer(); timerMain.scheduleAtFixedRate(new TimerTask() { public void run() { if (camera != null) { HowOldAreUAs.photo(); } } }, 0, 500); } }
======================
HowOldAreUAs
====================================
package app; import java.awt.image.BufferedImage; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Random; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.arcsoft.face.AgeInfo; import com.arcsoft.face.Face3DAngle; import com.arcsoft.face.FaceFeature; import com.arcsoft.face.FaceInfo; import com.arcsoft.face.FaceSimilar; import com.arcsoft.face.FunctionConfiguration; import com.arcsoft.face.GenderInfo; import com.arcsoft.face.Rect; import com.arcsoft.face.enums.ImageFormat; import com.sun.jna.Platform; import app.FaceAbout.ImageInfo; import tools.MyFunc; import tools.SoundPlay; public class HowOldAreUAs { public static final int recoFreq = 60;//同一人不重復識別時間(秒) public static final int scoreThreshold = 70;//人臉相似度閥值 //3D角度閥值 public static final BigDecimal yes3d = new BigDecimal("5"); //拍照 @SuppressWarnings("unchecked") public static void photo() { int rtn=-1,sex=-1,age=-1; //當前時間 String nowTime = MyFunc.getSvrTime("yyyy-MM-dd HH:mm:ss"); //不重復識別時間(去除過期的) for(int n=HowOldAreU.aryFFTime.size()-1;n>=0;n--) { if(MyFunc.datetimeSub(HowOldAreU.aryFFTime.get(n).toString(), nowTime) >= recoFreq) { HowOldAreU.aryFFTime.remove(n); HowOldAreU.aryFFCnt.remove(n); HowOldAreU.FaceFeature.remove(n); } } //拍照 BufferedImage cameraImg = HowOldAreU.camera.getImage(); //找臉 List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>(); ImageInfo imageInfo = new FaceAbout().bufferedImage2ImageInfo(cameraImg); HowOldAreU.faceEngine.detectFaces(imageInfo.getRgbData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList); int cnt = faceInfoList.size(); if (cnt == 0) { //5分鍾后,如果沒有人來,則呼喚 if(MyFunc.datetimeSub(HowOldAreU.lastTime, nowTime) > 300) { HowOldAreU.lastTime = nowTime; playSound(200); } return; } HowOldAreU.lastTime = nowTime; //找最大臉(第一張臉即為最大臉) FaceInfo oneFace = faceInfoList.get(0); //提取臉紋 FaceFeature CmFeature = new FaceFeature(); rtn = HowOldAreU.faceEngine.extractFaceFeature(imageInfo.getRgbData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, oneFace, CmFeature); if (rtn != 0) { playSound(250); return; } //是否剛剛識別過 int rfe = 0; int dSimilScore = 0; FaceSimilar faceSimilar = new FaceSimilar(); for(int n=0;n<HowOldAreU.aryFFTime.size();n++) { rtn = HowOldAreU.faceEngine.compareFaceFeature(CmFeature, (FaceFeature) HowOldAreU.FaceFeature.get(n), faceSimilar); if (rtn != 0) { return; } //得分 dSimilScore = new BigDecimal(faceSimilar.getScore()).multiply(new BigDecimal("100")).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); //大於閥值 if(dSimilScore >= scoreThreshold){ if(MyFunc.datetimeSub(HowOldAreU.aryFFTime.get(n).toString(), nowTime) < recoFreq) { rfe = 1; int hdt = Integer.parseInt( HowOldAreU.aryFFCnt.get(n).toString() ); if(hdt >= 1 && hdt <= 3) { playSound(180+hdt); HowOldAreU.aryFFCnt.set(n, hdt+1 ); //停頓一下 try { Thread.sleep(3000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } } break; } } } //最近識別過 if(rfe == 1) {return;} //識別過10個人后,做一次自我介紹 if(HowOldAreU.faceCnt == 11) { HowOldAreU.faceCnt = 0; } if(HowOldAreU.faceCnt == 0) { playSound(150); HowOldAreU.faceCnt ++; } //停頓一下 try { Thread.sleep(1000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } //原型 faceInfoList.add(oneFace); rtn = HowOldAreU.faceEngine.process(imageInfo.getRgbData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList, FunctionConfiguration.builder().supportAge(true).supportFace3dAngle(true).supportGender(true).build()); if (rtn != 0) { playSound(250); return; } //3D信息提取 List<Face3DAngle> face3DAngleList = new ArrayList<Face3DAngle>(); rtn = HowOldAreU.faceEngine.getFace3DAngle(face3DAngleList); if (rtn != 0) { playSound(250); return; } if(face3DAngleList.size() == 0) { playSound(250); return; } //0: 正常,其他數值:檢測結果不可信 int status3d = face3DAngleList.get(0).getStatus(); if(status3d != 0) {return;} BigDecimal pitch = new BigDecimal("0"); BigDecimal roll = new BigDecimal("0"); BigDecimal yaw = new BigDecimal("0"); BigDecimal yes3db = new BigDecimal("0").subtract(yes3d); //俯仰角 pitch = new BigDecimal(face3DAngleList.get(0).getPitch()).setScale(7, BigDecimal.ROUND_HALF_UP); if(pitch.compareTo(yes3d) == 1) { playSound(301); return; } if(pitch.compareTo(yes3db) == -1) { playSound(302); return; } //橫滾角 roll = new BigDecimal(face3DAngleList.get(0).getRoll()).setScale(7, BigDecimal.ROUND_HALF_UP); if(roll.compareTo(yes3d) == 1) { playSound(311); return; } if(roll.compareTo(yes3db) == -1) { playSound(312); return; } //偏航角 yaw = new BigDecimal(face3DAngleList.get(0).getYaw()).setScale(7, BigDecimal.ROUND_HALF_UP); if(yaw.compareTo(yes3d) == 1) { playSound(321); return; } if(yaw.compareTo(yes3db) == -1) { playSound(322); return; } //年齡提取 List<AgeInfo> ageInfoList = new ArrayList<AgeInfo>(); rtn = HowOldAreU.faceEngine.getAge(ageInfoList); if (rtn != 0) { playSoundSexAge(-1,-1); return; } age = ageInfoList.get(0).getAge(); if(age > 120) {age = 120;} //性別提取 List<GenderInfo> genderInfoList = new ArrayList<GenderInfo>(); rtn = HowOldAreU.faceEngine.getGender(genderInfoList); if (rtn != 0) { playSoundSexAge(-1,age); return; } sex = genderInfoList.get(0).getGender(); // if(sex == -1 && age == -1) { playSound(360); return; } //播報 playSoundSexAge(sex,age); //記錄人臉,防止重復識別同一個人 HowOldAreU.FaceFeature.add(CmFeature); HowOldAreU.aryFFTime.add(nowTime); HowOldAreU.aryFFCnt.add("1"); //記錄已識別數量 HowOldAreU.faceCnt ++; //System.out.println(HowOldAreU.faceCnt+" "+now_time); } public static void playSoundSexAge(int sex,int age) { //不同年齡段,不同稱謂 String agename = "frend"; if(sex >= 0) { if(age >= 0 && age <= 2) { agename = "00-"+sex; }else if(age >= 3 && age <= 18) { agename = "03-"+sex; }else if(age >= 19 && age <= 45) { agename = "19-"+sex; }else if(age >= 46 && age <= 75) { agename = "46-"+sex; }else if(age >= 76 && age <= 120) { agename = "76-"+sex; } } SoundPlay.playSoundFile(HowOldAreU.soundDir+"agename"+HowOldAreU.fs+"agename-"+agename+".mp3", null); //推測用語 JSONArray ary = new JSONArray(); ary.add("401");//你,大概 ary.add("402");//我猜你 ary.add("403");//我估計你 ary.add("404");//我看你 ary.add("405");//你看起來 int cnt = ary.size(); //隨機選擇一個 int idx = 0; if(cnt > 1) { Random random = new Random(); idx = random.nextInt(cnt)%(cnt+1); } SoundPlay.playSoundFile(HowOldAreU.soundDir+ary.get(idx)+".mp3", null); //年齡 SoundPlay.playSoundFile(HowOldAreU.soundDir+"age"+HowOldAreU.fs+"age"+age+".mp3", null); //停頓一下 try { Thread.sleep(1000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } //確認 ary = new JSONArray(); ary.add("481");//對不對啊? ary.add("482");//是不是呢? ary.add("483");//准嗎? ary.add("484");//差不多嗎? ary.add("485");//靠譜吧? cnt = ary.size(); idx = 0; if(cnt > 1) { Random random = new Random(); idx = random.nextInt(cnt)%(cnt+1); } SoundPlay.playSoundFile(HowOldAreU.soundDir+ary.get(idx)+".mp3", null); //停頓一下 try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } //笑一個 ary = new JSONArray(); ary.add("501");//哈哈! ary.add("502");//嘻嘻! cnt = ary.size(); idx = 0; if(cnt > 1) { Random random = new Random(); idx = random.nextInt(cnt)%(cnt+1); } SoundPlay.playSoundFile(HowOldAreU.soundDir+ary.get(idx)+".mp3", null); //如果錯了 ary = new JSONArray(); ary.add("521");//如果我說錯了, ary.add("522");//要是我沒有說對, cnt = ary.size(); idx = 0; if(cnt > 1) { Random random = new Random(); idx = random.nextInt(cnt)%(cnt+1); } SoundPlay.playSoundFile(HowOldAreU.soundDir+ary.get(idx)+".mp3", null); //別生氣 ary = new JSONArray(); ary.add("541");//你可別生氣哦! ary.add("542");//你別往心里去啊! ary.add("543");//你千萬別介意哈! cnt = ary.size(); idx = 0; if(cnt > 1) { Random random = new Random(); idx = random.nextInt(cnt)%(cnt+1); } SoundPlay.playSoundFile(HowOldAreU.soundDir+ary.get(idx)+".mp3", null); //停頓一下 try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } //下一個 ary = new JSONArray(); ary.add("561");//來,下一個! ary.add("562");//請下一位朋友! ary.add("563");//下一位,誰來? cnt = ary.size(); idx = 0; if(cnt > 1) { Random random = new Random(); idx = random.nextInt(cnt)%(cnt+1); } SoundPlay.playSoundFile(HowOldAreU.soundDir+ary.get(idx)+".mp3", null); } public static void playSound(int sound_kind) { // http://ai.baidu.com/tech/speech/tts JSONArray ary = new JSONArray(); //文件集 switch(sound_kind) { case 100: ary.add("101");//秋語已經啟動,就要工作啦! break; case 150: //大家好,我是機器人。主人給我取名:秋語,他還幫我訓練了一雙火眼金睛, //看一眼就能識別出你們人類的性別和年齡。有人想過來試一試嗎? ary.add("150"); break; case 181: ary.add("181");//你來過的,一分鍾之后再來,好嗎? break; case 182: ary.add("182");//你來過的,一分鍾之后再來,好嗎? break; case 183: ary.add("183");//你怎么還來呀?跟你說了等一分鍾的!你真是個急性子,不理你了。 break; case 200: //沒有發現人臉時 ary.add("201");//怎么沒有人來跟我玩兒? ary.add("202");//有人嗎?快來和我玩啦! ary.add("203");//我知道你幾歲了,過來試試吧! ary.add("204");//你們人呢?都到哪兒去了? ary.add("205");//我等了老半天,怎么連個人影也沒看到! break; case 250: //看不請人臉或無法提取臉紋時 ary.add("251");//嗨!靠近一點兒,我想看看你呢! ary.add("252");//喂!過來一點嘛,我都看不清你! ary.add("253");//hello,離我近一點兒,會有驚喜的! break; //3D角度過大 case 301://俯仰角過大:請低一下頭! case 302://俯仰角過大:把頭抬一下! case 311://橫滾角過大:頭向左轉一下! case 312://橫滾角過大:向右轉一下頭! case 321://偏航角過大:脖子向左歪一下! case 322://偏航角過大:向右歪一下脖子! ary.add(sound_kind); break; case 360: //性別、年齡均未知 ary.add("361");//你太神秘了,我實在猜不出你幾歲! ary.add("362");//你到底幾歲呢?我絞盡腦汁也想不出來! ary.add("363");//我無法識別你的年齡,我要請主人繼續進化我。 break; } int cnt = ary.size(); if(cnt == 0) {return;} //隨機選擇一個 int idx = 0; if(cnt > 1) { Random random = new Random(); idx = random.nextInt(cnt)%(cnt+1); } //播放 SoundPlay.playSoundFile(HowOldAreU.soundDir+ary.get(idx)+".mp3", null); } public static String initEngine() { JSONObject parm = MyFunc.GetAllProperties("config/parm.properties"); String s = MyFunc.strTrim(parm.getString("err")); if(!"".equals(s)) {return "參數文件讀取失敗。"+s; } //APPID String APPID = MyFunc.strTrim(parm.getString("APP_ID")); if("".equals(APPID)){return "終端APPID缺失,程序將終止。";} //SDKKEY String WIN_SDKKEY = MyFunc.strTrim(parm.getString("WIN_SDKKEY")); String LIN_SDKKEY = MyFunc.strTrim(parm.getString("LIN_SDKKEY")); String SDKKEY = WIN_SDKKEY; if(!Platform.isWindows()) {SDKKEY = LIN_SDKKEY;} if("".equals(SDKKEY)){return "終端SDKKEY缺失,程序將終止。";} //加載動態庫 s = FaceAbout.loadDllSo(); if(!"".equals(s)) { return "動態庫加載失敗,程序將終止。"+s; } //人臉引擎初始化 try { HowOldAreU.faceEngine = FaceAbout.initFaceEngine(APPID, SDKKEY); } catch (IOException e) { return "人臉引擎初始化失敗,程序將終止。"+e; } return ""