Android人脸检测器未能找到任何人脸...每次findFace()返回0

9
我正在尝试构建一个应用程序,可以检测设备相机拍摄的照片中面部的数量。目前,我拥有的代码如下所示。通过在这里研究该问题,我认为可能是由于图片分辨率太低导致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;
}

}

事先感谢您的帮助!

1个回答

23

你无法检测到任何面孔的原因是需要将位图转换为RGB 565。

使用以下内容将位图转换为RGB 565

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

1
那是非常技术性的回答。谢谢。 - Murtaza Khursheed Hussain
因此,我们只能从资源文件夹中获取位图,而不能从设备的sdCard或内部存储器中获取。 - Amrit Chakradhari
有一种方法可以从SD卡获取图像并创建位图。您可以查看此线程的答案(https://dev59.com/CWoy5IYBdhLWcg3wMLSj) - Sandeep R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接