【问题标题】:Unable to detect face using FaceDetector in android无法在android中使用FaceDetector检测人脸
【发布时间】:2012-11-29 20:52:01
【问题描述】:

我需要检测用户面部并比较面部以验证我的应用程序,因为我使用 FaceDetector API 来检测用户面部。

当我运行我的代码时,它没有任何缺陷。但是它使检测到的人脸计数为零。

    public class AndroidFaceDetectorActivity extends Activity {
    private static final int TAKE_PICTURE_CODE = 100;
    private static final int MAX_FACES = 5;

    private Bitmap cameraBitmap = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button)findViewById(R.id.take_picture)).setOnClickListener(btnClick);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if(TAKE_PICTURE_CODE == requestCode){
                    processCameraImage(data);
            }
    }


    private void openCamera(){
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(intent, TAKE_PICTURE_CODE);
    }

    private void processCameraImage(Intent intent){
        setContentView(R.layout.detectlayout);

        ((Button)findViewById(R.id.detect_face)).setOnClickListener(btnClick);

        ImageView imageView = (ImageView)findViewById(R.id.image_view);

        cameraBitmap = (Bitmap)intent.getExtras().get("data");

        imageView.setImageBitmap(cameraBitmap);
    }

    private void detectFaces(){
        if(null != cameraBitmap){
                Log.d("FACE_RECOGNITION","CHECK");
                int width = cameraBitmap.getWidth();
                int height = cameraBitmap.getHeight();

                FaceDetector detector = new FaceDetector(width, height,AndroidFaceDetectorActivity.MAX_FACES);
                Face[] faces = new Face[AndroidFaceDetectorActivity.MAX_FACES];

                Bitmap bitmap565 = Bitmap.createBitmap(width, height, Config.RGB_565);
                Paint ditherPaint = new Paint();
                Paint drawPaint = new Paint();

                ditherPaint.setDither(true);
                drawPaint.setColor(Color.RED);
                drawPaint.setStyle(Paint.Style.STROKE);
                drawPaint.setStrokeWidth(2);

                Canvas canvas = new Canvas();
                canvas.setBitmap(bitmap565);
                canvas.drawBitmap(cameraBitmap, 0, 0, ditherPaint);

                int facesFound = detector.findFaces(bitmap565, faces);
                PointF midPoint = new PointF();
                float eyeDistance = 0.0f;
                float confidence = 0.0f;

                Log.i("FaceDetector", "Number of faces found: " + facesFound);

                if(facesFound > 0)
                {
                        for(int index=0; index<facesFound; ++index){
                                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 + ")");

                                canvas.drawRect((int)midPoint.x - eyeDistance , 
                                                                (int)midPoint.y - eyeDistance , 
                                                                (int)midPoint.x + eyeDistance, 
                                                                (int)midPoint.y + eyeDistance, drawPaint);
                        }
                }

                String filepath = Environment.getExternalStorageDirectory() + "/facedetect" + System.currentTimeMillis() + ".jpg";

                    try {
                            FileOutputStream fos = new FileOutputStream(filepath);

                            bitmap565.compress(CompressFormat.JPEG, 90, fos);

                            fos.flush();
                            fos.close();
                    } catch (FileNotFoundException e) {
                            e.printStackTrace();
                    } catch (IOException e) {
                            e.printStackTrace();
                    }

                    ImageView imageView = (ImageView)findViewById(R.id.image_view);

                    imageView.setImageBitmap(bitmap565);
        }
    }

    private View.OnClickListener btnClick = new View.OnClickListener() {
            //@Override
            public void onClick(View v) {
                    switch(v.getId()){
                            case R.id.take_picture:         openCamera();   break;
                            case R.id.detect_face:          detectFaces();  break;  
                    }
            }
    };
}

我做错了什么?

还有其他方法吗?

谢谢

【问题讨论】:

  • 为什么没人回答我的问题?
  • 你是如何构建那个 cameraBitmap 的?
  • @AlexCohn:我已经更新了上面的源代码。
  • @AlexCohn:上述代码有什么结果吗?
  • 对不起,我错过了你的更新。查看答案。

标签: android image-processing face-detection


【解决方案1】:

getExtras().get("data") for MediaStore.ACTION_IMAGE_CAPTURE intent 会生成分辨率非常低的位图(我相信它是 160x120 像素),它可以用作缩略图,但不足以让人脸检测发挥作用。

通常情况下,人脸检测在中等分辨率图像(例如 64x480 像素)上是可以的,您可以收到表单 Camera.previewCallback(),但这样您需要权限和代码来控制应用中的相机,您不能使用 意图

以下是 Android 上的人脸检测官方介绍:http://developer.android.com/guide/topics/media/camera.html#face-detection

如果你真的更喜欢这种方式,你可以使用getData() 来查找全分辨率的捕获图像,并将其转换为位图,例如

cameraBitmap = BitmapFactory.decodeFile(data.getData().getPath());

【讨论】:

    猜你喜欢
    • 2014-08-09
    • 1970-01-01
    • 2014-05-03
    • 2018-03-24
    • 2014-07-06
    • 2013-09-24
    • 2013-07-03
    • 2013-09-02
    • 2021-08-27
    相关资源
    最近更新 更多