[英]FaceDetector does not detect faces
I get the images from device camera: 我从设备相机获取图像:
public synchronized void onPreviewFrame(byte[] data, Camera camera) {
//Get image from data
}
Decode the images to RGB: 将图像解码为RGB:
int[] rgb = decodeYUV420SPtoRGB(data, width, height);
RGB to Bitmap: RGB到位图:
Bitmap bitmap = rgbToBitmap(rgb,width, height);
Print images in ImageView to make sure that conversion is fine: 在ImageView中打印图像以确保转换正常:
iv.setImageBitmap(bitmap);
Detect num faces: 检测面数:
FaceDetector faceDet = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 1);
Face[] faceList = new Face[1];
int faces = faceDet.findFaces(bitmap, faceList);
Log.e("NumFaces: ",faces + "");
Decode functions: 解码功能:
private int[] decodeYUV420SPtoRGB(byte[] yuv420sp, int width, int height) {
if (yuv420sp == null) throw new NullPointerException();
final int frameSize = width * height;
int[] rgb = new int[frameSize];
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & (yuv420sp[yp])) - 16;
if (y < 0) y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0) r = 0;
else if (r > 262143) r = 262143;
if (g < 0) g = 0;
else if (g > 262143) g = 262143;
if (b < 0) b = 0;
else if (b > 262143) b = 262143;
rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
}
}
return rgb;
}
public static Bitmap rgbToBitmap(int[] rgb, int width, int height) {
if (rgb == null) throw new NullPointerException();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
bitmap.setPixels(rgb, 0, width, 0, 0, width, height);
return bitmap;
}
I get always 0 in Log.e("NumFaces: ",faces + ""); 我在Log.e(“ NumFaces:”,faces +“”);中总是为0。 While I'm seeing in the imageView faces, real faces and faces in photos.
当我在imageView中看到面孔,真实面孔和照片中的面孔时。 I tried it with physical devices, 2.3 (bad camera) and 4.2 (good camera HD)
我在物理设备,2.3(不良相机)和4.2(优质相机高清)上尝试过
Edit: in 4.2 device I am getting 800x480 frames in onPreviewFrame. 编辑:在4.2设备中,我在onPreviewFrame中得到800x480帧。
Any idea what happens? 知道会发生什么吗?
通过将设备水平而不是垂直握在手中,已经解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.