Face++人臉識別小demo

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

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

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

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

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

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

Face++ 人臉識別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)測按鈕
    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)測
- (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) {
        //處理點(diǎn)擊拍照
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            // 跳轉(zhuǎn)到相機(jī)或相冊頁面
            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: UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        //處理點(diǎn)擊從相冊選取
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            // 跳轉(zhuǎn)到相機(jī)或相冊頁面
            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)容合作請聯(lián)系作者
  • 序言:七十年代末卷扮,一起剝皮案震驚了整個(gè)濱河市荡澎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌晤锹,老刑警劉巖摩幔,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異抖甘,居然都是意外死亡热鞍,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進(jìn)店門衔彻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來薇宠,“玉大人,你說我怎么就攤上這事艰额〕胃郏” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵柄沮,是天一觀的道長回梧。 經(jīng)常有香客問我,道長祖搓,這世上最難降的妖魔是什么狱意? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮拯欧,結(jié)果婚禮上详囤,老公的妹妹穿的比我還像新娘。我一直安慰自己镐作,他們只是感情好藏姐,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著该贾,像睡著了一般羔杨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上杨蛋,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天兜材,我揣著相機(jī)與錄音理澎,去河邊找鬼。 笑死护姆,一個(gè)胖子當(dāng)著我的面吹牛矾端,可吹牛的內(nèi)容都是我干的掏击。 我是一名探鬼主播卵皂,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼砚亭!你這毒婦竟也來了灯变?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤捅膘,失蹤者是張志新(化名)和其女友劉穎添祸,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體寻仗,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡刃泌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了署尤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片耙替。...
    茶點(diǎn)故事閱讀 40,424評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖曹体,靈堂內(nèi)的尸體忽然破棺而出俗扇,到底是詐尸還是另有隱情,我是刑警寧澤箕别,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布铜幽,位于F島的核電站,受9級特大地震影響串稀,放射性物質(zhì)發(fā)生泄漏除抛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一母截、第九天 我趴在偏房一處隱蔽的房頂上張望到忽。 院中可真熱鬧,春花似錦微酬、人聲如沸绘趋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽陷遮。三九已至,卻和暖如春垦江,著一層夾襖步出監(jiān)牢的瞬間帽馋,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留绽族,地道東北人姨涡。 一個(gè)月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像吧慢,于是被迫代替她去往敵國和親涛漂。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 圖像識別主要用到了兩個(gè)第三方的框架:OpenCV和TesseractOCR检诗,OpenCV用來做圖像處理匈仗,定位到身份...
    方弟閱讀 11,600評論 9 52
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,262評論 25 707
  • 1.人臉識別概述 人臉識別,是基于人的臉部特征信息進(jìn)行身份識別的一種生物識別技術(shù)逢慌。用攝像機(jī)或攝像頭采集含有人臉的圖...
    Jinwong閱讀 4,916評論 4 22
  • 在人臉識別屬性返回功能以范圍方面悠轩,較突出的是百度人臉識別與Face++,其次是云飛科技、科大訊飛攻泼、FaceCore...
    攸攸牧牧閱讀 11,608評論 2 39
  • 今天我們要談的是十七世紀(jì)火架,也就是我們一般所說的巴洛克時(shí)期。 巴洛克這個(gè)名詞原來的意思是形狀不規(guī)則的珍珠忙菠,這是巴洛克...
    嘉愛佐鳴唯愛鼬神閱讀 249評論 0 1