基于Face++实现的人脸检测(年龄检测)Android程序

本项目是本人大二上人工智能导论课的小demo。
主要内容是Android+Face++实现的图片人脸检测及年龄检测。

1 SDK配置
基于Face++实现的人脸检测(年龄检测)Android程序_第1张图片

//配置Face++的
//Face++的api对象定义
    FaceDetecter detecter = null;
    HttpRequests request = null;// 在线api

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        detectThread = new HandlerThread("detect");
        detectThread.start();
        detectHandler = new Handler(detectThread.getLooper());

        //初始化布局控件,设置初始的检测图片
        ageTextView = (TextView) findViewById(R.id.age);
        imageView = (ImageView) findViewById(R.id.imageview);
        curBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.linzhiling);
        imageView.setImageBitmap(curBitmap);

        // 创建detecter
        detecter = new FaceDetecter();
        detecter.init(this, "01c699dd07e89b675e115b4a151936c8");

        // FIXME 替换成申请的key
        request = new HttpRequests("01c699dd07e89b675e115b4a151936c8",
                "P5IJQ5tnIGcUjmOrRJ7-OTiXZYtSD3bn");

2 按钮的监听

//按钮的监听方法重写
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.pick:
            ageTextView.setVisibility(TextView.INVISIBLE);
            startActivityForResult(new Intent("android.intent.action.PICK",
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
                    REQUEST_GET_PHOTO);
            break;
        case R.id.detect:
            detectHandler.post(new Runnable() {

                @Override
                public void run() {

                    Log.i("test--zhiqian", "ddd");
                    System.out.println("-------zhiqian--------");
                    Face[] faceinfo = detecter.findFaces(curBitmap);// 进行人脸检测
                    if (faceinfo == null) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "未发现人脸信息",
                                        Toast.LENGTH_LONG).show();

                            }
                        });
                        return;
                    }

                    Log.i("test-----", "aaa");
                    System.out.println("---------------");
                    // 在线api交互
                    try {

                        JSONObject jsonObject = request.offlineDetect(
                                detecter.getImageByteArray(),
                                detecter.getResultJsonString(),
                                new PostParameters());
                        ageString = jsonObject.getJSONArray("face")
                                .getJSONObject(0)
                                .getJSONObject("attribute")
                                .getJSONObject("age").getString("value");
                    } catch (FaceppParseException e) {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace(); 
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    final Bitmap bit = getFaceInfoBitmap(faceinfo, curBitmap);
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            ageTextView.setVisibility(TextView.VISIBLE);
                            ageTextView.setText(ageString);
                            imageView.setImageBitmap(bit);
                            System.gc();
                        }
                    });
                }
            });
        }
    }

3 切换照片的方法

//切换照片,将照片返回并显示在界面上
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case REQUEST_GET_PHOTO: {
                if (data != null) {
                    final String str;
                    Uri localUri = data.getData();
                    String[] arrayOfString = new String[1];
                    arrayOfString[0] = "_data";
                    Cursor localCursor = getContentResolver().query(localUri,
                            arrayOfString, null, null, null);
                    if (localCursor == null)
                        return;
                    localCursor.moveToFirst();
                    str = localCursor.getString(localCursor
                            .getColumnIndex(arrayOfString[0]));
                    localCursor.close();
                    if ((curBitmap != null) && (!curBitmap.isRecycled()))
                        curBitmap.recycle();
                    curBitmap = getScaledBitmap(str, 600);
                    imageView.setImageBitmap(curBitmap);
                }
                break;
            }
            }

        }
    }

4 最后说明
以下为项目csdn下载地址
http://download.csdn.net/detail/chenzhao2013/8801369
以下为项目github下载地址
https://github.com/chenzhao2013/FaceDetect_Six
csdn下载需要1个积分,如果你常常混迹于csdn,积分比较多的话,就用
csdn账号下载一下,如果你没有csdn账号,就github下载吧。

你可能感兴趣的:(Android)