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使用習悅人臉服務實現簡單的人臉年齡和性別檢測。*

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章