简体   繁体   English

无法在Android中使用FaceDetector检测到脸部

[英]Unable to detect face using FaceDetector in android

I need to detect the user face and also compare the face to authenticate my application,for that I used FaceDetector API to detect the user face. 我需要检测用户面部并比较面部以验证我的应用程序,因为我使用FaceDetector API检测用户面部。

When i run my code it works without any defects.But it gives detected faces count as Zero. 当我运行我的代码时,它没有任何缺陷。但是它会将检测到的面孔计数为零。

    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;  
                    }
            }
    };
}

What i did wrong? 我做错了什么?

or 要么

Is any other way to do that? 还有其他方法吗?

Thanks 谢谢

getExtras().get("data") for MediaStore.ACTION_IMAGE_CAPTURE intent produces very low-resolution bitmap (I believe it's 160x120 px) which could work as a thumbnail, but is not enough for face detection to do its job. MediaStore.ACTION_IMAGE_CAPTURE意图的getExtras().get("data")会生成分辨率很低的位图(我认为是160x120 px),可以用作缩略图,但不足以进行人脸检测。

Normally face detection is OK on medium-res images (eg 64x480 px) that you can receive form Camera.previewCallback() , but this way you need permissions and code that controls the camera in your app, you cannot use an intent for that. 通常,可以从Camera.previewCallback()接收的中等分辨率图像(例如64x480 px)上,人脸检测是可以的,但是通过这种方式,您需要在应用中控制相机的权限和代码,因此您不能为此使用意图

Here is the official into to face detection on Android: http://developer.android.com/guide/topics/media/camera.html#face-detection . 这是在Android上进行脸部检测的官方网站: http : //developer.android.com/guide/topics/media/camera.html#face-detection

If you really prefer it this way, you may use getData() to find the captured image at its full resolution, and convert it into a bitmap, like 如果您真的喜欢这种方式,则可以使用getData()来以全分辨率找到捕获的图像,并将其转换为位图,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM