利用第三方server實現的人臉識別技術(3)

說明:face.com已經被facebook收購,停止了人臉識別的服務了。        

(接上篇)在上篇的基礎之上我們獲得人臉的相關的信息,如何來用呢,下面我接下來我貼上的一部分的代碼:

首先這一個ViewContrller的頭文件

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate,UIActionSheetDelegate>
{
    NSTimer *_scanningTimer; //用於處理模擬的掃描的狀態
    BOOL    isUp;            //用於判斷圖片的位置
    UIView  *scanning;       //這個視圖在等待數據返回的時候用於模擬掃描的假象
    UIView *infoBackgroungView; //呈現背景的圖片
    UIImageView *imageView;     //呈現選中的圖片視圖
    
    
}
- (IBAction)selectPictrue:(id)sender;
@end

然後是我獲得的http請求的返回的值之後的收到的通知,這裏我要提取我需要的信息,同時更新當前view中的內容

- (void)getText:(NSNotification*)notification
{
    [scanning removeFromSuperview]; 
    for (UIView *view in [infoBackgroungView subviews]) {
        [view removeFromSuperview];
    }
    
    [_scanningTimer  invalidate];
    [_scanningTimer release];
    _scanningTimer = nil;
    NSDictionary *infoDictionary = [[NSMutableDictionary alloc]init];
    NSDictionary *dic = [[notification userInfo] objectForKey:@"PostData"];
    NSMutableString *string = [[NSMutableString alloc]init];
    if ([[dic objectForKey:@"status"] isEqualToString:@"fail"]) {
        [string appendFormat:@"識別失敗:%@\n",[dic objectForKey:@"message"]];
        return;
        
    }
    
    if ([[dic objectForKey:@"status"] isEqualToString:@"success"]) {
        [string appendFormat:@"%@\n",@"識別成功"];
        
    }
    NSMutableDictionary *rectangleInfo = [[NSMutableDictionary alloc]init];
    if ([dic objectForKey:@"photos"]) 
    {
        NSDictionary *photos = [[dic objectForKey:@"photos"]lastObject];
        if ([photos objectForKey:@"height"]) {
            [string appendFormat:@"圖片高度:%3f\n",[[photos objectForKey:@"height"]floatValue]];
            [infoDictionary setValue:[photos objectForKey:@"height"] forKey:@"height"];
            [rectangleInfo setValue:[photos objectForKey:@"height"] forKey:@"picHeight"];
            
        }
        if ([photos objectForKey:@"width"]) {
            [infoDictionary setValue:[photos objectForKey:@"width"] forKey:@"width"];
            [rectangleInfo setValue:[photos objectForKey:@"width"] forKey:@"picWidth"];
        }
        if ([photos objectForKey:@"tags"]&&[[photos objectForKey:@"tags"] count ] !=0 ) 
        {
            
            NSDictionary *array = [[photos objectForKey:@"tags"]lastObject];
            NSDictionary *attr = [array objectForKey:@"attributes"];
            if ([attr objectForKey:@"age_max"] ) {
                NSDictionary *dic = [attr objectForKey:@"age_max"];
                [string appendFormat:@"最大年紀是:%@,置信度是:%f\n",[dic objectForKey:@"value"],[[dic objectForKey:@"confidence"]floatValue]];
                [infoDictionary setValue:[dic objectForKey:@"value"] forKey:@"maxAge"];
            }
            if ([attr objectForKey:@"age_min"] ) {
                NSDictionary *dic = [attr objectForKey:@"age_min"];
                [string appendFormat:@"最小年紀是:%@,置信度是:%f\n",[dic objectForKey:@"value"],[[dic objectForKey:@"confidence"]floatValue]];
                [infoDictionary setValue:[dic objectForKey:@"value"] forKey:@"minAge"];
            }
            if ([attr objectForKey:@"gender"] ) {
                NSDictionary *dic = [attr objectForKey:@"gender"];
                [string appendFormat:@"性別是:%@,置信度是:%f\n",[dic objectForKey:@"value"],[[dic objectForKey:@"confidence"]floatValue]];
                [infoDictionary setValue:[dic objectForKey:@"value"] forKey:@"sex"];
            }
            if ([attr objectForKey:@"glasses"] ) {
                NSDictionary *dic = [attr objectForKey:@"glasses"];
                [string appendFormat:@"是否帶眼鏡是:%@ ,置信度是:%f\n",[[dic objectForKey:@"value"]boolValue ]?@"是":@"不是",[[dic objectForKey:@"confidence"]floatValue]];
                [infoDictionary setValue:[dic objectForKey:@"value"] forKey:@"glasses"];
            }
            if ([attr objectForKey:@"mood"] ) {
                NSDictionary *dic = [attr objectForKey:@"mood"];
                [string appendFormat:@"心情是:%@,置信度是:%f\n",[dic objectForKey:@"value"],[[dic objectForKey:@"confidence"]floatValue] ];
                [infoDictionary setValue:[dic objectForKey:@"value"] forKey:@"mood"];
            }
            if ([attr objectForKey:@"smiling"] ) {
                NSDictionary *dic = [attr objectForKey:@"smiling"];
                [string appendFormat:@"是否在笑:%@,置信度是:%f\n",[[dic objectForKey:@"value"]boolValue ]?@"是":@"不是",[[dic objectForKey:@"confidence"] floatValue]];
                [infoDictionary setValue:[dic objectForKey:@"value"] forKey:@"smiling"];
            }
           if ([attr objectForKey:@"lips"]) {
               NSDictionary *dic = [attr objectForKey:@"lips"];
               [infoDictionary setValue:[dic objectForKey:@"value"] forKey:@"lips"];
            }
            if ([array objectForKey:@"center"]) {
                NSDictionary *dic = [array objectForKey:@"center"];
                [rectangleInfo setValue:dic forKey:@"center"];
            }
            if ([array objectForKey:@"height"]) {
                NSString *height = [array objectForKey:@"height"];
                [rectangleInfo setValue:height forKey:@"faceHeight"];
            }
            if ([array objectForKey:@"width"]) {
                NSString *width = [array objectForKey:@"width"];
                [rectangleInfo setValue:width forKey:@"faceWidth"];
                
            }
           
         }
        else {
            
            UILabel *recognizeError = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 100, 34)];
            recognizeError.tag      = RECOGNIZEDERRORLABELTAG;
            recognizeError.text     = @"識別失敗";
            recognizeError.backgroundColor = [UIColor clearColor];
            recognizeError.textColor= [UIColor whiteColor];
            recognizeError.textAlignment = UITextAlignmentLeft;
            [infoBackgroungView addSubview:recognizeError];
            [recognizeError release];
            
            UIButton *reSelectImage = [[UIButton alloc]initWithFrame:CGRectMake(110,20, 170, 34)];
            [reSelectImage setTitle:@"重新選擇圖片" forState:UIControlStateNormal];
            [reSelectImage addTarget:self action:@selector(clickRecognize:) forControlEvents:UIControlEventTouchUpInside];
            reSelectImage.tag       = SELECTIMAGETAG;
            [infoBackgroungView addSubview:reSelectImage];
            [reSelectImage release];
            return;
        }

     
    }
    
    [self drawFaceRectangle:rectangleInfo];  //爲圖片劃框
    [rectangleInfo release];
    UIView *view = (UIView*)[self.view viewWithTag:INFOVIEWTAG];
    if(view)
    {
        [view removeFromSuperview];
    }
    // 這個informationVIew 是用來呈現我要的信息的視圖 
    InformationView *infoView = [[InformationView alloc]initWithFrame:CGRectMake(0, 366, 320, 96) withInfo:infoDictionary];
    infoView.tag = INFOVIEWTAG;
    [self.view addSubview:infoView];
    [infoView release];
    [infoDictionary release];
    UIButton *clickRecognize = [UIButton buttonWithType:UIButtonTypeCustom];
    clickRecognize.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"NavigationLeftUper"]];
    clickRecognize.frame     = CGRectMake(240, 7, 68, 30);
    clickRecognize.titleLabel.font = [UIFont systemFontOfSize:12];
    [clickRecognize setTitle:@"換張照片" forState:UIControlStateNormal];
    [clickRecognize setTag:RERECOGNIZEBUTTONTAG];
    [clickRecognize addTarget:self action:@selector(clickRecognize:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clickRecognize];
    //[clickRecognize release];
    
}

OK,大功告成,我接下來一張圖



 稍後在淡藍色的視圖上會呈現人臉的各項信息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章