人脸识别

作者: 帅哥_刷哥 | 来源:发表于2016-04-06 10:38 被阅读175次

1.介绍

识别图片上的脸部.
使用face++来做人脸识别(百度搜索face++即可)。
face++的文档很详细,所以根据face++的案例去做就ok了。

2.使用

1.到face++中注册账号,并登陆
2.在我的应用中创建一个新的应用
3.下载SDK
4.引入项目-使用ARC的即可.
5.在AppDelegate中的application:didFinishLaunchingWithOptions:进行注册.
6.在AppDelegate中的application:didFinishLaunchingWithOptions:打开调试模式
7.获得图片
8.调用face++API,解析图片,查看是否有人脸
9.对脸部位置进行显示

3.注册代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //  注册AppKey
    [FaceppAPI initWithApiKey:@"5ec524794e29cb7c7f4d1ba38f914e82" andApiSecret:@"1m0JIbSW8vwGUn79d7EMHtO2_ZwSecaP"
                    andRegion:APIServerRegionCN];
    //  打开调试模式,如果是调试模式,会打印一些日志信息,如果上线要关闭它.
    [FaceppAPI setDebugMode:YES];
    return YES;
}

4.识别人脸代码

- (void) detactImage:(UIImage *) image
{
    //  修正图片的方向
    image = [self fixOrientation:image];

    //  压缩质量 0 - 1 值
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
    //  调用人脸识别Api,获取人脸识别信息
    FaceppResult *faceRs = [[FaceppAPI detection] detectWithURL:nil orImageData:imageData];
    
    if (!faceRs.success) {
        NSLog(@"人脸识别失败:%@",faceRs.error);
        return;
    }
    
    //  获取识别到人脸
    NSArray *faces = faceRs.content[@"face"];
    if (faces.count == 0) {
        NSLog(@"没有脸");
        return;
    }
    //  遍历人脸数据
    for (NSDictionary *face in faces) {
        
        NSNumber *age = face[@"attribute"][@"age"][@"value"];
        NSNumber *gender = face[@"attribute"][@"gender"][@"value"];
        NSNumber *race = face[@"attribute"][@"race"][@"value"];
        NSNumber *smile = face[@"attribute"][@"smiling"][@"value"];
        self.title = [NSString stringWithFormat:@"%@,%@,%@,%@",age,gender,race,smile];
        
        //      获取脸的中心点
        CGFloat centerX = [face[@"position"][@"center"][@"x"] doubleValue];
        CGFloat centerY = [face[@"position"][@"center"][@"y"] doubleValue];
        
        //      脸的宽和高
        CGFloat width = [face[@"position"][@"width"] doubleValue];
        CGFloat height = [face[@"position"][@"height"] doubleValue];
        
        //      计算左上角的位置
        CGFloat faceX = centerX - width * 0.5;
        CGFloat faceY = centerY - height * 0.5;
        
        //      上面数据都是百分比数据,需要还原为真实的大小
        CGFloat scaleX = image.size.width / 100;
        CGFloat scaleY = image.size.height / 100;
        
        faceX *= scaleX;
        width *= scaleX;
        faceY *= scaleY;
        height *= scaleY;
        
        //      1. 开启图片上下文
        UIGraphicsBeginImageContext(image.size);
        //      2. 把图片回到上面
        [image drawAsPatternInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
        //      3. 绘制矩形框
        //      获取当前图形的上下文
        CGContextRef context = UIGraphicsGetCurrentContext();
        //      设置颜色
        [[UIColor greenColor] set];
        
        //      绘制矩形
        CGContextAddRect(context, CGRectMake(faceX, faceY, width, height));
        //      渲染(空心)
        CGContextStrokePath(context);
        //      从上下文中获取图片
        image = UIGraphicsGetImageFromCurrentImageContext();
        
        //      关闭图形上下文
        UIGraphicsEndImageContext();
    }
    
    self.imageView.image = image;
}

// 旋转图片的方向
- (UIImage *)fixOrientation:(UIImage *)aImage {
    
    if (aImage.imageOrientation == UIImageOrientationUp)
        return aImage;
    
    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;
    }
    
    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;
    }
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

相关文章

  • 人脸识别技术 通识

    人脸识别技术 通识 1 人脸识别概要 2 人脸识别工作原理 3 人脸识别技术分类 4 人脸识别技术优缺点 5 人脸...

  • 都在说人脸识别有Bug,是真的吗?

    “人脸识别破解”“反人脸识别技术”“人脸识别漏洞”“人脸识别失误”“尽量不要用人脸识别”….网上N多这样的信息铺天...

  • 2020-06-09

    云卡通人脸识别系统含盖:动态人脸识别门禁管理系统、动态人脸识别消费系统、动态人脸识别考勤系统。其中动态人脸识别消费...

  • 人脸识别闸机安装知识

    随着人脸识别技术的成熟,人脸识别闸机越来越受欢迎,在选型上也丰富多样。人脸识别闸机包含人脸识别摆闸、人脸识别翼闸、...

  • 2020-07-28

    一、人脸识别系统具有广泛的应用:包括人脸识别出入管理系统、人脸识别门禁考勤系统、人面识别消费管理系统、人脸识别通道...

  • 人脸识别 -- 活体检测(张嘴摇头识别)

    一:简介 最近项目在做了身份证银行卡识别之后,开始实现人脸识别和活体识别,其中人脸识别包括人脸入库、人脸查找、人脸...

  • facexx解析:人脸识别技术市场在哪些领域?

    人脸识别技术经历了可见光图像人脸识别、三维图像人脸识别/热成像人脸识别、基于主动近红外图像的多光源人脸识别三层进化...

  • Android实现人脸识别(人脸检测)初识

    title: Android实现人脸识别(人脸检测)初识categories: Androidtags: 人脸识别...

  • h

    # 多人脸识别系统 [^]: by CathyZhang ## 背景 > 多人脸识别,又称为M:N人脸识别,主要用...

  • 2020-05-14

    云卡通人脸识别消费系统解决方案 云卡通人脸识别系统含盖:动态人脸识别门禁管理系统、动态人脸识别消费系统、动态人脸识...

网友评论

  • 酷走天涯:这个框架真的可以,我之前也用它写了一个叫做《看谁最年轻》的应用,准确度特别高
  • 叶舞清风:这个能不能对一般的图片进行处理呢?
    叶舞清风:@帅哥_刷哥 嗯,谢谢
    帅哥_刷哥:@叶舞清风可以

本文标题:人脸识别

本文链接:https://www.haomeiwen.com/subject/jgtmlttx.html