【4858美高梅】人脸识别小demo

人脸识别技术概念

所谓人脸识别技术,即按照人的脸部特征,对输入的人脸图象或者视频流举行判断,首先判断其是否留存人脸。假设存在人脸,则进一步的交由每个脸的岗位、大小和各类显要面部器官的职务音讯。并依照那些信息,进一步提取每个人脸中所蕴涵的地方特征,并将其与已知的人脸举办比较,从而识别每个人脸的身份。

人脸识别技术的发展前景

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

列贡士脸识别在手机APP上的一对应用

1.美图秀秀邪恶大测试:识别面部表情,给出分数和评价
2.百度图片识图效用
3.百度魔图APP推出了“PK大咖”效用,用户只需要采取一张自己的大头照,就足以由这个人脸识别技术跟明星拓展PK,找到与你面部形象最为相似的明星大咖
4.百度钱包APP拍照付只是说当你想买一款商品,却不知道商品的现实性音讯,这时候就可以用到百度钱包的拍照付,拍一下就能检索到商品,采用购买
4858美高梅,5.支出宝APP人脸识别登录
6.iPhoto
在苹果的iPhoto中,同样提供了人脸识别效率,用户可以将图纸中的人脸和人名相匹配,该功效通过脸部检测识别照片中的人物,再经过脸部识别找到与之特征相符的录像对象,帮你找到想找的人,甚至是海量的照片库也不费吹灰之力
7.图图搜是先找到Taobao上的同款,然后得到成品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

相关文章