iOS人臉年齡|性別檢測
阿新 • • 發佈:2019-02-19
*轉載請標明來源,文章相關詳細程式碼。
本文主要介紹iOS使用習悅人臉服務實現簡單的人臉年齡和性別檢測。*
1. 建立應用,獲取API Key和API Secret
首先進入習悅開發者平臺,註冊或登入成功後,點選建立應用,填寫必要內容後建立應用,如圖:
建立應用後,在我的應用模組,選擇對應應用點選管理應用,如圖:
複製API Key和API Secret以備後用,如圖:
2. 下載人臉識別SDK
將解壓後的人臉識別SDK拖拽到專案中,如圖勾選:
使用之前建立的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];
}];
}
檢測效果如下圖:
-