face++人脸识别

来源:互联网 发布:网站建设和优化排名 编辑:程序博客网 时间:2024/05/06 23:21

简介:        Face++是北京旷视科技有限公司旗下的人脸识别云服务平台,Face++平台通过提供云端API、离线SDK、以及面向用户的自主研发产品等形式,将人脸识别技术广泛应用到互联网及移动应用场景中。Face++为广大开发者提供了简单易用的API,开发者可以轻松搭建属于自己的云端身份认证、用户兴趣挖掘、移动体感交互、社交娱乐分享等多种类型的应用。

        Face++提供的技术服务包括人脸检测、人脸分析和人脸识别,主要说明如下:

                1)人脸检测:可以从图片中快速、准确的定位面部的关键区域位置,包括眉毛、眼睛、鼻子、嘴巴等。
                2)人脸分析:可以从图片或实时视频流中分析出人脸的性别(准确度达96%)、年龄、种族等多种属性。

                3)人脸识别:可以快速判定两张照片是否为同一个人,或者快速判定视频中的人像是否为某一位特定的人。

  现面是一个简单的人脸识别demo:

人脸显示区:

    _pickerImage = [[UIImagePickerController alloc] init];//初始化    UIView * aview = [[UIView alloc] initWithFrame:CGRectMake(0, 120, self.view.bounds.size.width, self.view.bounds.size.height-120)];    [self.view addSubview:aview];

人脸轮廓分析:

