Android NDK开发:SeetaFace2实现人脸检测
2019-08-26 本文已影响0人
itfitness
目录
效果展示
代码解释
这里的SeetaFace2库的引入请查看:Android NDK开发:SeetaFace2人脸识别算法简介
这里进行人脸检测所需要的图像模式为BGR,所以我们需要将原本的RGBA图像转为BGR图像,好在SeetaFace2案例中给出了转换的方法,这里我封装成了一个工具类:
public class ConvertUtil {
/**
* 转换生成SeetaImageData
* @param bitmap
* @return
*/
public static SeetaImageData ConvertToSeetaImageData(Bitmap bitmap) {
Bitmap bmp_src = bitmap.copy(Bitmap.Config.ARGB_8888, true); // true is RGBA
//SeetaImageData大小与原图像一致,但是通道数为3个通道即BGR
SeetaImageData imageData = new SeetaImageData(bmp_src.getWidth(), bmp_src.getHeight(), 3);
imageData.data = getPixelsBGR(bmp_src);
return imageData;
}
/**
* 提取图像中的BGR像素
* @param image
* @return
*/
public static byte[] getPixelsBGR(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 data.
byte[] pixels = new byte[(temp.length/4) * 3]; // Allocate for BGR
// Copy pixels into place
for (int i = 0; i < temp.length/4; i++) {
pixels[i * 3] = temp[i * 4 + 2]; //B
pixels[i * 3 + 1] = temp[i * 4 + 1]; //G
pixels[i * 3 + 2] = temp[i * 4 ]; //R
}
return pixels;
}
}
下面是Activity中的代码:
public class FaceDetectorActivity extends AppCompatActivity {
private Button mBt;
private ImageView mImg;
private FaceDetector2 faceDetector;
private SeetaRect[] faceRects;
private Bitmap bitmap;
private SeetaImageData seetaImageData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facedetector);
//将模型拷贝到SD卡中
//FileUtil.CopyAssets(this,"SeetaFaceDetector2.0.ats",Environment.getExternalStorageDirectory()+ File.separator+"SeetaFaceDetector2.0.ats");
initView();
initFace();
mBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//如果图像中检测出人脸了再进行绘制
if(faceRects.length>0){
//这里必须进行copy否则修改不了
Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
//利用Bitmap创建Canvas,为了在图像上绘制人脸区域
Canvas canvas = new Canvas(copy);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
//绘制出所有的检测出来的人脸的区域
for(int i = 0 ; i < faceRects.length ; i++){
paint.setColor(Color.BLUE);
SeetaRect faceRect = faceRects[i];
Rect rect = new Rect(faceRect.x,faceRect.y,faceRect.x+faceRect.width,faceRect.y+faceRect.height);
canvas.drawRect(rect,paint);
}
mImg.setImageBitmap(copy);
}
}
});
}
/**
* 初始化人脸检测器
*/
private void initFace() {
//初始化检测器(参数是模型在SD卡的位置)
faceDetector = new FaceDetector2(Environment.getExternalStorageDirectory()+ File.separator+"seetaface"+File.separator+"SeetaFaceDetector2.0.ats");
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.heying);
//利用SeetaFace2提供的转换方法获取SeetaRect(人脸识别结果)
seetaImageData = ConvertUtil.ConvertToSeetaImageData(bitmap);
faceRects = faceDetector.Detect(seetaImageData);
}
private void initView() {
mBt = findViewById(R.id.bt_face);
mImg = findViewById(R.id.img);
}
@Override
protected void onDestroy() {
super.onDestroy();
faceDetector.dispose();
}
}