iOS人脸年龄|性别检测

原文地址:http://www.jianshu.com/p/4f2a14f9caac

*转载请标明来源,文章相关详细代码
本文主要介绍iOS使用习悦人脸服务实现简单的人脸年龄和性别检测。*

1. 创建应用,获取API Key和API Secret

  • 首先进入习悦开发者平台,注册或登录成功后,点击创建应用,填写必要内容后创建应用,如图:
    创建新应用.png

    • 创建应用后,在我的应用模块,选择对应应用点击管理应用,如图:
      应用管理.png

    • 复制API Key和API Secret以备后用,如图:
      查看API_Key和API_Secret.png

2. 下载人脸识别SDK

  • 将解压后的人脸识别SDK拖拽到项目中,如图勾选:
    添加人脸识别SDK.png

  • 使用之前创建的API Key和API Secret进行认证,在application: didFinishLaunchingWithOptions:入口方法中添加以下代码:

[[ZHTJBaseService shared] setApiKey:@"2208eda555994ea8a20cdcd5d9cc3670" apiSecret:@"aaed815544547725d6bd9a6690ff13b76d20f020"];
//认证成功后需要绑定用户,userID为自定义(1-32位字母/数字)
  [ZHTJFaceUserManagertj_creatUserWithUserID:@"UserID" completionHandler:^(NSString *userID, ZHTJFaceError *error) {}];
  • 选择图片进行年龄和性别检测,代码如下:
-(IBAction)clickOneFaceDetection:(id)sender
{
    //清除掉覆盖层
    [self removeCoverLayer];

    [ZHTJDetectManagertj_DetectImage:self.mainImgV.imageisAge:_ageSwitch.onisGender:_genderSwitch.oncompletionHandler:^(NSArray<ZHTJFaceFeaturesModel *> *faceFeatureMDAry, ZHTJFaceError *error)
     {
dispatch_async(dispatch_get_main_queue(), ^{

             if (error.code) {
NSLog(@"失败:%@",error.msg);
}else{

NSLog(@"faceNum==%d",(int)faceFeatureMDAry.count);

NSArray * scaleAry = [self getScaleAndCcaleImgOriginWithImgView:self.mainImgV];
                 double scale = [scaleAry.firstObjectdoubleValue];
CGPointscaleImgOrigin = [scaleAry.lastObjectCGPointValue];

                 [faceFeatureMDAryenumerateObjectsUsingBlock:^(ZHTJFaceFeaturesModel * _Nonnullobj, NSUIntegeridx, BOOL * _Nonnull stop) {
ZHTJFaceFeaturesModel * model = (ZHTJFaceFeaturesModel*)obj;
NSLog(@"%@-%@-%.3f-%@",model.faceID,NSStringFromCGRect(model.faceRect),model.faceAge,model.faceGender);

NSMutableString * textMuStr = [[NSMutableStringalloc]init];
                     if (_ageSwitch.on) {
                         [textMuStrappendString:[NSStringstringWithFormat:@"age:%.f\n",model.faceAge]];
                     }
                     if (_genderSwitch.on) {
                         [textMuStrappendString:[NSStringstringWithFormat:@"女:男=%.f:%.f",[model.faceGender.firstObjectdoubleValue]*100,[model.faceGender.lastObjectdoubleValue]*100]];
                     }

                     //绘制人脸框及年龄和性别信息
                     [self drawTextLayerWithFaceRect:model.faceRecttext:textMuStrscale:scalescaleImgOrigin:scaleImgOriginonImgView:self.mainImgV];
                 }];
             }

         });

     }];

}
#pragma mark    获得图片在imgV上的缩放比和img相对于imgV的左上角位置
- (NSArray*)getScaleAndCcaleImgOriginWithImgView:(UIImageView *)imgV
{
    if (imgV.contentMode == UIViewContentModeScaleAspectFit) {
        //1.当imgV.contentMode = UIViewContentModeScaleAspectFit时,计算缩放后的imgSize和img在imgV的左上角位置
        //计算缩放比例
CGFloatimgV_W  =imgV.bounds.size.width;
CGFloatimgV_H  =imgV.bounds.size.height;
CGFloatimg_W   =   imgV.image.size.width;
CGFloatimg_H   =   imgV.image.size.height;
        double scale   =   MIN(imgV_H / img_H, imgV_W / img_W);
CGSizescaleImgSize = CGSizeMake(img_W*scale, img_H*scale);//缩放后的imgSize
CGPointscaleImgOrigin = CGPointMake((imgV_W-scaleImgSize.width)/2.f, (imgV_H-scaleImgSize.height)/2.f);//缩放后的img在imgV的左上角位置
        return @[@(scale),[NSValuevalueWithCGPoint:scaleImgOrigin]];
    }
    return nil;
}

- (void)drawTextLayerWithFaceRect:(CGRect)faceRect text:(NSString *)text scale:(double)scale scaleImgOrigin:(CGPoint)scaleImgOriginonImgView:(UIImageView *)imgV
{
    //2,根据缩放后的imgSize得到人脸相对于img的位置
CGFloatfea_W = faceRect.size.width * scale;
CGFloatfea_H = faceRect.size.height * scale;
    //3,根据缩放后的img在imgV的左上角位置得到人脸相对于imgV的位置
CGFloatfea_X = faceRect.origin.x * scale + scaleImgOrigin.x;
CGFloatfea_Y = faceRect.origin.y * scale + scaleImgOrigin.y;

CGRectNowFaceRect = CGRectMake(fea_X, fea_Y, fea_W, fea_H); //得到人脸在imgV的位置

CATextLayer * textLay = [CATextLayer layer];

textLay.string  =   text;
textLay.alignmentMode = @"center";//文字对齐方式

    //字体 (仅限于非富文本才能用)
UIFont *fontTemp = [UIFontfontWithName:@"Heiti SC" size:12];
CFStringReffontName = (__bridge CFStringRef)fontTemp.fontName;
textLay.font = CGFontCreateWithFontName(fontName);
textLay.fontSize = fontTemp.pointSize;

textLay.bounds = CGRectMake(0, 0, NowFaceRect.size.width, NowFaceRect.size.height);//文本大小,默认是0

    //给图层加边框
textLay.borderColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1].CGColor;
textLay.borderWidth = 1;

    //字体颜色(仅限于非富文本才能用)
textLay.foregroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1].CGColor;
textLay.wrapped = YES;//是否折行,默认是no
textLay.contentsScale = [UIScreenmainScreen].scale;//清晰度
textLay.position    =   CGPointMake(NowFaceRect.size.width/2+NowFaceRect.origin.x, NowFaceRect.size.height/2+NowFaceRect.origin.y);

    [imgV.layeraddSublayer:textLay];

textLay =   nil;
}

#pragma mark    清除覆盖层
- (void)removeCoverLayer
{
    //清除掉覆盖层
NSMutableArray * muAry = [NSMutableArrayarrayWithArray:_mainImgV.layer.sublayers];
    [muAryenumerateObjectsUsingBlock:^(CALayer * _Nonnullobj, NSUIntegeridx, BOOL * _Nonnull stop)
     {
         [objremoveFromSuperlayer];
     }];
}

检测效果如下图:
- 检测效果

*转载请标明来源,文章相关详细代码
本文主要介绍iOS使用习悦人脸服务实现简单的人脸年龄和性别检测。*

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值