虹软官网:https://ai.arcsoft.com.cn
注册后,下载适配的sdk
注意提取下载的sdk中的jar包,代码中需要用到
<dependency> <groupId>com.arcsoft.face</groupId> <artifactId>arcsoft-sdk-face</artifactId> <version>3.0.0.0</version> <scope>system</scope> <systemPath>${basedir}/declib/arcsoft-sdk-face-3.0.0.0.jar</systemPath> </dependency>
我这里全都写在了一个test类里,代码如下:
package com.example.hr_demo; import Decoder.BASE64Encoder; import com.arcsoft.face.*; import com.arcsoft.face.enums.DetectMode; import com.arcsoft.face.enums.DetectOrient; import com.arcsoft.face.enums.ErrorInfo; import com.arcsoft.face.toolkit.ImageInfo; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.io.*; import java.nio.charset.StandardCharsets; import java.text.DecimalFormat; import java.util.*; import static com.arcsoft.face.toolkit.ImageFactory.getRGBData; @SpringBootTest class HrDemoApplicationTests { @Test void contextLoads() throws UnsupportedEncodingException { //在线激活 String appId = "ididididiididid"; //你下载sdk的时候会给你id和key,填写进来就行 String sdkKey = "keykeykeykey"; //System.loadLibrary(""); FaceEngine faceEngine = new FaceEngine("D:\\HRSDK\\libs\\WIN64"); int activeCode = faceEngine.activeOnline(appId, sdkKey); if (activeCode != ErrorInfo.MOK.getValue() && activeCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) { System.out.println("引擎激活失败"); }else { System.out.println("引擎激活成功"); } int errorCode; ActiveFileInfo activeFileInfo=new ActiveFileInfo(); errorCode = faceEngine.getActiveFileInfo(activeFileInfo); if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) { System.out.println("获取激活文件信息失败"); } //引擎配置 EngineConfiguration engineConfiguration = new EngineConfiguration(); engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE); engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT); engineConfiguration.setDetectFaceMaxNum(10); engineConfiguration.setDetectFaceScaleVal(16); //功能配置 FunctionConfiguration functionConfiguration = new FunctionConfiguration(); functionConfiguration.setSupportAge(true); functionConfiguration.setSupportFace3dAngle(true); functionConfiguration.setSupportFaceDetect(true); functionConfiguration.setSupportFaceRecognition(true); functionConfiguration.setSupportGender(true); functionConfiguration.setSupportLiveness(true); functionConfiguration.setSupportIRLiveness(true); engineConfiguration.setFunctionConfiguration(functionConfiguration); //初始化引擎 errorCode = faceEngine.init(engineConfiguration); if (errorCode != ErrorInfo.MOK.getValue()) { System.out.println("初始化引擎失败"); } //人脸检测 输入端 ImageInfo imageInfo = getRGBData(new File("C:\\Users\\xxxx\\Desktop\\fbb.png"));//被检测的图片 //ImageInfo imageInfo = getRGBData(new File("D:\\Idea\\DB_images\\fanbingbing_jksiahd.png")); System.out.println("imageInfo =" + imageInfo ); List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>(); errorCode = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList); System.out.println("识别到的人脸数 = " + faceInfoList.size());// //特征提取 FaceFeature faceFeature = new FaceFeature(); errorCode = faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList.get(0), faceFeature); String str = "0,-128,-6,68,0,0,-96,65,93,105,37,-69,-10,10,14,61,115,99,-104,60,-14,20,50,-67,-53,127,-26,61,-74,-56,5,-66,-85,62,-103,-67,75,83,33,-68,-43,75,106,-68,101,76,-79,-69,34,-86,88,61,74,-118,-57,61,72,81,65,-67,-59,23,68,60,-6,-106,24,61,5,121,6,-66,-26,-69,-28,-68,-114,74,18,62,-62,63,-36,61,-85,68,19,59,34,-78,94,61,3,60,-20,-67,-85,34,38,-66,33,106,-60,-67,48,71,-27,-69,66,-20,41,61,121,-47,-30,60,-31,-65,45,-67,-117,26,11,62,-34,107,-113,61,59,92,-104,-68,109,31,-95,-68,-90,-28,-34,60,19,-115,80,61,-63,2,-45,-67,-16,-58,-4,61,-113,-67,33,-70,-79,39,-85,-67,1,71,-79,-67,89,60,12,62,3,-75,33,61,-38,-50,18,61,101,-43,-67,-68,77,-6,18,-67,13,-124,-52,60,-41,-85,-104,-67,7,52,86,60,-58,-85,-27,61,40,34,40,-67,36,-121,10,-68,120,61,-36,61,126,46,55,60,90,-25,7,-67,-11,-125,-62,60,70,19,66,62,110,120,-122,61,50,34,-36,-69,-89,115,-73,-67,-118,29,27,60,41,-92,101,61,-43,73,84,61,-9,75,119,61,-81,55,123,-67,-2,-12,-52,60,-25,40,123,-67,-5,45,19,61,-91,1,-53,60,115,-92,-4,-69,-85,-76,-122,60,89,49,-123,-70,-30,57,-60,-67,87,25,88,-67,95,-61,-84,59,114,70,-102,60,-86,-79,47,59,-70,-62,72,-67,113,127,-48,61,-68,-111,-123,-67,65,39,-111,-67,-9,-102,-38,-68,111,100,-86,61,-14,126,36,-67,75,-4,-10,-69,105,61,113,-68,15,121,20,-67,7,47,-73,61,8,23,-102,-67,83,25,6,62,49,-7,34,61,-90,19,-55,60,-2,31,-40,-68,37,84,-120,61,22,119,53,62,-46,43,-125,61,49,93,33,61,5,11,79,61,60,110,-124,61,-86,-39,-41,-67,37,87,-30,-68,-5,40,-91,-68,-3,-21,-88,59,116,30,-121,59,-128,-98,-113,-67,-75,44,-101,-67,19,26,-109,61,-88,124,-79,61,-55,-2,-42,61,-84,-126,-125,59,30,-49,-59,-67,-67,-46,-45,-68,26,-33,32,60,-9,-88,-102,61,-11,-110,-41,61,-75,-70,115,-70,75,-93,-127,61,33,65,-73,60,98,117,39,-69,-87,106,-61,-67,-12,50,-114,-68,121,-18,52,61,-124,69,14,-67,108,-11,4,-66,11,67,54,-66,-120,5,103,-68,58,-96,83,-67,-48,-30,-118,-67,126,-59,119,61,-111,-125,-109,-67,8,1,-15,60,-82,40,85,61,88,82,56,-70,1,-30,4,62,-16,37,-93,61,117,-13,50,-67,43,88,-116,59,40,-77,-73,-68,35,-34,8,-67,-106,61,-115,-67,120,96,-78,-68,-106,-44,-12,61,-45,126,-5,61,7,118,15,-67,-111,-40,-107,-67,-5,99,68,61,-106,71,-13,60,17,81,-115,60,-58,21,49,59,106,-22,-29,-67,-119,57,-79,-68,-17,-59,34,-67,74,39,-10,-68,58,56,123,61,-73,72,-73,56,86,119,-76,61,89,-91,-15,-67,-65,-24,-107,-69,51,80,14,-67,-48,123,23,60,-65,-12,-69,-68,71,-71,-103,-68,-17,101,-83,60,29,-99,-120,61,38,77,-13,60,71,-5,15,-69,-91,-91,72,-68,-128,-98,66,61,-126,101,30,58,45,-109,-65,61,-95,-113,43,-67,69,105,6,-68,-21,13,-128,61,90,-68,10,-67,-42,-82,71,-67,102,-35,-41,61,24,25,93,-67,118,9,-36,-67,10,-117,-74,-68,101,124,110,-69,46,53,-103,61,6,-10,73,-68,115,81,-6,59,88,100,68,-67,120,78,-57,-67,-113,-101,93,61,8,111,74,60,20,119,16,60,9,-108,-47,61,123,-112,7,62,17,-106,7,61,59,64,-64,60,22,78,12,61,126,-18,-122,61,-78,117,-46,-68,109,89,-63,-67,-103,-90,7,-66,-85,112,60,-67,79,-43,-111,61,61,127,-88,-67,19,56,-60,-68,117,28,9,-67,-120,108,-82,-67,-107,22,36,60,-37,-63,-36,60,43,-84,-19,59,15,-11,-125,-68,-48,-11,-40,-67,-50,123,-75,-67,75,111,-106,-68,70,-12,-40,-68,-64,-68,-12,-68,-5,-54,-47,59,40,-85,57,-67,53,-56,73,-67,-113,113,22,61,1,103,-53,-67,119,-39,72,-67,96,-80,57,-67,10,-22,-113,-68,19,-38,-116,-67,-6,-82,-11,-68,57,-28,-79,-67,-3,121,-4,-68,-17,-109,-89,-67,97,17,-58,61,102,-46,52,62,-83,9,-118,-67,82,-62,95,-67,-50,-22,63,59,81,37,-18,-67,-61,-99,-27,-68,109,105,101,-67,-51,-48,-41,-67,-105,-97,82,61,-103,115,-43,60,-66,-47,-119,-67,15,50,10,-67,-103,-86,-5,61,18,-7,-77,-67,5,43,18,61,-41,-46,125,-67,123,97,3,-67,-14,-86,96,-67,110,-82,97,60,98,76,-71,60,-73,-6,17,60,75,113,29,61,69,-22,-89,61,-105,47,63,-68,34,1,-121,61,-91,-113,-62,-67,93,71,93,-70,42,2,-81,61,-54,33,-65,-70,-103,-42,-82,-67,-26,114,-117,60,14,8,-90,-69"; byte[] s = new byte[1032]; s= str.getBytes("UTF-8"); System.out.println(Arrays.equals(faceFeature.getFeatureData(), s)); System.out.println(faceFeature.getFeatureData().length + "====" +s.length); System.out.println(new String(s, StandardCharsets.UTF_8)); //检索数据库已存在人脸信息比对 String dbImgPath = "D:\\Idea\\DB_images\\";//我这里是用了本地文件夹中的图片代替数据库(实际运用中肯定是保存特征码到数据库中来和被检测的对象进行比对) List<String> imageNames = getImageFileNames(dbImgPath); int getFlag = 0; for (String imageName : imageNames) { ImageInfo imageInfo2 = getRGBData(new File(dbImgPath + imageName));//数据库文件xxxxx List<FaceInfo> faceInfoList2 = new ArrayList<FaceInfo>(); errorCode = faceEngine.detectFaces(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(),imageInfo2.getImageFormat(), faceInfoList2); FaceFeature faceFeature2 = new FaceFeature(); errorCode = faceEngine.extractFaceFeature(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), imageInfo2.getImageFormat(), faceInfoList2.get(0), faceFeature2); //特征比对 FaceFeature targetFaceFeature = new FaceFeature(); targetFaceFeature.setFeatureData(faceFeature.getFeatureData()); FaceFeature sourceFaceFeature = new FaceFeature(); sourceFaceFeature.setFeatureData(faceFeature2.getFeatureData()); FaceSimilar faceSimilar = new FaceSimilar(); errorCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar); // //String s = new String(targetFaceFeature.getFeatureData()); //"GBK" "UTF-8" //System.out.println("-==- " + s); if (faceSimilar.getScore() > 0.7){ //转换为百分比 DecimalFormat df = new DecimalFormat("0.00%"); System.out.println("你好你是" + imageName.substring(0, imageName.indexOf("_")) + "吧。相似度:" + df.format(faceSimilar.getScore())); getFlag = 1; break; } } if (getFlag == 0) System.out.println("人员未识别到"); //设置活体测试 errorCode = faceEngine.setLivenessParam(0.5f, 0.7f); //人脸属性检测 FunctionConfiguration configuration = new FunctionConfiguration(); configuration.setSupportAge(true); configuration.setSupportFace3dAngle(true); configuration.setSupportGender(true); configuration.setSupportLiveness(true); errorCode = faceEngine.process(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList, configuration); //性别检测 List<GenderInfo> genderInfoList = new ArrayList<GenderInfo>(); errorCode = faceEngine.getGender(genderInfoList); if (genderInfoList.get(0).getGender() == 0)System.out.println("性别男"); if (genderInfoList.get(0).getGender() == 1)System.out.println("性别女"); //引擎卸载 errorCode = faceEngine.unInit(); } //遍历文件夹图片名称 public static List<String> getImageFileNames(String dbImgPath) { List<String> imageNames = new ArrayList<>(); File folder = new File(dbImgPath); File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isFile()) { String fileName = file.getName(); if (isImageFile(fileName)) { imageNames.add(fileName); } } } } return imageNames; } //文件名称筛选 public static boolean isImageFile(String fileName) { String extension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();//转为小写 return extension.equals("jpg") || extension.equals("jpeg") || extension.equals("png"); } //这里是一般前端会取图片或者视频取一帧给你 @Test public void GetImageStr() {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 String imgFile = "C:\\Users\\xxx\\Desktop\\lll.png";// 待处理的图片 InputStream in = null; byte[] data = null; String encode = null; // 返回Base64编码过的字节数组字符串 // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); try { // 读取图片字节数组 in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); encode = encoder.encode(data); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(encode); } }
其大概用法就是这样,要结合实际业务去调整。
标签:人脸识别,59,虹软,demo,60,61,67,68,new From: https://www.cnblogs.com/iRyz/p/17986846