使用face++做人脸,年龄,性别识别

package com.example.administrator.testdemo.face;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import com.example.administrator.testdemo.bean.FaceMessage;

import java.util.List;

/**
 * Created by Administrator on 2017/8/16.
 */

public class BitmapCanvas  {
  private   Bitmap bitmap;
  private List<FaceMessage>  messageList;

    public Bitmap getBitmap() {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }
    public BitmapCanvas(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public List<FaceMessage> getMessageList() {
        return messageList;
    }

    public void setMessageList(List<FaceMessage> messageList,CanvasFinish c) {
        this.messageList = messageList;
        draw(c);
    }

    public void draw(CanvasFinish c){
        if (messageList!=null){
            Canvas  canvas =new Canvas(bitmap);
           // canvas.drawRect(getRectF(),getpaint());
            for (int i = 0; i <messageList.size() ; i++) {
                canvas.drawRect(messageList.get(i).getRectF(),getpaint());
                canvas.drawText(messageList.get(i).getGender()+"年龄"+messageList.get(i).getAge(),messageList.get(i).getPoint().x,messageList.get(i).getPoint().y,textPaint());
            }
            c.canvasfinish();
        }

    }

    private Paint getpaint(){
      Paint  paint =new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5f);
        return paint;
    }
    private Paint textPaint(){
        Paint  paint =new Paint();
        paint.setColor(Color.BLUE);
         paint.setTextSize(40f);
        return paint;
    }

}
public interface CanvasFinish {
    void canvasfinish();
}
package com.example.administrator.testdemo.bean;

import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;

import java.util.List;

/**
 * Created by Administrator on 2017/8/15.
 */

public class FaceMessage {
   private String gender;
    private int age;
    private RectF rectF;

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender=gender.equals("Male")?"男":"女";
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public RectF getRectF() {
        return rectF;
    }

    public void setRectF(RectF rectF) {
        this.rectF = rectF;
    }
    public void setRectF(int size,int top,int left) {
       this.rectF=new RectF(left,top,left+size,top+size);
    }


    public PointF  getPoint(){
        if (rectF!=null){
            PointF  point =new PointF(rectF.left,rectF.top);
            return point;
        }
        return null;
    }
}
package com.example.administrator.testdemo.face;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.testdemo.Log.LogShow;
import com.example.administrator.testdemo.R;
import com.example.administrator.testdemo.bean.FaceMessage;
import com.megvii.cloud.http.CommonOperate;
import com.megvii.cloud.http.FaceSetOperate;
import com.megvii.cloud.http.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    TextView mTextView;
    String key = "aRKh7sDhT536YLHORdXOFFBqJOeF5Gzv";//api_key
    String secret = "zrfIe28avDUP4z36mEy9vg2csDXszxB8";//api_secret

    String imageUrl = "http://pic1.hebei.com.cn/003/005/869/00300586905_449eedbb.jpg";
    StringBuffer sb = new StringBuffer();
    long start=System.currentTimeMillis();
    private File file;
    private ImageView img;
    private Bitmap bitmap;
    private BitmapCanvas bitmacanvas;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mains);
        img= (ImageView) findViewById(R.id.img);
        bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.timg3).copy(Bitmap.Config.RGB_565,true);
        bitmacanvas=new BitmapCanvas(bitmap);
        LogShow.logShow(bitmap.getWidth());
        LogShow.logShow(bitmap.getHeight());
        LogShow.logShow("===========================================");
        img.setImageBitmap(bitmap);
        new Thread(new Runnable() {
            @Override
            public void run() {
                post();
            }
        }).start();

    }
    public void post(){
        //  bitmacanvas.draw();

        ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
        bytes = byteArrayOutputStream.toByteArray();
        if (bytes.length>0){
            start=System.currentTimeMillis();
            handler.sendEmptyMessage(0);
        }
    }

    private byte[] bytes;
    private Handler handler =new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==2){
                img.setImageBitmap(bitmap);
                return;
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //  ArrayList<String> faces = new ArrayList<>();
                    CommonOperate commonOperate = new CommonOperate(key, secret, false);
                    try {
                        Response response1 = commonOperate.detectByte(bytes, 0, "gender,age");
                      if (getFaceToken(response1)){
                         bitmacanvas.setMessageList(facemessages, new CanvasFinish() {
                             @Override
                             public void canvasfinish() {
                                 handler.sendEmptyMessage(2);
                             }
                         });
                      }


                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }
    };

    private boolean getFaceToken(Response response) throws JSONException {
        if(response.getStatus() != 200){
            return false;
        }
        String res = new String(response.getContent());
         jsonParse(res);
        return true;
    }
    private List<FaceMessage> facemessages;
    private void jsonParse(String value){
        if (facemessages==null){
            facemessages=new ArrayList<>();
        }else {
            facemessages.clear();
        }
        try {
            JSONObject jsonObject =new JSONObject(value);
            JSONArray faces = jsonObject.getJSONArray("faces");
            for (int i = 0; i <faces.length() ; i++) {
                LogShow.logShow(faces.get(i).toString());
                FaceMessage faceonew=new FaceMessage();
                faceonew.setGender( faces.getJSONObject(i).getJSONObject("attributes").getJSONObject("gender").getString("value"));
                faceonew.setAge(faces.getJSONObject(i).getJSONObject("attributes").getJSONObject("age").getInt("value"));
                int  size=faces.getJSONObject(i).getJSONObject("face_rectangle").getInt("width");
                int top=  faces.getJSONObject(i).getJSONObject("face_rectangle").getInt("top");
                int left=  faces.getJSONObject(i).getJSONObject("face_rectangle").getInt("left");
                faceonew.setRectF(size,top,left);
                facemessages.add(faceonew);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bitmap!=null){
            bitmap.recycle();
        }
    }
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值