微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android FaceDetector找不到任何面孔… findFace()每次都返回0

我正在尝试构建一个可以检测设备摄像头拍摄的照片中脸部数量的应用程序.到目前为止,我所拥有的代码如下所示.通过研究这里的问题,我认为这可能是一个问题,图片的分辨率对于FaceDetector来说太差了,但如果是这样的话,我不知道如何解决这个问题.如果情况并非如此,那么我对错误感到茫然.任何帮助深表感谢!
public class CrowdDetection extends Activity {

ImageView display;
ImageView pic;
Bitmap image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crowd_detection);

    display = (ImageView) findViewById(R.id.imageView2);

    Button takePicture = (Button) findViewById(R.id.button1);
    takePicture.setonClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,0);

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

    super.onActivityResult(requestCode,resultCode,data);

    setContentView(R.layout.detect_faces);

    pic = (ImageView) findViewById(R.id.imageView1);

    image = (Bitmap) data.getExtras().get("data");
    image = BitmapFactory.decodeFile(data.getData().getPath());
    pic.setimageBitmap(image);

    Button detect = (Button) findViewById(R.id.button2);
    detect.setonClickListener(new OnClickListener(){

        @Override
        public void onClick(View v){
            detectFaces();
        }
    });     
}

private void detectFaces() {

    setContentView(R.layout.display_crowd);

    int h = image.getHeight();
    int w = image.getWidth();
    int max = 10;

    FaceDetector detector = new FaceDetector(w,h,max);
    Face[] faces = new Face[max];

    ImageView pic2 = (ImageView) findViewById(R.id.imageView3);
    pic2.setimageBitmap(image);

    int facesFound = detector.findFaces(image,faces);

    TextView result = (TextView) findViewById(R.id.textView3);

    if(facesFound>5){
        result.setText("There are " + facesFound + " faces in this picture,therefore you have a crowd!");
    }
    else{
        result.setText("There are only " + facesFound + " faces in this picture,therefore you do not have a crowd!");
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.crowd_detection,menu);
    return true;
}

}

在这里先向您的帮助表示感谢!

解决方法

您无法检测任何面部的原因是您需要将位图转换为RGB 565.

使用此选项可将位图转换为RGB 565

BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.RGB_565;
image=BitmapFactory.decodeResource(getResources(),R.drawable.image,bitmapFatoryOptions);

原文地址:https://www.jb51.cc/android/313879.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