- (UIImage *)fixOrientation:(UIImage *)aImage {        // No-op if the orientation is already correct    if (aImage.imageOrientation == UIImageOrientationUp)        return aImage;        // We need to calculate the proper transformation to make the image upright.    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.    CGAffineTransform transform = CGAffineTransformIdentity;        switch (aImage.imageOrientation) {        case UIImageOrientationDown:        case UIImageOrientationDownMirrored:            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);            transform = CGAffineTransformRotate(transform, M_PI);            break;                    case UIImageOrientationLeft:        case UIImageOrientationLeftMirrored:            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);            transform = CGAffineTransformRotate(transform, M_PI_2);            break;                    case UIImageOrientationRight:        case UIImageOrientationRightMirrored:            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);            transform = CGAffineTransformRotate(transform, -M_PI_2);            break;        default:            break;    }        switch (aImage.imageOrientation) {        case UIImageOrientationUpMirrored:        case UIImageOrientationDownMirrored:            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);            transform = CGAffineTransformScale(transform, -1, 1);            break;                    case UIImageOrientationLeftMirrored:        case UIImageOrientationRightMirrored:            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);            transform = CGAffineTransformScale(transform, -1, 1);            break;        default:            break;    }        // Now we draw the underlying CGImage into a new context, applying the transform    // calculated above.    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,                                             CGImageGetBitsPerComponent(aImage.CGImage), 0,                                             CGImageGetColorSpace(aImage.CGImage),                                             CGImageGetBitmapInfo(aImage.CGImage));    CGContextConcatCTM(ctx, transform);    switch (aImage.imageOrientation) {        case UIImageOrientationLeft:        case UIImageOrientationLeftMirrored:        case UIImageOrientationRight:        case UIImageOrientationRightMirrored:            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);            break;                    default:            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);            break;    }        // And now we just create a new UIImage from the drawing context    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);    UIImage *img = [UIImage imageWithCGImage:cgimg];    CGContextRelease(ctx);    CGImageRelease(cgimg);    return img;}
获取面部轮廓:

-(void) detectWithImage: (UIImage*) image {        //    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];        /*     *    mode : 检测模式可以是normal(默认) 或者 oneface 。在oneface模式中,检测器仅找出图片中最大的一张脸。     *     *    attribute:可以是none或者由逗号分割的属性列表。默认为gender, age, race, smiling。目前支持的属性包括:gender, age, race, smiling, glass, pose     */        //脸部姿势分析结果中包括 :pitch_angle, roll_angle, yaw_angle,分别对应抬头,旋转(平面旋转),摇头。单位为角度。     FaceppResult *result = [[FaceppAPI detection] detectWithURL:nil orImageData:UIImageJPEGRepresentation(image, 1) mode:FaceppDetectionModeOneFace attribute:FaceppDetectionAttributeAll];    if (result.success) {        double image_width = [[result content][@"img_width"] doubleValue] *0.01f;        double image_height = [[result content][@"img_height"] doubleValue] * 0.01f;                UIGraphicsBeginImageContext(image.size);        [image drawAtPoint:CGPointZero];        CGContextRef context = UIGraphicsGetCurrentContext();        CGContextSetRGBFillColor(context, 0, 0, 1.0, 1.0);        CGContextSetLineWidth(context, image_width * 0.7f);                // draw rectangle in the image        int face_count = [[result content][@"face"] count];        for (int i=0; i<face_count; i++) {            double width = [[result content][@"face"][i][@"position"][@"width"] doubleValue];            double height = [[result content][@"face"][i][@"position"][@"height"] doubleValue];            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);            CGContextStrokeRect(context, rect);        }                UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        float scale = 4.0f;        scale = MIN(scale, 280.0f/newImage.size.width);        scale = MIN(scale, 257.0f/newImage.size.height);        /*         *    图片等比例 自适应         *///        [_imageView setFrame:CGRectMake(_imageView.frame.origin.x,//                                        _imageView.frame.origin.y,//                                        newImage.size.width * scale,//                                        newImage.size.height * scale)];        [_imageView setImage:newImage];                //面部属性分析        NSDictionary * dic = [NSDictionary dictionaryWithDictionary:[result content]];        [self face:dic];            } else {        // some errors occurred        UIAlertView *alert = [[UIAlertView alloc]                              initWithTitle:[NSString stringWithFormat:@"error message: %@", [result error].message]                              message:@""                              delegate:nil                              cancelButtonTitle:@"OK!"                              otherButtonTitles:nil];        [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];    }    }

调用相机:

- (IBAction)camera:(id)sender {        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {        _pickerImage.delegate = self;        _pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;        [self presentViewController:_pickerImage animated:YES completion:nil];    } else {        UIAlertView *alert = [[UIAlertView alloc]                              initWithTitle:@"没有相机"                              message:@""                              delegate:nil                              cancelButtonTitle:@"OK!"                              otherButtonTitles:nil];        [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];    }        }

调用相册:

- (IBAction)photot:(id)sender {        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {        _pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;                _pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:_pickerImage.sourceType];    }    _pickerImage.delegate = self;    _pickerImage.allowsEditing = NO;    [self presentViewController:_pickerImage animated:YES completion:nil];    }

点击相册中的图片 或者照相机照完后点击use  后触发的方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{    [HUD1 show:YES];    [self.age setText:@"年龄:"];    [self.sex setText:@"性别:"];    [self.fuse setText:@"肤色:"];    [self.weixiao setText:@"微笑程度:"];    self.glass.text = nil;        UIImage *sourceImage = info[UIImagePickerControllerOriginalImage];    UIImage *imageToDisplay = [self fixOrientation:sourceImage];#warning new//    [self performSelectorInBackground:@selector(detectWithImage:) withObject:[imageToDisplay retain]];    [self performSelectorInBackground:@selector(detectWithImage:) withObject:imageToDisplay ];    [_imageView setImage:sourceImage];    [picker dismissViewControllerAnimated:YES completion:nil];        }

点击cancel 调用的方法:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [self.age setText:@"年龄:"];
    [self.sex setText:@"性别:"];
    [self.fuse setText:@"肤色:"];
    [self.weixiao setText:@"微笑程度:"];
    self.glass.text = nil;
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)face:(NSDictionary *)dic{        NSArray * arr = [dic objectForKey:@"face"];        NSDictionary * dic1 = [arr objectAtIndex:0];    NSDictionary * dic2 = [dic1 objectForKey:@"attribute"];        //年龄    NSDictionary * dic3 = [dic2 objectForKey:@"age"];    NSString * ageValue = [dic3 objectForKey:@"value"];    NSString * ageRange = [dic3 objectForKey:@"range"];        //性别    NSDictionary * dic4 = [dic2 objectForKey:@"gender"];    NSString * sex = [dic4 objectForKey:@"value"];        //肤色    NSDictionary * dic5 = [dic2 objectForKey:@"race"];    NSString * renzhong = [dic5 objectForKey:@"value"];        //微笑    NSDictionary * dic6 = [dic2 objectForKey:@"smiling"];    NSString * weixiao = [dic6 objectForKey:@"value"];        //眼镜    NSDictionary * dic7 = [dic2 objectForKey:@"glass"];    NSString * glass = [dic7 objectForKey:@"value"];            [self.age setText:[NSString stringWithFormat:@"年龄:%@(+-%@)",ageValue,ageRange]];        if ([sex isEqualToString:@"Male"]){        [self.sex setText:@"性别:男"];    }else{        [self.sex setText:@"性别:女"];    }    if ([renzhong isEqualToString:@"Asian"]) {        self.fuse.text = @"肤色:黄种人";    }else if ([renzhong isEqualToString:@"White"]){        self.fuse.text = @"肤色:白种人";    }else{        self.fuse.text = @"肤色:黑种人";    }    [self.weixiao setText:[NSString stringWithFormat:@"微笑程度:%@",weixiao]];        if ([glass isEqualToString:@"Normal"]) {        if ([sex isEqualToString:@"Male"]) {            self.glass.text = @"文艺眼镜男";        }else{            self.glass.text = @"文艺眼镜女";        }            }        }

- (IBAction)shuxing:(id)sender {        FaceppResult * result = [[FaceppAPI detection] detectWithURL:nil orImageData:UIImageJPEGRepresentation(_imageView.image, 1) mode:FaceppDetectionModeNormal attribute:FaceppDetectionAttributeAll tag:nil async:NO];    if ([result success]) {        NSDictionary * dic = [NSDictionary dictionaryWithDictionary:[result content]];                NSArray * arr = [dic objectForKey:@"face"];                NSDictionary * dic1 = [arr objectAtIndex:0];        NSDictionary * dic2 = [dic1 objectForKey:@"attribute"];                //年龄        NSDictionary * dic3 = [dic2 objectForKey:@"age"];        NSString * ageValue = [dic3 objectForKey:@"value"];        NSString * ageRange = [dic3 objectForKey:@"range"];                //性别        NSDictionary * dic4 = [dic2 objectForKey:@"gender"];        NSString * sex = [dic4 objectForKey:@"value"];                //肤色        NSDictionary * dic5 = [dic2 objectForKey:@"race"];        NSString * renzhong = [dic5 objectForKey:@"value"];                //微笑        NSDictionary * dic6 = [dic2 objectForKey:@"smiling"];        NSString * weixiao = [dic6 objectForKey:@"value"];                        [self.age setText:[NSString stringWithFormat:@"年龄:%@(+-%@)",ageValue,ageRange]];                if ([sex isEqualToString:@"Male"]){            [self.sex setText:@"性别:男"];        }else{            [self.sex setText:@"性别:女"];        }                if ([renzhong isEqualToString:@"Asian"]) {            self.fuse.text = @"肤色:黄种人";        }else if ([renzhong isEqualToString:@"White"]){            self.fuse.text = @"肤色:白种人";        }else{            self.fuse.text = @"肤色:黑种人";        }        [self.weixiao setText:[NSString stringWithFormat:@"微笑程度:%@",weixiao]];    }    }



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 花盆的土变硬板结怎么办 lol皮肤不想要了怎么办 泉州小黄人自行车被偷怎么办 小学数学没学好上初中怎么办 50天的宝宝缺钙怎么办 5个月的宝宝缺钙怎么办 空调接电后指示灯不亮怎么办 欧普led灯坏了怎么办 led灯条芯片坏了怎么办 太阳能板只有电压没有电流怎么办 农村按空调房屋不保温怎么办 自己一个人想去足疗店不敢去怎么办 吊灯led灯坏了怎么办 办健身卡老板携款跑路了怎么办 武安丰尚健身怎么办卡 热敷后眼睛肿了怎么办 两眼视力差距大怎么办 怀孕体重长得快怎么办 怀孕初期发胖6斤怎么办 孕早期长得太快怎么办 怀孕了肚子眼脏怎么办 孕38周孩子偏小怎么办 孕初期胖的厉害怎么办 怀孕干活累着了怎么办 怀孕了上班很累怎么办 孕妇胖的太快怎么办 孕妇长得太快怎么办 眼睛一按吱吱响怎么办 孕期太胖了怎么办啊 人流后子宫复位不好怎么办 怀孕初期有盆腔积液怎么办 怀孕了有盆腔积液怎么办 多囊怀孕不想要怎么办 6个月婴儿大小眼怎么办 健身教练岁数大了以后怎么办 超变战陀玩具手柄坏了怎么办 飓风战魂三陀螺中轴坏了怎么办 怎么办晚安角和铁陀螺 白衣服染上荧光剂了怎么办 指尖陀螺不亮了怎么办 手指陀螺不转了怎么办