Face++人臉識(shí)別小demo

人臉識(shí)別技術(shù)概念

所謂人臉識(shí)別技術(shù),即基于人的臉部特征,對(duì)輸入的人臉圖象或者視頻流進(jìn)行判斷,首先判斷其是否存在人臉。如果存在人臉,則進(jìn)一步的給出每個(gè)臉的位置、大小和各個(gè)主要面部器官的位置信息。并依據(jù)這些信息,進(jìn)一步提取每個(gè)人臉中所蘊(yùn)涵的身份特征,并將其與已知的人臉進(jìn)行對(duì)比,從而識(shí)別每個(gè)人臉的身份。

人臉識(shí)別技術(shù)的發(fā)展前景

在這個(gè)刷臉的時(shí)代,人臉識(shí)別技術(shù)怎能不火。人臉識(shí)別技術(shù),是目前生物科技上在可行性,穩(wěn)定性和準(zhǔn)確性等專業(yè)技術(shù)指標(biāo)中數(shù)值最高的技術(shù)。也是目前各行各業(yè)安全保衛(wèi)中運(yùn)用最廣,效果最好的一種技術(shù)。在未來的幾年內(nèi),它必將超越指紋識(shí)別等生物技術(shù),成為生物識(shí)別技術(shù)領(lǐng)域的霸主。

列舉人臉識(shí)別在手機(jī)APP上的一些應(yīng)用

1.美圖秀秀邪惡大測(cè)試:識(shí)別面部表情,給出分?jǐn)?shù)和評(píng)價(jià)
2.百度圖片識(shí)圖功能
3.百度魔圖APP推出了“PK大咖”功能,用戶只需要選取一張自己的大頭照,就可以通過人臉識(shí)別技術(shù)跟明星進(jìn)行PK,找到與你面部形象最為相似的明星大咖
4.百度錢包APP拍照付只是說當(dāng)你想買一款商品,卻不知道商品的具體信息,這時(shí)候就可以用到百度錢包的拍照付,拍一下就能搜索到商品,選擇購(gòu)買
5.支付寶APP人臉識(shí)別登錄
6.iPhoto 在蘋果的iPhoto中,同樣提供了人臉識(shí)別功能,用戶可以將圖片中的人臉和人名相匹配,該功能通過臉部檢測(cè)辨別照片中的人物,再通過臉部識(shí)別找到與之特征相符的拍攝對(duì)象,幫你找到想找的人,甚至是海量的照片庫(kù)也不費(fèi)吹灰之力
7.圖圖搜是先找到淘寶上的同款,然后拿到產(chǎn)品tag,接著根據(jù)tag、主顏色等信息進(jìn)行二次查找。最基本的技術(shù)還是相同圖像查找,當(dāng)然也包含了商品主體識(shí)別。

Face++ 人臉識(shí)別demo

主要實(shí)現(xiàn)的功能是:給出兩張面孔,判斷兩張面孔的相似度
封裝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;
//觸發(fā)比較相似度的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;
//通過此標(biāo)記判斷選擇圖片要顯示到哪一個(gè)相框上
@property (nonatomic,assign) NSInteger imageTag;

@end

@implementation YYFaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self drawView];
}

- (void)drawView{
    //創(chuàng)建兩個(gè)按鈕,點(diǎn)擊事件為選擇照片
    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];
    
    //創(chuàng)建兩個(gè)相框,顯示圖片
    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;
    
    //相似度監(jiān)測(cè)按鈕
    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:@"相似度計(jì)算(%)" 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;
    
    
}

//設(shè)置第一張圖片
- (void)selectFirstPhoto{
    self.imageTag = 999;
    [self alertController];
}
//設(shè)置第二張圖片
- (void)selectSecondPhoto{
    self.imageTag = 888;
    [self alertController];
}

//相似度監(jiān)測(cè)
- (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:@"未檢測(cè)到五官" 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:@"未檢測(cè)到五官" 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:@"未檢測(cè)到五官" 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) {
        //處理點(diǎn)擊拍照
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            // 跳轉(zhuǎn)到相機(jī)或相冊(cè)頁(yè)面
            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:@"打開相機(jī)失敗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];
            [alert show];
        }
        
    }]];
    [alertController addAction: [UIAlertAction actionWithTitle: @"從相冊(cè)選取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        //處理點(diǎn)擊從相冊(cè)選取
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            // 跳轉(zhuǎn)到相機(jī)或相冊(cè)頁(yè)面
            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:@"打開相機(jī)失敗" 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容