【问题标题】:opencv android facerecognitionopencv 安卓人脸识别
【发布时间】:2014-09-18 20:50:17
【问题描述】:

这是我从http://www.java2s.com/Open-Source/Android_Free_Code/Facebook/Download_Free_code_face_recognition.htm下载的人脸识别码。

package face_recognition;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.LinkedList;

import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.objdetect.CascadeClassifier;

import com.face_recognition.R;
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.cpp.opencv_objdetect;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

public class ImageRecognitionActivity extends Activity {

private Mat imageMat;

private FacebookImage facebookImage = null;

private Context context=this;

private ImageView imageView; 

private Bitmap fbImage;

private CascadeClassifier mCascade;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view3_layout);
    imageView = (ImageView) findViewById(R.id.imageview);

    Intent intent = getIntent();   
    Bundle b = intent.getExtras();  
    String chosenImageUrl = (String) b.get("chosenImageUrl");   

    try {                           
        facebookImage = new FacebookImage(chosenImageUrl);    
        fbImage = facebookImage.getPicture(); 


       imageMat =                 org.opencv.android.Utils.bitmapToMat(fbImage.copy(Bitmap.Config.ARGB_8888,true)); 

注意: //我在这一行遇到错误。它说它需要一个垫子,一旦我添加它它说不能//从垫子转换为无效..我该如何解决这个问题?

    } catch (MalformedURLException e) { /* ... */ e.printStackTrace();
    } catch (IOException e)     { /* ... */ e.printStackTrace();
    }

    Loader.load(opencv_objdetect.class);  

    try {

        InputStream is = context.getResources().openRawResource(R.raw.haarcascade_frontalface_alt);
        File cascadeDir = context.getDir("cascade", Context.MODE_PRIVATE);
        File cascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
        FileOutputStream os = new FileOutputStream(cascadeFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        mCascade = new CascadeClassifier(cascadeFile.getAbsolutePath());
        if (mCascade.empty()) {
                Log.e("TAG", "Failed to load cascade classifier");
                mCascade = null;
        } else
            Log.i("TAG", "Loaded cascade classifier from " + cascadeFile.getAbsolutePath());

        cascadeFile.delete();
        cascadeDir.delete();

    } catch (IOException e) {
        e.printStackTrace();
        Log.e("TAG", "Failed to load cascade. Exception thrown: " + e);
    }

    LinkedList<Rect> facesdetection = new LinkedList<Rect>();

    mCascade.detectMultiScale(imageMat, facesdetection);

    Bitmap imageBitmap = null;
    for (Rect r : facesdetection){  
        Core.rectangle(imageMat, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 2); 
    }

    imageBitmap = Bitmap.createBitmap(imageMat.cols(), imageMat.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(imageMat, imageBitmap);
    imageView.setImageBitmap(imageBitmap); 
}

}

【问题讨论】:

  • 上面是做人脸检测(人脸还是不是人脸?),不是人脸识别(是谁?)

标签: android opencv face-recognition facebook-sdk-3.1


【解决方案1】:

bitmapToMat 不返回任何内容。您必须将结果 Mat 作为参数传递,如下所示:

imageMat = new Mat();
org.opencv.android.Utils.bitmapToMat(fbImage.copy(Bitmap.Config.ARGB_8888,true), imageMat);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-18
    • 2014-05-19
    • 2012-11-07
    • 2019-10-26
    • 2020-03-24
    • 2015-07-05
    • 2016-09-18
    • 2018-09-15
    相关资源
    最近更新 更多