人脸识别应用于许多领域。二维码的识别更是疯狂。下面,我们一起去看看简单的人脸识别和二维码识别。
1、测试数据的展示(人脸)。
原图:
1、人脸的大小
// 人脸大小
CGRect FaceRect = FaceFeature.bounds;
NSLog(@"H:%f W:%f",FaceRect.size.height,FaceRect.size.width);
// 人脸的位置
NSLog(@"X:%f Y:%f",FaceRect.origin.x,FaceRect.origin.y);
/*
* 输出信息的校验
* 2016-07-09 17:55:05.210 CIDetector_zsj[1849:136896] H:212.000000 W:212.000000
* 2016-07-09 17:55:05.211 CIDetector_zsj[1849:136896] X:203.000000 Y:419.000000
* 解说:
* 原始图片的大小是 600 * 854 px.
* 图片上人脸的位置 X: 210 px Y: 420 px (对于位置的展示,请理解画布的原点在哪里为准?)
* 图片上人脸的大小(正方形算) 213 * 213 px
* 输出人脸信息相比精度还是很高
*/
// *************
// 人的眼睛 (左右)
// 判断人的眼睛在图上是否显示
EyesLeft IsSave= [FaceFeaturehasLeftEyePosition];
if (IsSave) {
// 人左眼的位置
CGPoint EyesLeftCGPoint = FaceFeature.leftEyePosition;
NSLog(@"EyeLeft_X:%f EyesLeft_Y:%f",EyesLeftCGPoint.x,EyesLeftCGPoint.y);
/*
* 输出数据: 2016-07-09 18:42:20.295 CIDetector_zsj[2156:155810]
EyeLeft_X:284.000000 EyesLeft_Y:584.000000
*
* 实际图片上的左眼位置:
EyeLeft_X:284.000000 EyesLeft_Y:587.000000
*
* 两组数据进行对比,几乎一样,精度很高。
*/
// 左眼是否是关闭的
EyesLeft CloseEyesLeft = FaceFeature.leftEyeClosed;
if (CloseEyesLeft) {
NSLog(@"左眼是闭上的");
}else{
NSLog(@"左眼是睁开的");
}
/*
* 输出信息:
2016-07-09 18:48:40.408 CIDetector_zsj[2241:159731]左眼是睁开的 (符合实际)
*/
}
// *******************
// 人的右眼
EyesRight IsSaveR = FaceFeature.hasRightEyePosition;
if (IsSaveR) {
// 获取人眼右眼的位置
CGPoint EyesRightCGPoint = FaceFeature.rightEyePosition;
NSLog(@"EyesRight_X:%f EyesRight_Y:%f",EyesRightCGPoint.x,EyesRightCGPoint.y);
/*
* 输出右眼位置信息:
2016-07-09 19:07:45.125 CIDetector_zsj[2335:164771]
EyesRight_X:368.000000 EyesRight_Y:565.000000
*
* 实际的右眼的位置:
EyesRight_X:367.000000 EyesRight_Y:565.000000
*
* 数据比较接近,精度很高
*/
// 检测人都眼睛是否是闭上的
EyesRight CloseEyesRight = FaceFeature.rightEyeClosed;
if (CloseEyesRight) {
NSLog(@"右眼是闭上的");
}else{
NSLog(@"右眼是睁开的");
}
/*
* 输出信息:
2016-07-09 19:12:38.295 CIDetector_zsj[2387:167289]右眼是睁开的 (符合实际)
*/
}
// ***********************
// 人的嘴
// 判断人的嘴是否在图片上
Mouth MouthIsSave = FaceFeature.hasMouthPosition;
if (MouthIsSave) {
CGPoint MouthCgPoint = FaceFeature.mouthPosition;
NSLog(@"Mouth_X:%f Mouth_Y:%f",MouthCgPoint.x,MouthCgPoint.y);
/**
* 输出信息:
2016-07-09 19:19:22.408 CIDetector_zsj[2474:171661]
Mouth_X:298.000000 Mouth_Y:474.000000
*
* 实际嘴的位置:
Mouth_X:301.000000 Mouth_Y:474.000000
*
* 数据分析的结果两组数据几乎一样,精度很高
*/
// 判断人脸是否微笑
Mouth IsSmile = FaceFeature.hasSmile;
if (IsSmile) {
NSLog(@"人在微笑");
}else{
NSLog(@"人不在微笑");
}
/*
* 输出信息:
2016-07-09 19:27:22.460 CIDetector_zsj[2561:175632]人不在微笑 (符合实际)
*/
}
// ****************
// 获取人脸的角度
BOOL FaceAngle = FaceFeature.hasFaceAngle;
if (FaceAngle) {
float Angle = FaceFeature.faceAngle;
NSLog(@"FaceAngle:%f",Angle);
/*
* 输出的信息
* 2016-07-09 19:32:42.485 CIDetector_zsj[2638:178512] FaceAngle:13.000000
*
* 解说:以中为 0度 ,向左为正(0~90)度 ,向右为(0~90)度
*/
}
// 获取追踪对象
BOOL Istracking = FaceFeature.hasTrackingID;
if (Istracking) {
// 获取追踪的对象的ID
int TrackingID = FaceFeature.trackingID;
NSLog(@"Tracking:%d",TrackingID);
}
}
2、二维码识别。
// 获取图像的大小
CGRect QRCodeRect = QRCodeFeature.bounds;
NSLog(@"W:%f H:%f",QRCodeRect.size.width,QRCodeRect.size.height);
// 二维码的内容
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
UIAlertController * QRCodeAlert = [UIAlertControlleralertControllerWithTitle:@"二维码识别"message:QRCodeFeature.messageStringpreferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * Action = [UIAlertActionactionWithTitle:@"OK"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {
}];
[QRCodeAlert addAction:Action];
[selfpresentViewController:QRCodeAlertanimated:YEScompletion:nil];
});
3、完整代码。
//
// ViewController.m
// CIDetector_zsj
//
// Created by 周双建 on 16/7/9.
// Copyright © 2016年周双建. All rights reserved.
//
#import "ViewController.h"
// 引入探测启的头文件
#import <CoreImage/CIDetector.h>
typedef NSDictionary Performance ;
typedef NSArray DetectionDataArray;
typedef BOOL EyesLeft;
typedef BOOL EyesRight;
typedef BOOL Mouth;
typedef NSArray QRCodeArray;
typedef NSArray ContentArray;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// 创建探测器的对象唯一的方法
/**
*
* @type 有四种类型如下:
* 范围系统限制: MAC OSX 10_7以上 ; IOS SDK_5.0以上
*
* CIDetectorTypeFace 是用于人脸识别的类型
*
* CIDetectorTypeRectangle 是用于识别大小的类型
*
* CIDetectorTypeQRCode 是用于条形码&二维码识别的类型
*
*@ context 是用于对图像进行操作的
*
*@ options 是一个字典 用于设置探测器的性能
*/
/**
* CIDetectorAccuracy(精度) && CIDetectorTracking(轨迹)对与探测器的精度有两个字 key
*
* CIDetectorAccuracyHigh(高) && CIDetectorAccuracyLow(低) Value
*
*/
/**********∑∑人脸识别*****************************/
// 我们准备要识别的人脸(美女一个)
// 正要说明:经过测试,侧脸检测不到。
// 创建人脸对象(有好多中创建方法,可根据自己需求选择)
CIImage *FaceCImage = [CIImageimageWithData:UIImagePNGRepresentation([UIImageimageNamed:@"right1.jpg"])];
// 设置探测器的性能
Performance * FaceDetectorDict =@{@"CIDetectorAccuracy":@"CIDetectorAccuracyHigh",@"CIDetectorTracking":@"1"};
// 创建探测器的对象
CIDetector * FaceDetector = [CIDetectordetectorOfType:CIDetectorTypeFacecontext:niloptions:FaceDetectorDict];
// 获取探测到得人脸信息
DetectionDataArray * FaceArray = [FaceDetectorfeaturesInImage:FaceCImageoptions:FaceDetectorDict];
// 获取人脸的个部位的信息
for (CIFaceFeature * FaceFeaturein FaceArray) {
// ************
// 人脸大小
CGRect FaceRect = FaceFeature.bounds;
NSLog(@"H:%f W:%f",FaceRect.size.height,FaceRect.size.width);
// 人脸的位置
NSLog(@"X:%f Y:%f",FaceRect.origin.x,FaceRect.origin.y);
/*
* 输出信息的校验
* 2016-07-09 17:55:05.210 CIDetector_zsj[1849:136896] H:212.000000 W:212.000000
* 2016-07-09 17:55:05.211 CIDetector_zsj[1849:136896] X:203.000000 Y:419.000000
* 解说:
* 原始图片的大小是 600 * 854 px.
* 图片上人脸的位置 X: 210 px Y: 420 px (对于位置的展示,请理解画布的原点在哪里为准?)
* 图片上人脸的大小(正方形算) 213 * 213 px
* 输出人脸信息相比精度还是很高
*/
// *************
// 人的眼睛 (左右)
// 判断人的眼睛在图上是否显示
EyesLeft IsSave= [FaceFeaturehasLeftEyePosition];
if (IsSave) {
// 人左眼的位置
CGPoint EyesLeftCGPoint = FaceFeature.leftEyePosition;
NSLog(@"EyeLeft_X:%f EyesLeft_Y:%f",EyesLeftCGPoint.x,EyesLeftCGPoint.y);
/*
* 输出数据: 2016-07-09 18:42:20.295 CIDetector_zsj[2156:155810]
EyeLeft_X:284.000000 EyesLeft_Y:584.000000
*
* 实际图片上的左眼位置:
EyeLeft_X:284.000000 EyesLeft_Y:587.000000
*
* 两组数据进行对比,几乎一样,精度很高。
*/
// 左眼是否是关闭的
EyesLeft CloseEyesLeft = FaceFeature.leftEyeClosed;
if (CloseEyesLeft) {
NSLog(@"左眼是闭上的");
}else{
NSLog(@"左眼是睁开的");
}
/*
* 输出信息:
2016-07-09 18:48:40.408 CIDetector_zsj[2241:159731]左眼是睁开的 (符合实际)
*/
}
// *******************
// 人的右眼
EyesRight IsSaveR = FaceFeature.hasRightEyePosition;
if (IsSaveR) {
// 获取人眼右眼的位置
CGPoint EyesRightCGPoint = FaceFeature.rightEyePosition;
NSLog(@"EyesRight_X:%f EyesRight_Y:%f",EyesRightCGPoint.x,EyesRightCGPoint.y);
/*
* 输出右眼位置信息:
2016-07-09 19:07:45.125 CIDetector_zsj[2335:164771]
EyesRight_X:368.000000 EyesRight_Y:565.000000
*
* 实际的右眼的位置:
EyesRight_X:367.000000 EyesRight_Y:565.000000
*
* 数据比较接近,精度很高
*/
// 检测人都眼睛是否是闭上的
EyesRight CloseEyesRight = FaceFeature.rightEyeClosed;
if (CloseEyesRight) {
NSLog(@"右眼是闭上的");
}else{
NSLog(@"右眼是睁开的");
}
/*
* 输出信息:
2016-07-09 19:12:38.295 CIDetector_zsj[2387:167289]右眼是睁开的 (符合实际)
*/
}
// ***********************
// 人的嘴
// 判断人的嘴是否在图片上
Mouth MouthIsSave = FaceFeature.hasMouthPosition;
if (MouthIsSave) {
CGPoint MouthCgPoint = FaceFeature.mouthPosition;
NSLog(@"Mouth_X:%f Mouth_Y:%f",MouthCgPoint.x,MouthCgPoint.y);
/**
* 输出信息:
2016-07-09 19:19:22.408 CIDetector_zsj[2474:171661]
Mouth_X:298.000000 Mouth_Y:474.000000
*
* 实际嘴的位置:
Mouth_X:301.000000 Mouth_Y:474.000000
*
* 数据分析的结果两组数据几乎一样,精度很高
*/
// 判断人脸是否微笑
Mouth IsSmile = FaceFeature.hasSmile;
if (IsSmile) {
NSLog(@"人在微笑");
}else{
NSLog(@"人不在微笑");
}
/*
* 输出信息:
2016-07-09 19:27:22.460 CIDetector_zsj[2561:175632]人不在微笑 (符合实际)
*/
}
// ****************
// 获取人脸的角度
BOOL FaceAngle = FaceFeature.hasFaceAngle;
if (FaceAngle) {
float Angle = FaceFeature.faceAngle;
NSLog(@"FaceAngle:%f",Angle);
/*
* 输出的信息
* 2016-07-09 19:32:42.485 CIDetector_zsj[2638:178512] FaceAngle:13.000000
*
* 解说:以中为 0度 ,向左为正(0~90)度 ,向右为(0~90)度
*/
}
// 获取追踪对象
BOOL Istracking = FaceFeature.hasTrackingID;
if (Istracking) {
// 获取追踪的对象的ID
int TrackingID = FaceFeature.trackingID;
NSLog(@"Tracking:%d",TrackingID);
}
}
// **************∑∑∑二维码识别***************************
// 导入识别对象
CIImage * QRcodeImage = [CIImageimageWithData:UIImagePNGRepresentation([UIImageimageNamed:@"tixingma.png"])];
// 配置探测器的性质
Performance * QRcodeDetectorDict =@{@"CIDetectorAccuracy":@"CIDetectorAccuracyHigh"};
// 创建探测器
CIDetector * QRCodeDetector = [CIDetectordetectorOfType:CIDetectorTypeQRCodecontext:niloptions:QRcodeDetectorDict];
// 获取图像信息
QRCodeArray * CodeArray = [QRCodeDetectorfeaturesInImage:QRcodeImage];
for (CIQRCodeFeature * QRCodeFeature in CodeArray) {
// 获取图像的大小
CGRect QRCodeRect = QRCodeFeature.bounds;
NSLog(@"W:%f H:%f",QRCodeRect.size.width,QRCodeRect.size.height);
// 二维码的内容
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
UIAlertController * QRCodeAlert = [UIAlertControlleralertControllerWithTitle:@"二维码识别"message:QRCodeFeature.messageStringpreferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * Action = [UIAlertActionactionWithTitle:@"OK"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {
}];
[QRCodeAlert addAction:Action];
[selfpresentViewController:QRCodeAlertanimated:YEScompletion:nil];
});
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end