【问题标题】:Android FaceDetector not finding any faces...findFace() returns 0 every timeAndroid FaceDetector 没有找到任何面孔...findFace() 每次都返回 0
【发布时间】:2013-11-05 05:05:13
【问题描述】:

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

}

提前感谢您的帮助!

【问题讨论】:

    标签: android


    【解决方案1】:

    您无法检测到任何人脸的原因是您需要将位图转换为 RGB 565。

    使用它来将您的位图转换为 RGB 565

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

    【讨论】:

    • 这是非常技术性的答案。谢谢
    • 因此我们只能从资源文件夹中获取位图,而不能从 sdCard 或设备内存中获取。
    • 有一种方法可以从 SD 卡中获取图像并从中创建位图。你可以从这个帖子(stackoverflow.com/questions/8710515/…)查看答案
    猜你喜欢
    • 2017-09-26
    • 2012-03-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2017-10-07
    相关资源
    最近更新 更多