说明: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,大功告成,我接下来一张图
稍后在淡蓝色的视图上会呈现人脸的各项信息。