- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = [UIImage imageNamed:@"timg.jpeg"];
[self.view addSubview:imageView]; CGFloat width = imageView.image.size.width;
CGFloat height = imageView.image.size.height; CGFloat sWidth = imageView.bounds.size.width;
CGFloat sHeight = imageView.bounds.size.height; CGFloat x = width/sWidth;
CGFloat y = height/sHeight; // 根据image修改imageview的frame
if (x > y)
{
imageView.frame = CGRectMake(, (sHeight-height/x)/, sWidth, height/x);
}else{
imageView.frame = CGRectMake((sWidth-width/y)/, , width/y, sHeight);
} // 压缩image
UIGraphicsBeginImageContext(CGSizeMake(imageView.bounds.size.width , imageView.bounds.size.height));
[imageView.image drawInRect:CGRectMake(, , imageView.bounds.size.width, imageView.bounds.size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); imageView.image = scaledImage; //识别
dispatch_async(dispatch_get_global_queue(, ), ^{
CIImage *cImage = [CIImage imageWithCGImage:imageView.image.CGImage];
// 设置识别模式
NSDictionary *opts = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh
forKey:CIDetectorAccuracy];
/* Lower accuracy, higher performance */
//CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyLow NS_AVAILABLE(10_7, 5_0); /* Lower performance, higher accuracy */
//CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyHigh NS_AVAILABLE(10_7, 5_0); CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:opts]; NSArray *features = [detector featuresInImage:cImage]; if ([features count] == )
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"检测失败");
});
return ;
} for (CIFaceFeature *feature in features)
{
// 是否微笑
BOOL smile = feature.hasSmile; NSLog(smile ? @"微笑" : @"没微笑"); // 眼睛是否睁开
BOOL leftEyeClosed = feature.leftEyeClosed;
BOOL rightEyeClosed = feature.rightEyeClosed; NSLog(leftEyeClosed ? @"左眼没睁开" : @"左眼睁开");
NSLog(rightEyeClosed ? @"右眼没睁开" : @"右眼睁开"); // 获取脸部frame
CGRect rect = feature.bounds;
rect.origin.y = imageView.bounds.size.height - rect.size.height - rect.origin.y;// Y轴旋转180度
faceRect = rect;
NSLog(@"脸 %@",NSStringFromCGRect(rect)); // 左眼
if (feature.hasLeftEyePosition)
{
CGPoint eye = feature.leftEyePosition;
eye.y = imageView.bounds.size.height - eye.y;// Y轴旋转180度
NSLog(@"左眼 %@",NSStringFromCGPoint(eye));
} // 右眼
if (feature.hasRightEyePosition)
{
CGPoint eye = feature.rightEyePosition;
eye.y = imageView.bounds.size.height - eye.y;// Y轴旋转180度
NSLog(@"右眼 %@",NSStringFromCGPoint(eye));
} // 嘴
if (feature.hasMouthPosition)
{
CGPoint mouth = feature.mouthPosition;
mouth.y = imageView.bounds.size.height - mouth.y;// Y轴旋转180度
NSLog(@"嘴 %@",NSStringFromCGPoint(mouth));
}
} dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"检测完成");
UIView *view = [[UIView alloc] initWithFrame:faceRect];
view.backgroundColor = [UIColor blueColor];
view.alpha = 0.3;
[imageView addSubview:view];
});
});
}

关于图片压缩:imageview的大小与image的大小不一致,识别的时候,是按照image的大小进行计算,通过重绘,使imageview与image保持一致。

关于坐标Y轴翻转:屏幕的坐标原点即(0,0)点在左上角,识别图片的坐标原点在左下角。

运行结果

控制台输出结果

最新文章

  1. python开发 Day1 python基础
  2. Maximum length of a table name in MySQL
  3. 清除浮动clear/BFC
  4. 1069 Nim游戏
  5. 集合类学习之Hashmap机制研究
  6. wpf 只在window是ShowDialog打开时才设置DialogResult
  7. MYSQL大小写(由于数据由windows迁移到Linux导致)
  8. 职业定位(Web前端、后台、PC端)
  9. async/await与promise(nodejs中的异步操作问题)
  10. MySQL中的字符串函数
  11. 我是如何将博客转成PDF的
  12. [Swift]LeetCode871. 最低加油次数 | Minimum Number of Refueling Stops
  13. 软件工程(FZU2015) 增补作业
  14. Apache kylin进阶——元数据篇
  15. POJ 3415 Common Substrings(长度不小于K的公共子串的个数+后缀数组+height数组分组思想+单调栈)
  16. day12-python的类
  17. mAP的计算
  18. C# ABP 配置连接数据库&创建表
  19. 前端笔记----jquery入门知识点总结 (转)
  20. CentOS7.0 x86_64系统上构建php开发环境--Lamp(包含设置虚拟文件夹,加入SELinux对httpd的支持等知识)

热门文章

  1. JavaScript的进阶之路(四)理解对象1
  2. UIResponder NSSet UITouch UIEvent
  3. shell学习笔记--持续记录
  4. selenium元素定位方法
  5. spring+quarts常见问题
  6. c# 控制台定时程序
  7. asp.net生成PDF文件(一)
  8. 四、python小功能记录——按键转点击事件
  9. 二、python小功能记录——监听鼠标事件
  10. MySQL 数据库 -- 数据操作