Face++人脸识别小demo

作者: yyMae | 来源:发表于2016-01-27 14:02 被阅读4911次

    人脸识别技术概念

    所谓人脸识别技术,即基于人的脸部特征,对输入的人脸图象或者视频流进行判断,首先判断其是否存在人脸。如果存在人脸,则进一步的给出每个脸的位置、大小和各个主要面部器官的位置信息。并依据这些信息,进一步提取每个人脸中所蕴涵的身份特征,并将其与已知的人脸进行对比,从而识别每个人脸的身份。

    人脸识别技术的发展前景

    在这个刷脸的时代,人脸识别技术怎能不火。人脸识别技术,是目前生物科技上在可行性,稳定性和准确性等专业技术指标中数值最高的技术。也是目前各行各业安全保卫中运用最广,效果最好的一种技术。在未来的几年内,它必将超越指纹识别等生物技术,成为生物识别技术领域的霸主。

    列举人脸识别在手机APP上的一些应用

    1.美图秀秀邪恶大测试:识别面部表情,给出分数和评价
    2.百度图片识图功能
    3.百度魔图APP推出了“PK大咖”功能,用户只需要选取一张自己的大头照,就可以通过人脸识别技术跟明星进行PK,找到与你面部形象最为相似的明星大咖
    4.百度钱包APP拍照付只是说当你想买一款商品,却不知道商品的具体信息,这时候就可以用到百度钱包的拍照付,拍一下就能搜索到商品,选择购买
    5.支付宝APP人脸识别登录
    6.iPhoto 在苹果的iPhoto中,同样提供了人脸识别功能,用户可以将图片中的人脸和人名相匹配,该功能通过脸部检测辨别照片中的人物,再通过脸部识别找到与之特征相符的拍摄对象,帮你找到想找的人,甚至是海量的照片库也不费吹灰之力
    7.图图搜是先找到淘宝上的同款,然后拿到产品tag,接着根据tag、主颜色等信息进行二次查找。最基本的技术还是相同图像查找,当然也包含了商品主体识别。

    Face++ 人脸识别demo

    主要实现的功能是:给出两张面孔,判断两张面孔的相似度
    封装YYFaceViewController
    .h文件

    //
    //  YYFaceViewController.h
    //  Demo_Face++
    //
    //  Created by yyMae on 15/12/25.
    //  Copyright (c) 2015年 yyMae. All rights reserved.
    //
    #import <UIKit/UIKit.h>
    
    @interface YYFaceViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
    
    @end
    

    .m文件

    //
    //  YYFaceViewController.m
    //  Demo_Face++
    //
    //  Created by yyMae on 15/12/25.
    //  Copyright (c) 2015年 yyMae. All rights reserved.
    //
    
    #import "YYFaceViewController.h"
    #import "FaceppAPI.h"
    
    #define kwidth self.view.frame.size.width
    #define height self.view.frame.size.height
    #define btnBorder 40
    #define imgBorder 20
    
    @interface YYFaceViewController ()
    
    //显示图片的imageView
    @property (nonatomic,strong) UIImageView *firstImgV;
    @property (nonatomic,strong) UIImageView *secondImgV;
    //触发比较相似度的button
    @property (nonatomic,strong) UIButton *recognizedBtn;
    //五官的相似度
    @property (nonatomic,strong) NSString *eye;
    @property (nonatomic,strong) NSString *eyebrow;
    @property (nonatomic,strong) NSString *mouth;
    @property (nonatomic,strong) NSString *nose;
    @property (nonatomic,strong) NSString *similarity;
    @property (nonatomic,strong) UITextView *similarityView;
    //通过此标记判断选择图片要显示到哪一个相框上
    @property (nonatomic,assign) NSInteger imageTag;
    
    @end
    
    @implementation YYFaceViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        [self drawView];
    }
    
    - (void)drawView{
        //创建两个按钮,点击事件为选择照片
        UIButton *firstBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        firstBtn.frame = CGRectMake(btnBorder, 100, (kwidth - btnBorder * 3)/2, 30);
        [firstBtn setTitle:@"setFirstPhoto" forState:UIControlStateNormal];
        [firstBtn addTarget:self action:@selector(selectFirstPhoto) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:firstBtn];
        
        UIButton *secondBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        secondBtn.frame = CGRectMake(CGRectGetMaxX(firstBtn.frame) + btnBorder, 100, (kwidth - btnBorder * 3)/2, 30);
        [secondBtn setTitle:@"setSecondPhoto" forState:UIControlStateNormal];
        [secondBtn addTarget:self action:@selector(selectSecondPhoto) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:secondBtn];
        
        //创建两个相框,显示图片
        UIImageView *firstImgV = [[UIImageView alloc]initWithFrame:CGRectMake(imgBorder, 140, (kwidth - imgBorder *3)/2, (kwidth - imgBorder *3)/2 *height / kwidth)];
        firstImgV.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:firstImgV];
        self.firstImgV = firstImgV;
        
        UIImageView *secondImgV = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(firstImgV.frame) + imgBorder, 140, (kwidth - imgBorder *3)/2, (kwidth - imgBorder *3)/2 *height / kwidth)];
        secondImgV.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:secondImgV];
        self.secondImgV = secondImgV;
        
        //相似度监测按钮
        UIButton *recognizedBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        recognizedBtn.frame = CGRectMake(self.view.bounds.size.width / 2, CGRectGetMaxY(secondImgV.frame) + 20, (kwidth - btnBorder * 3)/2, 30);
        recognizedBtn.center = CGPointMake(self.view.bounds.size.width / 2, CGRectGetMaxY(secondImgV.frame) + 20);
        [recognizedBtn setTitle:@"相似度计算(%)" forState:UIControlStateNormal];
        [recognizedBtn addTarget:self action:@selector(recognized) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:recognizedBtn];
        recognizedBtn.enabled = NO;
        self.recognizedBtn = recognizedBtn;
        
        //添加输入框显示输出信息
        UITextView *similarityView = [[UITextView alloc]initWithFrame:CGRectMake((kwidth - 300) / 2, CGRectGetMaxY(recognizedBtn.frame) + 10, 300, 150)];
        similarityView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:similarityView];
        self.similarityView = similarityView;
        
        
    }
    
    //设置第一张图片
    - (void)selectFirstPhoto{
        self.imageTag = 999;
        [self alertController];
    }
    //设置第二张图片
    - (void)selectSecondPhoto{
        self.imageTag = 888;
        [self alertController];
    }
    
    //相似度监测
    - (void)recognized{
        //获取到两张面孔的face_id
        NSString *firstFace_id;
    
        NSData *firstImgVData = UIImageJPEGRepresentation(self.firstImgV.image, 0.6);
        FaceppResult *firstResult = [[FaceppAPI detection] detectWithURL:nil orImageData:firstImgVData];
        NSArray *array1 = firstResult.content[@"face"];
        
        if (array1.count == 1) {
            firstFace_id = [firstResult content][@"face"][0][@"face_id"];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"未检测到五官" delegate:self cancelButtonTitle:@"重新选择图片" otherButtonTitles: nil];
            [alert show];
    
            return;
        }
    
        
        NSString *secondFace_id;
        NSData *secondImgVData = UIImageJPEGRepresentation(self.secondImgV.image, 0.6);
        FaceppResult *secondResult = [[FaceppAPI detection] detectWithURL:nil orImageData:secondImgVData];
        NSArray *array2 = secondResult.content[@"face"];
        if (array2.count == 1) {
            secondFace_id = [secondResult content][@"face"][0][@"face_id"];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"未检测到五官" delegate:self cancelButtonTitle:@"重新选择图片" otherButtonTitles: nil];
            [alert show];
            return;
    
        }
        
        //比较二者的相似度
        FaceppResult *similarResult = [[FaceppAPI recognition] compareWithFaceId1:firstFace_id andId2:secondFace_id async:NO];
        if ([similarResult success]) {
            self.eye = [similarResult content][@"component_similarity"][@"eye"];
            self.eyebrow = [similarResult content][@"component_similarity"][@"eyebrow"];
            self.mouth = [similarResult content][@"component_similarity"][@"mouth"];
            self.nose = [similarResult content][@"component_similarity"][@"nose"];
            self.similarity = [similarResult content][@"similarity"];
            
            
            NSString *content = [NSString stringWithFormat:@"眼睛:%@\n眉毛:%@\n嘴巴:%@\n鼻子:%@\n综合:%@",self.eye,self.eyebrow,self.mouth,self.nose,self.similarity];
            self.similarityView.text = content;
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"未检测到五官" delegate:self cancelButtonTitle:@"error" otherButtonTitles: nil];
            [alert show];
    
            return;
        }
        
        
    
    }
    
    - (void)alertController{
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        //添加Button
        [alertController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            //处理点击拍照
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                // 跳转到相机或相册页面
                UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
                imagePickerController.delegate = self;
                imagePickerController.allowsEditing = YES;
                [self presentViewController:imagePickerController animated:YES completion:^{}];
            }else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"打开相机失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];
                [alert show];
            }
            
        }]];
        [alertController addAction: [UIAlertAction actionWithTitle: @"从相册选取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            //处理点击从相册选取
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                // 跳转到相机或相册页面
                UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
                
                imagePickerController.delegate = self;
                
                imagePickerController.allowsEditing = YES;
                
                
                [self presentViewController:imagePickerController animated:YES completion:^{}];
            }else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"打开相机失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];
                [alert show];
            }
        }]];
        [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
        [picker dismissViewControllerAnimated:YES completion:nil];
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        if (self.imageTag == 999) {
            self.firstImgV.image = image;
        }
        if (self.imageTag == 888) {
            self.secondImgV.image = image;
        }
        if (self.firstImgV.image && self.secondImgV.image) {
            self.recognizedBtn.enabled = YES;
        }
       
    }
    
    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
        
    }
    
    @end
    

    效果见下图:

    333.png

    相关文章

      网友评论

      • gofun:楼主有demo吗,能否给一份学习学习,谢谢,gofunink711823@163.com
      • 熙棋:求demo 18613993441@163.com
      • 来宝:楼主,有demo吗,发一份学习学习,460876003@qq.com
      • wg689:face++的githu b SDk 14年最后更新,下载demo 也不能用,楼主哪里下载的demo
        来宝:楼主,有demo吗,发一份学习学习,460876003@qq.com
        wg689:@yyMae 谢谢
        yyMae:@hjl_wg 官网下载的 http://www.faceplusplus.com.cn/dev-tools-sdks/
      • dbebbc694bfc:你用的FaceID sdk?还是Faceplusplus的API?
        yyMae:@我是彩笔 后者face++
      • yyMae:你网络的问题吧 ,我刚试了试,正常啊
      • OCSwift:人脸识别功能实现 必须好好看
      • 泾河小马:66666
        話說記住了有卵用
        。。。臉是個意象。。。嘿嘿😁

      本文标题:Face++人脸识别小demo

      本文链接:https://www.haomeiwen.com/subject/rtgdkttx.html