Tengine-Kit 人脸检测及关键点
Tengine-Kit是一个在Android上的一个算法集合。目前的算法有,人脸检测及关键点,虹膜关键点,身体检测及关键点,手的检测及关键点。
1.build.gradle引入库
implementation 'com.tengine.tenginekit:core:0.0.1'
implementation 'com.tengine.tenginekit:face:0.0.1'
2.得到图像的byte数据
Bitmap bb = null;
try {
Drawable d = Drawable.createFromStream(getAssets().open("person.jpg"), null);
showImage.setImageDrawable(d);
bb = ((BitmapDrawable) d).getBitmap();
} catch (Exception e) {
e.printStackTrace();
}
//bitmap转bytes[]数据
private byte[] bitmap2Bytes(Bitmap image) {
// calculate how many bytes our image consists of
int bytes = image.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer
byte[] temp = buffer.array(); // Get the underlying array containing the
return temp;
}
3.初始化Tengine-Kit
KitCore.init(this,
AndroidConfig
.create() // 创建
.setNormalMode() // 由于这次用的图片,所以用Normal模式
.setDefaultFunc() // 设置默认功能,也就是检测&关键点
.setInputImageFormat(AndroidConfig.ImageFormat.RGBA) // 输入图像格式
.setInputImageSize(Image_w, Image_h) // 设置输入的图片大小
.setOutputImageSize((int) Image_w, (int) Image_h) // 设置输出的图像大小
);
4.函数调用人脸检测及关键点
Face.FaceDetect faceDetect = Face.detect(data);
List<FaceDetectInfo> faceDetectInfos = new ArrayList<>();
List<FaceLandmarkInfo> landmarkInfos = new ArrayList<>();
if (faceDetect.getFaceCount() > 0) {
faceDetectInfos = faceDetect.getDetectInfos();
landmarkInfos = faceDetect.landmark2d();
}
Log.d("#####", "Face Num: " + faceDetectInfos.size());
if (faceDetectInfos != null && faceDetectInfos.size() > 0) {
List<List<TenginekitPoint>> face_landmarks = new ArrayList<>();
for (int i = 0; i < faceDetectInfos.size(); i++) {
Rect rect = new Rect();
rect = faceDetectInfos.get(i).asRect();
for (int j = 0; j < landmarkInfos.get(i).landmarks.size() ; j++) {
// x 为关键点X坐标, y为关键点Y坐标
float x = landmarkInfos.get(i).landmarks.get(j).X;
float y = landmarkInfos.get(i).landmarks.get(j).Y;
}
}
}
5.释放
KitCore.release();
最终效果图: