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;
}
网友评论