我正在用相机捕捉图像。我有以下图像:
我正在尝试通过以下代码从此图像中检测人脸:
private Bitmap detectFaces(Bitmap imageBitmap) {
Bitmap background_image = imageBitmap.copy(Bitmap.Config.RGB_565, true);
if((1==(background_image.getWidth()%2))){
background_image = Bitmap.createScaledBitmap(background_image,
background_image.getWidth()+1, background_image.getHeight(), false);
}
imageBitmap = background_image.copy(Bitmap.Config.RGB_565, true);
int width = imageBitmap.getWidth();
int height = imageBitmap.getHeight();
// Call face detector object
FaceDetector detector = new FaceDetector(width, height,
10);
Face[] faces = new Face[10];
Bitmap bitmap565 = imageBitmap.copy(Bitmap.Config.RGB_565, true);
//Bitmap.createBitmap(width, height, Config.RGB_565);
Paint ditherPaint = new Paint();
Paint drawPaint = new Paint();
// Set properties of the rectangle that should appear for a detected
// face
ditherPaint.setDither(true);
drawPaint.setColor(Color.GREEN);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeWidth(3);
// Create a canvas and draw the imagebitmap over the canvas
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap565);
canvas.drawBitmap(imageBitmap, 0, 0, ditherPaint);
// trigger the face detector function and store the number of faces
// detected as int paramter faceFound
int facesFound = detector.findFaces(bitmap565, faces);
// This parameters find the midpoint, eyeDistance and confidence in
// a face
PointF midPoint = new PointF();
float eyeDistance = 0.0f;
float confidence = 0.0f;
Log.i("FaceDetector", "Number of faces found: " + facesFound);
Toast.makeText(getApplicationContext(), "Number of faces found: " + facesFound,
Toast.LENGTH_LONG).show();
if (facesFound > 0) {
// Select detected faces, one after one
for (int index = 0; index < facesFound; ++index) {
// Get mid point, eye distance and confidence of the face
faces[index].getMidPoint(midPoint);
eyeDistance = faces[index].eyesDistance();
confidence = faces[index].confidence();
Log.i("FaceDetector", "Confidence: " + confidence
+ ", Eye distance: " + eyeDistance
+ ", Mid Point: (" + midPoint.x + ", " + midPoint.y
+ ")");
try {
// Draw a rectange depending upon face parameters.
// Kindly note that this is just for displaying the
// rectangle
// canvas.drawRect((float) midPoint.x - (eyeDistance * 2),
// (float) midPoint.y - (eyeDistance * 2),
// (int) midPoint.x + (eyeDistance * 2),
// (int) midPoint.y + (eyeDistance * 2), drawPaint);
// And this is for cropping the detected face from the
// image
Bitmap croppedBitmap = Bitmap.createBitmap(imageBitmap,
(int) (midPoint.x - (eyeDistance * 2)),
(int) (midPoint.y - (eyeDistance * 2)),
(int) (eyeDistance * 4),
(int) (eyeDistance * 4));
bitmap565 = croppedBitmap.copy(Bitmap.Config.RGB_565, true);
} catch (Exception e) {
/*
* Toast.makeText( getApplicationContext(),
* "Too many faces/face size is too small, kindly go back and try again..."
* , Toast.LENGTH_SHORT).show();
*/
}
}
}
else
Toast.makeText(getApplicationContext(), "The Face is not detected",
Toast.LENGTH_LONG).show();
return bitmap565 ;
}
但是代码说检测到的人脸是0。实际上当我从远处捕捉图像时,它说检测到的人脸的数量是0。为什么?如何从 1 米距离拍摄的图像中检测人脸?任何建议都有很大帮助。