ios OpenCv的配置和人脸识别技术
作为一个好奇心非常重的人,面对未知的世界都想去一探到底。
于是做了个人脸识别的demo。
眼下国内的关于opencv技术文章非常少。都是互相抄袭。关键是抄个一小部分还不全。时间又是非常久之前的了,和如今的一些东西对不上。
没事,我是个实在人,啥也不多说,直接上開始。期间參考了国内很多opencv的文章,代码部分參考http://m.blog.csdn.net/blog/u013810454/27868973。大家能够查看。只是他那个项目下载下来有问题。
我这个融合了全部的长处,更加全面。从配置到使用。
首先我们来配置opencv在xcodeproject。
1.opencv官网下载ios下的框架,先把opencv2.framework下载下来。
然后直接拖到先前创建好的工程中。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
然后
然后
如今主要的配置已经完毕。是时候展现真正的技术了。当然别忘记把.m改成.mm。以便使用c++。
#import "ViewController.h"
#import <Foundation/Foundation.h> int currentvalue = 9; @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
//显示图片
UIImageView *_imageView;
UIImage *image;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createButton];
//创建一个UIImagePickerController对象
UIImagePickerController *ctrl = [[UIImagePickerController alloc] init];
//设置类型
ctrl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//设置代理
ctrl.delegate = self; //显示
[self presentViewController:ctrl animated:YES completion:nil]; self.view.backgroundColor = [UIColor whiteColor]; //创建一个UIImageView,用来显示选择的图片
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 300, 400)];
[self.view addSubview:_imageView]; } #pragma mark - UIImagePickerController代理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//取到选择的图片
image = info[UIImagePickerControllerOriginalImage]; UIImageOrientation imageOrientation=image.imageOrientation;
if(imageOrientation!=UIImageOrientationUp)
{
// 原始图片能够依据照相时的角度来显示,但UIImage无法判定,于是出现获取的图片会向左转90度的现象。
// 下面为调整图片角度的部分
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 调整图片角度完成
}
//处理图片
_imageView.image = image; [picker dismissViewControllerAnimated:YES completion:nil]; } -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
} //把image转化成opencv的图片格式
- (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {
CGImageRef imageRef = image.CGImage; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
IplImage *iplimage = cvCreateImage(cvSize(image.size.width, image.size.height), IPL_DEPTH_8U, 4);
CGContextRef contextRef = CGBitmapContextCreate(iplimage->imageData, iplimage->width, iplimage->height,
iplimage->depth, iplimage->widthStep,
colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace); IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
cvCvtColor(iplimage, ret, CV_RGBA2BGR);
cvReleaseImage(&iplimage); return ret;
}
- (void) opencvFaceDetect { UIImage* img = [image copy];
if(img) { cvSetErrMode(CV_ErrModeParent);
IplImage *image = [self CreateIplImageFromUIImage:img]; IplImage *grayImg = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1); //先转为灰度图
cvCvtColor(image, grayImg, CV_BGR2GRAY); //将输入图像缩小4倍以加快处理速度
int scale = 4;
IplImage *small_image = cvCreateImage(cvSize(image->width/scale,image->height/scale), IPL_DEPTH_8U, 1);
cvResize(grayImg, small_image); //载入分类器
NSString *path = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_alt2" ofType:@"xml"];
CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad([path cStringUsingEncoding:NSASCIIStringEncoding], NULL, NULL, NULL);
CvMemStorage* storage = cvCreateMemStorage(0);
cvClearMemStorage(storage); //关键部分。使用cvHaarDetectObjects进行检測。得到一系列方框
CvSeq* faces = cvHaarDetectObjects(small_image, cascade, storage ,1.1, currentvalue, CV_HAAR_DO_CANNY_PRUNING, cvSize(0,0), cvSize(0, 0)); NSLog(@"faces:%d",faces->total); //创建画布将人脸部分标记出
CGImageRef imageRef = img.CGImage;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef contextRef = CGBitmapContextCreate(NULL, img.size.width, img.size.height,8, img.size.width * 4,colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault); CGContextDrawImage(contextRef, CGRectMake(0, 0, img.size.width, img.size.height), imageRef); CGContextSetLineWidth(contextRef, 4);
CGContextSetRGBStrokeColor(contextRef, 1.0, 0.0, 0.0, 1); //对人脸进行标记
for(int i = 0; i < faces->total; i++) {
// Calc the rect of faces
CvRect cvrect = *(CvRect*)cvGetSeqElem(faces, i);
CGRect face_rect = CGContextConvertRectToDeviceSpace(contextRef, CGRectMake(cvrect.x*scale, cvrect.y*scale , cvrect.width*scale, cvrect.height*scale)); CGContextStrokeRect(contextRef, face_rect); } _imageView.image = [UIImage imageWithCGImage:CGBitmapContextCreateImage(contextRef)]; } } //检測略耗时,开一个新线程来处理吧
-(void)btn
{
[NSThread detachNewThreadSelector:@selector(opencvFaceDetect) toTarget:self withObject:nil];
} -(void)createButton
{ UIButton *btn = [[UIButton alloc]init];
btn.backgroundColor = [UIColor redColor];
btn.frame = CGRectMake(0, 100, 30, 30);
[btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; }
@end
ok,如今能够检測人脸了。
是不是非常奇妙。非常好玩?赶紧动手试一试吧。
ios OpenCv的配置和人脸识别技术的更多相关文章
- 人脸识别技术大总结(1):Face Detection & Alignment
http://blog.jobbole.com/85783/ 首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他技术 - 导航条 - 首页 最新文章 IT 职场 前端 - Ja ...
- 3D动态人脸识别技术分析——世纪晟人脸识别实现三维人脸建模
- 目录 - 国内3D动态人脸识别现状概况 - 新形势下人脸识别技术发展潜力 - 基于深度学习的3D动态人脸识别技术分析 1. 非线性数据建模方法 2. 基于3D变形模型的人脸建模 - 案例结合——世 ...
- face ++ 人脸识别技术初步
网站地址: https://console.faceplusplus.com.cn/documents/5671791主要有 1 人脸识别技术 2 人体识别技术 ...
- 旷视科技 -- Face++ 世界最大的人脸识别技术平台
旷视科技 -- Face++ 世界最大的人脸识别技术平台: https://www.megvii.com/
- 基于 HTML5 的人脸识别技术
基于 HTML5 的人脸识别技术 https://github.com/auduno/headtrackr/
- PHP实现人脸识别技术
这次人脸识别技术,是实现在微信端的,也就是说利用公众微信平台,调用第三的API来实现人脸识别这项技术的. 实现的思路: 首先呢,将收集的照片,建立一个照片库,然后利用在微信平台发送的照片,去到照片库进 ...
- OpenCV学习 物体检测 人脸识别 填充颜色
介绍 OpenCV是开源计算机视觉和机器学习库.包含成千上万优化过的算法.项目地址:http://opencv.org/about.html.官方文档:http://docs.opencv.org/m ...
- python与opencv的结合之人脸识别值
首先还是要感谢http://www.jb51.net/article/67392.htm这位大神的无私奉献,开源的代码,让我省去了很多事,但是就光系统环境的配置就花去了我将近一个星期的时间,真是不容易 ...
- 人脸识别技术大总结1——Face Detection & Alignment
搞了一年人脸识别,寻思着记录点什么,于是想写这么个系列,介绍人脸识别的四大块:Face detection, alignment, verification and identification(re ...
随机推荐
- hihoCoder-1829 2018亚洲区预选赛北京赛站网络赛 B.Tomb Raider 暴力 字符串
题面 题意:给你n个串,每个串都可以选择它的一个长度为n的环形子串(比如abcdf的就有abcdf,bcdfa,cdfab,dfabc,fabcd),求这个n个串的这些子串的最长公共子序列(每个串按顺 ...
- html页面中苹果手机遇到数字换行、样式变形
在做项目中遇到过几回苹果手机读取html页面时,如果出现一串数字,html页面会折行.变形,最后发现是因为苹果手机的打电话功能,如果html上有数字的话,苹果手机会以为是电话号码,就会改变其样式只需要 ...
- 本地sql文件导入mysql数据库
mysql中配置my.ini interactive_timeout = 120 wait_timeout = 120 max_allowed_packet = 32M 导入sql运行命令 sourc ...
- 微信小程序video组件出现无法播放或卡顿
微信小程序使用video组件播放视频的时候,会出现卡顿或者无法播放的问题,加一个custom-cache=”true“即可解决,这个属性文档上没有,是从小程序开发社区中get到的.
- RocketMQ之基本信息
1.Producer 即消息生产者,负责产生消息,一般由业务系统负责产生消息. 2.Consumer 即消息消费者,负责消费消息,一般是后台系统负责异步消费. 3.Push Consumer Cons ...
- 2018最新WordPress缩略图设置方法
缩略图设置的方法很多,但都不全面,且很多教程已经失效了,其中使用插件来实现,可是那些插件都使用过都不能实现效果,所以我整理了一份使用代码实现缩略图的方法. 1.找到网站根目录/wp-content/t ...
- 谷歌C++编程为何禁止缺省参数
C++的缺省参数尽量不要使用,结果可能出乎我们的意料,下面的程序大家看看输出结果是多少? ) cout << num << endl; ...
- 编写jQuery 插件
编写jQuery Plugin,要设置默认值,并允许用户修改默认值,或者运行是传入其他值. 最终,我们得出编写一个jQuery插件的原则: 给$.fn绑定函数,实现插件的代码逻辑: 插件函数最后要 r ...
- Linux终端 Tab 补全命令
1. vi编辑器打开 /etc/bash.bashrc文件 vi /etc/bash.bashrc 2.找到文件中的下列代码 3.将注释符号#去掉,即改成 4.最后 source一下 /etc/bas ...
- 前端swiper使用指南
swiper 在网页中常用的方法 1.使用时在页面引入 <link rel="stylesheet" href="front/css/swiper.min.css& ...