• 虹软人脸识别应用开发过程分享


    之前写了一个人脸识别demo,现在有时间向大家分享一波干货。  

    虹软的人脸识别是应用与离线开发的,因为不需要网络,所以它的识别速度较快。
    好了,废话不多说,接下来就开始教大家怎样使用了。 
    1. 首先就是去官网申请APPKEY,各种密匙,然后在下载jar包,这些就不一一给大家讲解了。注意一下,要在app的gradle里面加上下面这句话,不然可能会造成so库加载不了的错误。      

    sourceSets {   
         main {         
       jniLibs.srcDirs = ['libs']       
       }    
    }

    2.接下里就需要进行开发了。
    就拿人脸检测的功能来说吧,首先需要 
    对引擎初始化,

       AFD_FSDKEngine engine1 = new AFD_FSDKEngine();   AFD_FSDKError err = engine1.AFD_FSDK_InitialFaceEngine(Config.APP_ID, Config.FD_KEY, AFD_FSDKEngine.AFD_OPF_0_HIGHER_EXT, 16, 5);

    我们还需要一个集合,用来存放我们检测到的人脸,

    List<AFD_FSDKFace> result = new ArrayList<AFD_FSDKFace>();//新建AFD_FSDKFacejihe,用于存放识别的人脸信息

    接下来我们就可以进行人脸的检测了,但是对于照片的选取和格式是有要求的,所以我们需要对照片进行格式处理一下。

    Bitmap bitmap1 = decodeImage(path1);//path是照片的路径,先选取照片,转化为bitmap   
    byte[] data1 = getNv21(bitmap1);//再将bitmap转化为NV21格式的

    下面是工具类decodeImage和getNv21的代码:

    //getNv21 和 decodeImage 是照片格式的转化工具    
    public byte[] getNv21(Bitmap mBitmap) {       
             byte[] data = new byte[mBitmap.getWidth() * mBitmap.getHeight() * 3 / 2];        
             ImageConverter convert = new  ImageConverter();        
             convert.initial(mBitmap.getWidth(), mBitmap.getHeight(), ImageConverter.CP_PAF_NV21);      
             if (convert.convert(mBitmap, data)) {     
                    Log.e("TAG", "convert ok!");     
             }   
               convert.destroy();     
            return data;   
       }  
      public static Bitmap decodeImage(String path) {    
                      Bitmap res;      
                      try {     
                            ExifInterface exif = new ExifInterface(path);          
                            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);        
                            BitmapFactory.Options op = new BitmapFactory.Options();    
                            op.inSampleSize = 1;      
                            op.inJustDecodeBounds = false;            //op.inMutable = true;          
                            res = BitmapFactory.decodeFile(path, op);            //rotate and scale.         
                           Matrix matrix = new Matrix();        
                           if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {            
                                    matrix.postRotate(90);     
                             } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {        
                                    matrix.postRotate(180);       
                            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {       
                                    matrix.postRotate(270);           
                             }    
                    Bitmap temp = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), matrix, true);          
                    Log.d("com.arcsoft", "check target Image:" + temp.getWidth() + "X" + temp.getHeight());      
                   if (!temp.equals(res)) {         
                         res.recycle();  
                         }  
                       return temp;       
                        } catch (Exception e) {     
                             e.printStackTrace(); 
                         }   
                     return null;  
      }

    对格式进行转化完成后,就开始进行人脸的检测了。

    err = engine1.AFD_FSDK_StillImageFaceDetection(data1, bitmap1.getWidth(), bitmap1.getHeight(), AFD_FSDKEngine.CP_PAF_NV21, result);        Log.e("TAG", "getBit: " + result.size());

    我们可以查看集合result的size,来确定是否检测到人脸。
    在代码的最后,一定要对初始化的引擎进行销毁处理。不然程序会因为内存问题而崩溃。 

    engine1.AFD_FSDK_UninitialFaceEngine(); 

    人脸对比是在人脸检测的基础上进行的,在一张照片上先检测到人脸的信息,然后再将人脸的信息进行比对。  

    List result = new ArrayList(); 

    上面已经介绍了,检测到的人脸信息都是存放在result的集合中的, 
    然后是创建两个存放人脸点位信息的类

    AFR_FSDKFace face1 = new AFR_FSDKFace();
    AFR_FSDKFace face2 = new AFR_FSDKFace(); 将检测到的人脸信息的点位信息存放到 face类中        
     //新建两个AFR_FSDKFace类,保存人脸特征信息                 
     AFR_FSDKFace face1 = new AFR_FSDKFace();                
      AFR_FSDKFace face2 = new AFR_FSDKFace();                //对人脸特征信息的检测               
           er = engine_camera.AFR_FSDK_ExtractFRFeature(data_image, 
                                                                                         bitmap_idcard.getWidth(),
                                                                                         bitmap_idcard.getHeight(),      
                                                                                         AFR_FSDKEngine.CP_PAF_NV21,      
                                                                                         new Rect(result_image.get(0).getRect()), 
                                                                                         result_image.get(0).getDegree(),
                                                                                         face1);           
         er = engine_camera.AFR_FSDK_ExtractFRFeature(data, 
                                                                                         wid, 
                                                                                          hei, 
                                                                                         AFR_FSDKEngine.CP_PAF_NV21, 
                                                                                         new Rect(result_fd.get(0).getRect()),
                                                                                         result_fd.get(0).getDegree(),
                                                                                         face2);

    最后的比对的相似度信息存放在score中, float  score_face = score.getScore();

    我们可以通过这种方式得到 我们想要的相似度信息,最后得到的数据是float类型的。

    **注意!
      在使用照片的时候,分辨率大小最好是偶数的,不然会发生未知的错误。
      在进行人脸信息提取的时候,会耗时,耗时的时长,是根据设备的CPU处理能力来说的。

  • 相关阅读:
    Android Studio安装与配置
    T-SQL:qualify和window 使用(十七)
    《c#图解教程》
    c# 创建,加载,修改XML文档
    c# 使用迭代器来创建可枚举类型
    C#上手练习3(while、do while语句)(添加机器人聊天)
    C#上手练习2(FOR语句)
    C#上手练习1(if语句、Swich语句)
    解决java导入project出现红叉
    ABAP ALV显示前排序合并及布局显示
  • 原文地址:https://www.cnblogs.com/feishixin123/p/9780169.html
Copyright © 2020-2023  润新知