原文地址:http://www.jianshu.com/p/4f2a14f9caac
*轉(zhuǎn)載請(qǐng)標(biāo)明來(lái)源,文章相關(guān)詳細(xì)代碼。 本文主要介紹iOS使用習(xí)悅?cè)四樂(lè)?wù)實(shí)現(xiàn)簡(jiǎn)單的人臉年齡和性別檢測(cè)。*
1. 創(chuàng)建應(yīng)用,獲取API Key和API Secret
首先進(jìn)入習(xí)悅開(kāi)發(fā)者平臺(tái),注冊(cè)或登錄成功后,點(diǎn)擊創(chuàng)建應(yīng)用,填寫(xiě)必要內(nèi)容后創(chuàng)建應(yīng)用,如圖: 
創(chuàng)建應(yīng)用后,在我的應(yīng)用模塊,選擇對(duì)應(yīng)應(yīng)用點(diǎn)擊管理應(yīng)用,如圖: 
復(fù)制API Key和API Secret以備后用,如圖: 
2. 下載人臉識(shí)別SDK
將解壓后的人臉識(shí)別SDK拖拽到項(xiàng)目中,如圖勾選: 
使用之前創(chuàng)建的API Key和API Secret進(jìn)行認(rèn)證,在application: didFinishLaunchingWithOptions:入口方法中添加以下代碼:
[[ZHTJBaseService shared] setApiKey:@"2208eda555994ea8a20cdcd5d9cc3670" apiSecret:@"aaed815544547725d6bd9a6690ff13b76d20f020"];//認(rèn)證成功后需要綁定用戶,userID為自定義(1-32位字母/數(shù)字)  [ZHTJFaceUserManagertj_creatUserWithUserID:@"UserID" completionHandler:^(NSString *userID, ZHTJFaceError *error) {}];選擇圖片進(jìn)行年齡和性別檢測(cè),代碼如下:-(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相對(duì)于imgV的左上角位置- (NSArray*)getScaleAndCcaleImgOriginWithImgView:(UIImageView *)imgV{    if (imgV.contentMode == UIViewContentModeScaleaspectFit) {        //1.當(dāng)imgV.contentMode = UIViewContentModeScaleAspectFit時(shí),計(jì)算縮放后的imgSize和img在imgV的左上角位置        //計(jì)算縮放比例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);//縮放后的imgSizeCGPointscaleImgOrigin = 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,根據(jù)縮放后的imgSize得到人臉相對(duì)于img的位置CGFloatfea_W = faceRect.size.width * scale;CGFloatfea_H = faceRect.size.height * scale;    //3,根據(jù)縮放后的img在imgV的左上角位置得到人臉相對(duì)于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";//文字對(duì)齊方式    //字體 (僅限于非富文本才能用)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);//文本大小,默認(rèn)是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;//是否折行,默認(rèn)是notextLay.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];     }];}檢測(cè)效果如下圖: - 
*轉(zhuǎn)載請(qǐng)標(biāo)明來(lái)源,文章相關(guān)詳細(xì)代碼。 本文主要介紹iOS使用習(xí)悅?cè)四樂(lè)?wù)實(shí)現(xiàn)簡(jiǎn)單的人臉年齡和性別檢測(cè)。*