虹軟產品地址:http://ai.arcsoft.com.cn/product/arcface.html
虹軟ArcFace功能簡介
人臉檢測
人臉跟蹤
人臉屬性檢測(性別、年齡)
人臉三維角度檢測
人臉對比
示例代碼說明,其中圖片操作部分用到了javacv,javacv中針對opencv進行了一些封裝,可以很好的對圖片進行處理。此處主要用到了opencv的幾個函數
加載圖片
IplImage img = cvLoadImage("e:/pic/g5.jpg");
格式轉換,將IplImage 轉為Mat,
OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
Mat mat = converterToMat.convert(converterToMat.convert(img));
對圖片,增加矩形框,此處用來圈出人臉的位置
rectangle(mat, new Rect(mrect.left,mrect.top,mrect.right-mrect.left,mrect.bottom-mrect.top), new Scalar(0, 255, 0, 1));
將mat數據,寫入圖片文件中
imwrite("e:/pic/detected.jpg", mat);
示例代碼如下:
package com.arcsoft.face;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
import static org.bytedeco.javacpp.opencv_imgcodecs.imwrite;
import static org.bytedeco.javacpp.opencv_imgproc.rectangle;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Rect;
import org.bytedeco.javacpp.opencv_core.Scalar;
import org.bytedeco.javacv.OpenCVFrameConverter;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
public class FaceDetectTest {
public static void main(String[] args) {
FaceEngine ins = FaceEngine.INSTANCE;
//SDK激活
ins.ASFActivation("6iYHk9hRsCNBJvFY7qhpaxnmmpFPnjWWpboYSafaz5hN", "GRwuiEVRTyeRUCHyihMP1B8Xf59zsQBtdZSbeQtfWWhg");
PointerByReference phEngine = new PointerByReference();
//SDK初始化,需要設置模式是video,還是image,配置人臉檢測的角度,engine需要支持的功能 mask,最大支持的人臉數量50,人臉大小16
int mask = FaceEngine.ASF_FACE_DETECT | FaceEngine.ASF_FACERECOGNITION | FaceEngine.ASF_AGE | FaceEngine.ASF_GENDER | FaceEngine.ASF_FACE3DANGLE;
NativeLong ret = ins.ASFInitEngine(FaceEngine.ASF_DETECT_MODE_IMAGE, OrientPriority.AFD_FSDK_OPF_0_ONLY, 16,50, mask, phEngine);
if (ret.longValue() != FaceEngine.MOK) {
System.out.println(ret.longValue());
}
//獲取圖片數據,此處使用javacv中封裝的對應opencv的方法完成
IplImage img = cvLoadImage("e:/pic/g5.jpg");
BytePointer bp = img.arrayData();
byte[] imgData = new byte[img.arraySize()];//圖片數據
bp.get(imgData);
//獲得engine對應的指針
Pointer hEngine = phEngine.getValue();
//當前系統版本
Version version = ins.ASFGetVersion(hEngine);
System.out.println(version.Version);
System.out.println(version.CopyRight);
System.out.println(version.BuildDate);
//進行人臉檢測,detectedFaces為輸出項
MultiFaceInfo detectedFaces = new MultiFaceInfo();
ins.ASFDetectFaces(hEngine, img.width(), img.height(), ASVL_COLOR_FORMAT.ASVL_PAF_RGB24_B8G8R8,imgData, detectedFaces);
System.out.println("detected face num:"+detectedFaces.faceNum);
//使用opencv的api對照片進行處理,將人臉區域畫出來,保存為一張新圖片
OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
Mat mat = converterToMat.convert(converterToMat.convert(img));
MRECT[] rects = detectedFaces.getFaceRects();
for (MRECT mrect : rects) {
rectangle(mat, new Rect(mrect.left,mrect.top,mrect.right-mrect.left,mrect.bottom-mrect.top), new Scalar(0, 255, 0, 1));
}
imwrite("e:/pic/detected.jpg", mat);
}
}
待做人臉檢測的圖片
程序識別后,並標識出人臉位置后,效果看起來很棒
再找一張人臉特別多的圖片,准確圈出所有人臉位置
使用已經封裝好的SDK進行人臉識別
https://www.jianshu.com/p/96636db03792
使用已經封裝好的SDK進行人臉識別【人臉特征提取比對】
https://www.jianshu.com/p/ab64464571da
虹軟ArcFace 2.0,java SDK使用、人臉信息識別(年齡、性別)
https://www.jianshu.com/p/cfe90f82be97
