人脸识别及第三方库实现------Android

人脸识别的用处

人脸识别技术是指利用分析比较的计算机技术识别人脸。人脸识别是一项热门的计算机技术研究领域,其中包括人脸追踪侦测,自动调整影像放大,夜间红外侦测,自动调整曝光强度等技术

人脸识别(face recognizaton)按顺序可以大体上分为四个部分,即人脸检测(face detection),人脸对齐(face alignment),人脸校验(face verification)和人脸识别(face identification)。 人脸检测就是在一张图片中找到人脸所处的位置,即将人脸圈出来,比如拍照时数码相机自动画出人脸。人脸对齐就是在已经检测到的人脸的基础上,自动找到人脸上的眼睛鼻子嘴和脸轮廓等标志性特征位置。人脸校验就是判断两张脸是不是同一个人。人脸识别就是给定一张脸,判断这张脸是谁。

最近几年人脸识别被炒到了风口上,相对应的第三方平台也比较多,如face++,科大讯飞,阅面科技,包括
腾讯等等都有自己的人脸数据库,以及开放的api接口,、故在android平台集成人脸检测以及人脸识别技术也变得容易许多。

在这里插入图片描述

人脸识别实现

  • 自行选取相关的第三方平台及其开放的api接口

  • 先获取相应第三方平台的APPID、API Key和secret key

    如比较热门的face++平台就需要在face++网站中注册并且创建一个试用(试用免费)的api key
    在这里插入图片描述
    这个APPID、API Key和secret key获取主要取决于选择调用的第三方平台,作者在这实现的实例是调用一个第三方库,他可支持百度云获取的APPID、API Key和secret key。

获取APPID、API Key和secret key

一、在百度云网站中创建应用
百度云网址点这
在这里插入图片描述
二、填写相关应用信息 (选取的接口应根据自身要实现的功能进行选取api调用)
在这里插入图片描述

三、完成创建,获取到APPID、API Key和secret key

APPID、API Key和secret key

调用第三方库接口(导入相关jar包)

在这里插入图片描述
jar包:https://pan.baidu.com/s/1A4ktfhkFcxS-ga9-d4H4rQ
提取码:3reh

在这里插入图片描述

调用相应的接口方法进行人脸识别(该库中为AipOcr函数进行人脸识别)

  • AipOcr函数解析

    • 构造函数:
public AipOcr(String appId, String apiKey, String secretKey) {
        super(appId, apiKey, secretKey);
    }

传入参数就是上面获取到的appId,apiKey,secretKey

  • 调用图片是获取相关属性

  • 通过图片的二进制数据和想要获取图片的相关属性(性别,年龄等)

public JSONObject basicGeneral(byte[] image, HashMap<String, String> options) {
        AipRequest request = new AipRequest();
        this.preOperation(request);
        String base64Content = Base64Util.encode(image);
        request.addBody("image", base64Content);
        if (options != null) {
            request.addBody(options);
        }

        request.setUri("https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic");
        this.postOperation(request);
        return this.requestServer(request);
    }
  • 通过图片的本地路径和想要获取图片的相关属性(性别,年龄等)
public JSONObject basicAccurateGeneral(String image, HashMap<String, String> options) {
        try {
            byte[] imgData = Util.readFileByBytes(image);
            return this.basicAccurateGeneral(imgData, options);
        } catch (IOException var4) {
            var4.printStackTrace();
            return AipError.IMAGE_READ_ERROR.toJsonResult();
        }
    }
  • 通过图片的url路径和想要获取图片的相关属性(性别,年龄等)
public JSONObject generalUrl(String url, HashMap<String, String> options) {
        AipRequest request = new AipRequest();
        this.preOperation(request);
        request.addBody("url", url);
        if (options != null) {
            request.addBody(options);
        }

        request.setUri("https://aip.baidubce.com/rest/2.0/ocr/v1/general");
        this.postOperation(request);
        return this.requestServer(request);
    }

在此选取第一种方法进行调用

实例代码

实例代码地址:https://github.com/liuyzh25/Android/tree/master/FaceIdentify

  • 获取图片,可从相册中选择和调用手机相机拍摄

  • 从相册中选择

getImage=(Button)findViewById(R.id.getImage);
        getImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in=new Intent(Intent.ACTION_PICK);
                in.setType("image/*");
                startActivityForResult(in,PHOTO_ALBUM);
            }
        });
  • 调用手机相机拍摄
    先更改调用权限
    在这里插入图片描述
    进行调用
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
        }
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
        }

Camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File outputImage=new File(Environment.getExternalStorageDirectory()+File.separator+"face.jpg");
                try{
                    if(outputImage.exists()){
                        outputImage.delete();
                    }
                    outputImage.createNewFile();
                }catch (IOException e){
                    e.printStackTrace();
                }
                imageUri=Uri.fromFile(outputImage);
                ImagePath=outputImage.getAbsolutePath();//拍摄后图片的存储路径
                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(intent,CAMERA);
            }
        });
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //从相册中选择的图片
        if(requestCode==PHOTO_ALBUM){
            if(data!=null){
                Uri uri=data.getData();
                Cursor cursor=getContentResolver().query(uri,null,null,null,null);
                cursor.moveToNext();
                ImagePath=cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
                cursor.close();
                resizePhoto();
                myPhoto.setImageBitmap(myBitmapImage);
                Log.i("图片路径",ImagePath);
            }
        }
        //相机拍摄的图片
        else if(requestCode==CAMERA){
            try{
                resizePhoto();
                myPhoto.setImageBitmap(myBitmapImage);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    //调整图片的比例,使其大小小于1M,能够显示在手机屏幕上
    public void resizePhoto(){
        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=true;//返回图片宽高信息
        BitmapFactory.decodeFile(ImagePath,options);
        //让图片小于1024
        double radio=Math.max(options.outWidth*1.0d/1024f,options.outHeight*1.0d/1024f);
        options.inSampleSize=(int)Math.ceil(radio);//向上取整倍数
        options.inJustDecodeBounds=false;//显示图片
        myBitmapImage=BitmapFactory.decodeFile(ImagePath,options);
    }
  • 调用函数进行人脸识别
detect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                res=null;
                detect_tip.setVisibility(View.VISIBLE);
                detect_tip.setText("识别中...");
               if(myBitmapImage==null){
                   myBitmapImage=BitmapFactory.decodeResource(getResources(),R.mipmap.face2);
                   bitmapSmall=Bitmap.createBitmap(myBitmapImage,0,0,myBitmapImage.getWidth(),myBitmapImage.getHeight());
               }
               else{//由于有些图片在一些型号手机角度会倾斜,需要进行摆正
                   int degree=getPicRotate(ImagePath);
                   Matrix m=new Matrix();
                   m.setRotate(degree);
                   bitmapSmall=Bitmap.createBitmap(myBitmapImage,0,0,myBitmapImage.getWidth(),myBitmapImage.getHeight(),m,true);
               }
               //将图片由路径转为二进制数据流
                ByteArrayOutputStream stream=new ByteArrayOutputStream();
                //图片转数据流
                bitmapSmall.compress(Bitmap.CompressFormat.JPEG,100,stream);
                final byte[] arrays=stream.toByteArray();
                //网络申请调用函数进行人脸识别
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HashMap<String,String> options=new HashMap<>();
                        options.put("face_fields","age,gender,race,beauty,expression");//人脸属性:年龄,性别,肤色,颜值,笑容
                        AipFace client=new AipFace("10734368","6cvleSFbyRIRHzhijfYrHZFj","SDnCUfrtH0lgrK01HgTe2ZRLNsmCx5xy");
                        client.setConnectionTimeoutInMillis(2000);
                        client.setSocketTimeoutInMillis(6000);
                        res=client.detect(arrays,options);
                        try{
                            Message message = Message.obtain();
                            message.what = 1;
                            message.obj = res;
                            handler.sendMessage(message);
                        }catch (Exception e){
                            e.printStackTrace();
                            Message message = Message.obtain();
                            message.what = 2;
                            handler.sendMessage(message);
                        }
                    }
                }).start();
            }
        });
    }
  • 将获取的网络数据进行处理
private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                JSONObject res=(JSONObject) msg.obj;
                face_resultNum=res.optString("result_num");
                if(Integer.parseInt(face_resultNum)>=1) {
                    try {
                        JSONArray js = new JSONArray(res.optString("result"));
                        face_age = js.optJSONObject(0).optString("age");
                        face_gender = js.optJSONObject(0).optString("gender");
                        if (face_gender.equals("female")) {
                            face_gender = "女";
                        } else {
                            face_gender = "男";
                        }
                        face_race = js.optJSONObject(0).optString("race");
                        if (face_race.equals("yellow")) {
                            face_race = "黄种人";
                        } else if (face_race.equals("white")) {
                            face_race = "白种人";
                        } else if (face_race.equals("black")) {
                            face_race = "黑种人";
                        }else if(face_race.equals("arabs")){
                            face_race = "阿拉伯人";
                        }
                        int express = Integer.parseInt(js.optJSONObject(0).optString("expression"));
                        if (express == 0) {
                            face_expression = "无";
                        } else if (express == 1) {
                            face_expression = "微笑";
                        } else {
                            face_expression = "大笑";
                        }
                        face_beauty = js.optJSONObject(0).optString("beauty");
                        double  beauty=Math.ceil(Double.parseDouble(face_beauty)+25);
                        if(beauty>=100){//对获得的颜值数据进行一定处理,使得结果更为合理
                            beauty=99.0;
                        }
                        else if(beauty<70){
                            beauty+=10;
                        }
                        else if(beauty>80 && beauty<90){
                            beauty+=5;
                        }
                        else if(beauty>=90 && beauty<95){
                            beauty+=2;
                        }
                        face_beauty=String.valueOf(beauty);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    detect_tip.setVisibility(View.GONE);
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(faceRecognition.this);
                    String[] mItems = {"性别:" + face_gender, "年龄:" + face_age, "肤色:" + face_race, "颜值:" + face_beauty, "笑容:" + face_expression};
                    alertDialog.setTitle("人脸识别报告").setItems(mItems, null).create().show();
                }else{
                    detect_tip.setVisibility(View.VISIBLE);
                    detect_tip.setText("图片不够清晰,请重新选择");
                }
            }else{
                detect_tip.setVisibility(View.VISIBLE);
                detect_tip.setText("图片不够清晰,请重新选择");
            }
        }
    };

ps:layout布局相关(可自行设计)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:background="@color/color2"
        android:padding="5sp"
        android:gravity="center"
        android:text="火眼金睛:一张图看穿你的岁月"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/face2"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:id="@+id/myPhoto"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/detect_tip"
        android:text="识别中..."/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:text="选图"
            android:background="@drawable/button_decoration"
            android:textColor="@color/white"
            android:textSize="25sp"
            android:layout_weight="1"
            android:id="@+id/getImage"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:text="拍照"
            android:background="@drawable/button_decoration"
            android:textColor="@color/white"
            android:textSize="25sp"
            android:layout_weight="1"
            android:id="@+id/Camera"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/button_decoration"
            android:textColor="@color/white"
            android:textSize="25sp"
            android:id="@+id/detect"
            android:text="识别"/>
    </LinearLayout>
</LinearLayout>

代码运行结果:

https://s1.ax1x.com/2020/06/02/tY8qrF.jpg
在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值