作为二次人脸检测算法
import com.arcsoft.face.*;
import com.arcsoft.face.enums.ImageFormat;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.DataBufferByte;
import java.beans.beancontext.BeanContext;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class FaceEngineHR {
public static void main(String[] args) {
try {
new FaceEngineHR().FaceEngineHR();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void FaceEngineHR() throws InterruptedException {
String appId = "你的appid";
String sdkKey = "你的sdkKey";
FaceEngine faceEngine = new FaceEngine();
faceEngine.active(appId, sdkKey);
EngineConfiguration engineConfiguration = EngineConfiguration.builder().functionConfiguration(
FunctionConfiguration.builder()
.supportAge(true)
.supportFace3dAngle(true)
.supportFaceDetect(true)
.supportFaceRecognition(true)
.supportGender(true)
.build()).build();
faceEngine.init(engineConfiguration);
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
ImageInfo imageInfo = getRGBData(new File("D:\\worke3\\8.jpg"));
ImageInfo imageInfo2 = getRGBData(new File("D:\\worke3\\8.jpg"));
faceEngine.detectFaces(imageInfo.getRgbData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);
FaceFeature faceFeature2 = new FaceFeature();
faceEngine.extractFaceFeature(imageInfo2.getRgbData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature2);
FaceFeature faceFeature = new FaceFeature();
try {
faceEngine.extractFaceFeature(imageInfo.getRgbData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature);
} catch (Exception e) {
e.printStackTrace();
}
// //浜鸿劯瀵规瘮
FaceFeature targetFaceFeature = new FaceFeature();
targetFaceFeature.setFeatureData(faceFeature.getFeatureData());
FaceFeature sourceFaceFeature = new FaceFeature();
sourceFaceFeature.setFeatureData(faceFeature2.getFeatureData());
FaceSimilar faceSimilar = new FaceSimilar();
faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
float score = faceSimilar.getScore();
System.out.println(score);
}
public ImageInfo getRGBData(File file) {
if (file == null)
return null;
ImageInfo imageInfo;
try {
//灏嗗浘鐗囨枃浠跺姞杞藉埌鍐呭瓨缂撳啿锟?
BufferedImage image = ImageIO.read(file);
imageInfo = bufferedImage2ImageInfo(image);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return imageInfo;
}
private ImageInfo bufferedImage2ImageInfo(BufferedImage image) {
ImageInfo imageInfo = new ImageInfo();
int width = image.getWidth();
int height = image.getHeight();
// 浣垮浘鐗囧眳锟?
width = width & (~3);
height = height & (~3);
imageInfo.width = width;
imageInfo.height = height;
//鏍规嵁鍘熷浘鐗囦俊鎭柊寤轰竴涓浘鐗囩紦鍐插尯
BufferedImage resultImage = new BufferedImage(width, height, image.getType());
//寰楀埌鍘熷浘鐨剅gb鍍忕礌鐭╅樀
int[] rgb = image.getRGB(0, 0, width, height, null, 0, width);
//灏嗗儚绱犵煩锟? 缁樺埗鍒版柊鐨勫浘鐗囩紦鍐插尯锟?
resultImage.setRGB(0, 0, width, height, rgb, 0, width);
//杩涜鏁版嵁鏍煎紡鍖栦负鍙敤鏁版嵁
BufferedImage dstImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
if (resultImage.getType() != BufferedImage.TYPE_3BYTE_BGR) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
ColorConvertOp colorConvertOp = new ColorConvertOp(cs, dstImage.createGraphics().getRenderingHints());
colorConvertOp.filter(resultImage, dstImage);
} else {
dstImage = resultImage;
}
//鑾峰彇rgb鏁版嵁
imageInfo.rgbData = ((DataBufferByte) (dstImage.getRaster().getDataBuffer())).getData();
return imageInfo;
}
class ImageInfo {
public byte[] rgbData;
public int width;
public int height;
public byte[] getRgbData() {
return rgbData;
}
public void setRgbData(byte[] rgbData) {
this.rgbData = rgbData;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
}
------------------服务化-----------
private HttpRpc httpRpc = null;
/**
* td 图片地址
*/
private String imgFile = null;
/*线程同步工具类*/
private CountDownLatch countDownLatch = null;
/*特征值*/
private volatile DetectionResult detectionResults=null;
/*当前对应通行记录*/
private TDistinguishEntity tDistinguishEntity;
/*base64*/
private String base64;
/*is Check*/
private volatile boolean isNextRun = false;
/*http Param*/
private Map<String,String> parms = new HashMap<String, String>();
public BatchThread(String imageFile, CountDownLatch c, TDistinguishEntity td) {
if(imageFile==null||"".equals(imageFile)){
throw new IllegalArgumentException("图片地址不能为空!");
}
if(null== c) {
throw new IllegalArgumentException("线程同步不能为空!");
}
if(null==td) {
throw new IllegalArgumentException("通行记录不能为空!");
}
if("".equals(td.getPasspic())||null==td.getPasspic()) {
throw new IllegalArgumentException("通行记录图片不能为空!");
}
this.imgFile = imageFile;
this.countDownLatch = c;
this.tDistinguishEntity = td;
this.base64 = Base64Tool.encodeImage(imgFile);
if(Objects.isNull(this.base64)) {
isNextRun=false;
}
httpRpc = new HttpRpc();
}
@Override
public void run() {
parms.put("baseImage", this.base64);
String result = (String) httpRpc.call(PropertiesUtils.getConfig(PropertiesUtils.DATA_DETECTION), parms, null);
if("[]".equals(result)) {
rest();
return;
}
this.detectionResults = JSON.parseObject(result, DetectionResult.class);
countDownLatch.countDown();
base64 = null;
}
private void rest() {
this.isNextRun = true;
this.countDownLatch.countDown();
this.tDistinguishEntity.setDetection(1);
}
public DetectionResult getDetectionResults() {
return detectionResults;
}
public void setDetectionResults(DetectionResult detectionResults) {
this.detectionResults = detectionResults;
}
public TDistinguishEntity gettDistinguishEntity() {
return tDistinguishEntity;
}
public void settDistinguishEntity(TDistinguishEntity tDistinguishEntity) {
this.tDistinguishEntity = tDistinguishEntity;
}
public boolean isNextRun() {
return isNextRun;
}
public void setNextRun(boolean isNextRun) {
this.isNextRun = isNextRun;
}
public static ImageInfo getRGBData(File file) {
if (file == null)
return null;
ImageInfo imageInfo;
try {
//将图片文件加载到内存缓冲�?
BufferedImage image = ImageIO.read(file);
imageInfo = bufferedImage2ImageInfo(image);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return imageInfo;
}
private static ImageInfo bufferedImage2ImageInfo(BufferedImage image) {
ImageInfo imageInfo = new ImageInfo();
int width = image.getWidth();
int height = image.getHeight();
// 使图片居中
width = width & (~3);
height = height & (~3);
imageInfo.width = width;
imageInfo.height = height;
//根据原图片信息新建一个图片缓冲区
BufferedImage resultImage = new BufferedImage(width, height, image.getType());
//得到原图的rgb像素矩阵
int[] rgb = image.getRGB(0, 0, width, height, null, 0, width);
//将像素矩�? 绘制到新的图片缓冲区�?
resultImage.setRGB(0, 0, width, height, rgb, 0, width);
//进行数据格式化为可用数据
BufferedImage dstImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
if (resultImage.getType() != BufferedImage.TYPE_3BYTE_BGR) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
ColorConvertOp colorConvertOp = new ColorConvertOp(cs, dstImage.createGraphics().getRenderingHints());
colorConvertOp.filter(resultImage, dstImage);
} else {
dstImage = resultImage;
}
//获取rgb数据
imageInfo.rgbData = ((DataBufferByte) (dstImage.getRaster().getDataBuffer())).getData();
return imageInfo;
}
/*appKey*/
public static final String APPID=PropertiesUtils.getConfig("APPID");
/*appId*/
public static final String SDKKEY=PropertiesUtils.getConfig("SDKKEY");;
/*face++接口*/
public static String faceCountPost(String img1, String img2) {
FaceEngine faceEngine = null;
List<FaceInfo> faceInfoList = null;
ImageInfo imageInfo = null;
ImageInfo imageInfo2 = null;
WeakReference<FaceEngine> face = new WeakReference<FaceEngine>(new FaceEngine());
faceEngine = face.get();
faceEngine.active(APPID, SDKKEY);
EngineConfiguration engineConfiguration = EngineConfiguration.builder().functionConfiguration(
FunctionConfiguration.builder()
.supportAge(true)
.supportFace3dAngle(true)
.supportFaceDetect(true)
.supportFaceRecognition(true)
.supportGender(true)
.build()).build();
faceEngine.init(engineConfiguration);
faceInfoList = new ArrayList<FaceInfo>();
imageInfo = getRGBData(new File(img1));
imageInfo2 = getRGBData(new File(img2));
try {
faceEngine.detectFaces(imageInfo.getRgbData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);
} catch (Exception e) {
e.printStackTrace();
}
if(faceInfoList.isEmpty()) {
faceEngine.unInit();
faceEngine = null;
face = null;
System.gc();
return "000001";
}
FaceFeature faceFeature2 = new FaceFeature();
faceEngine.extractFaceFeature(imageInfo2.getRgbData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature2);
FaceFeature faceFeature = new FaceFeature();
faceEngine.extractFaceFeature(imageInfo.getRgbData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature);
// //人脸对比
FaceFeature targetFaceFeature = new FaceFeature();
targetFaceFeature.setFeatureData(faceFeature.getFeatureData());
FaceFeature sourceFaceFeature = new FaceFeature();
sourceFaceFeature.setFeatureData(faceFeature2.getFeatureData());
FaceSimilar faceSimilar = new FaceSimilar();
faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
float score = faceSimilar.getScore();
faceEngine.unInit();
faceEngine = null;
face = null;
System.gc();
return Float.toString(score);
}
特征比较算法:
public static double sim_distance(Double[] vector1, Double[] vector2) {
double distance = 0;
if (vector1.length == vector2.length) {
for (int i = 0; i < vector1.length; i++) {
double temp = Math.pow((vector1[i] - vector2[i]), 2);
distance += temp;
}
}
return distance;
}
注意几个文件位置!!