开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 3268|回复: 0

face++人脸识别集成方法汇总

[复制链接]

0

精华

0

贡献

0

赞扬

帖子
6
软币
158
在线时间
10 小时
注册时间
2014-6-4
发表于 2014-7-17 16:45:22 | 显示全部楼层 |阅读模式
5)初始化SDK
在AppDelegate.m文件中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里添加如下代码
1
[FaceppAPI initWithApiKey:API_KEY andApiSecret:API_SECRET andRegion:APIServerRegionCN];
2
    [FaceppAPI setDebugMode:YES];
6)创建一个imageView,从相册选择一张图片进行人脸检测
在.h文件
1
@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
2
{
3
    UIImagePickerController *imagePicker;
4
}
5

6
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
7
- (IBAction)PickFromPhoto:(id)sender;
在.m文件
01
- (IBAction)PickFromPhoto:(id)sender {
02
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
03
        imagePicker.delegate = self;
04
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
05
        [self presentModalViewController:imagePicker animated:YES];
06
    }
07
}
08
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
09
{
10
    UIImage *sourceImage = info[UIImagePickerControllerOriginalImage];
11
    [self performSelectorInBackground:@selector(detectWithImage:) withObject: sourceImage];
12
    [picker dismissModalViewControllerAnimated:YES];
13
}
7)根据相册选择的图片进行人脸检测
1
- (void)detectWithImage:(UIImage *)image{
2
    FaceppResult *result = [[FaceppAPI detection]detectWithURL:nil orImageData:UIImageJPEGRepresentation(image, 0.5) mode:FaceppDetectionModeNormal attribute:FaceppDetectionAttributeNone];
3
}
8)获取人脸的大小范围
01
if (result.success) {
02
        double image_width = [[result content][@"img_width"]doubleValue]*0.01f;
03
        double image_height = [[result content][@"img_height"]doubleValue]*0.01f;
04
int face_count = [[result content][@"face"]count];
05
        for (int i = 0; i<face_count; i++) {
06
            double width = [[result content][@"face"][i][@"position"][@"width"]doubleValue];
07
            double height = [[result content][@"face"][i][@"position"][@"height"]doubleValue];
08
            //获取到得人脸范围
09
            CGRect rect = CGRectMake(([[result content][@"face"][i][@"position"][@"center"][@"x"]doubleValue]-width/2)*image_width, ([[result content][@"face"][i][@"position"][@"center"][@"y"]doubleValue]-height/2)*image_height, width*image_width, height*image_height);
10
}
11
}
9)管理Person列表
1
personNames = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
2
for (int i = 0; i<[personNames count]; i++) {
3
//根据personName删除对应一组Person
4
[[FaceppAPI person]deleteWithPersonName:[personNames objectAtIndex:i] orPersonId:nil];
5
//根据personName创建一个Person
6
[[FaceppAPI person]createWithPersonName:[personNames objectAtIndex:i] andFaceId:[NSArray arrayWithObject:face_id] andTag:nil andGroupId:nil orGroupName:nil];}
10)管理Faceset列表
01
//创建一个faceset
02
    facesetName = @"sample_faceset";
03
    [[FaceppAPI faceset]deleteWithFacesetName:facesetName orFacesetId:nil];
04
    FaceppResult *createResult = [[FaceppAPI faceset]createWithFacesetName:facesetName andFaceId:faceIds andTag:nil];
05
    if ([createResult success]) {
06
        self.textView.text = [NSString stringWithFormat:@"成功在faceSet中添加%@个face,faceSetName:%@",[createResult content][@"added_face"],[createResult content][@"faceset_name"]];
07
    }
08
    /*
09
     在一个faceset内进行search之前,必须先对该faceset进行Train
10
     当Faceset中的信息被修改之后(增加,删除了Face等),为了保证结果与最新数据一致,Faceset应当被重新train. /train/search
11
     */
12
    [[FaceppAPI train]trainSynchronouslyWithId:nil orName:facesetName andType:FaceppTrainSearch refreshDuration:1.0f timeout:10.0f];
11)在Faceset里搜索最相似的face
01
/*
02
     在faceset里搜索最相似的face
03
     faceset_id或faceset_name
04
     */
05
    FaceppResult *searchResult = [[FaceppAPI recognition]searchWithKeyFaceId:face_id andFacesetId:nil orFacesetName:facesetName];
06
    NSMutableArray *similarityArray = [NSMutableArray array];
07
    if ([searchResult success]) {
08
        for (int i=0; i<3; i++) {
09
            NSString *similarity = [searchResult content][@"candidate"][i][@"similarity"];
10
            [similarityArray addObject:similarity];
11
            self.textView.text = [NSString stringWithFormat:@"与faceSet中三张图片的相似度分别为%@",similarityArray];
12
        }}


回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-3-29 05:48

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表