版本记录
版本号 | 时间 |
---|---|
V1.0 | 2018.01.31 |
前言
人脸识别是图像识别技术中的一种,广泛的应用于很多领域,接下来这几篇我们就一起来研究几种关于人脸识别的技术。感兴趣的可以参考上面几篇文章。
1. 人脸识别技术 (一) —— 基于CoreImage实现对静止图片中人脸的识别
2. 人脸识别技术 (二) —— 基于CoreImage实现视频中人脸的识别
基于AVFoundation的视频中人脸识别技术
前两篇文章我们利用CoreImage对静止图像和视频中的人脸进行识别,这一篇我们就利用AVFoundation进行视频中的人脸识别,主要是用类AVCaptureMetadataOutput
。
功能实现
下面我们就看一下实现代码。
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController () <AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureDevice *captureDevice;
@property (nonatomic, strong) AVCaptureDeviceInput *captureVideoDeviceInput;
@property (nonatomic, strong) AVCaptureMetadataOutput *metaDataOutput;
@property (nonatomic, strong) AVCaptureConnection *captureConnection;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
@property (nonatomic, strong) NSMutableArray <UIView *> *faceViewArrM;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.faceViewArrM = [NSMutableArray array];
self.captureSession = [[AVCaptureSession alloc] init];
if ([self.captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {
self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
}
else {
self.captureSession.sessionPreset = AVCaptureSessionPreset1280x720;
}
for (AVCaptureDevice *device in [AVCaptureDevice devices]) {
if ([device hasMediaType:AVMediaTypeVideo]) {
if (device.position == AVCaptureDevicePositionFront) {
self.captureDevice = device;
}
}
}
//添加输入
[self addVideoInput];
//添加输出
[self addVideoOutput];
//添加预览图层
[self addPreviewLayer];
[self.captureSession commitConfiguration];
[self.captureSession startRunning];
}
#pragma mark - Object Private Function
- (void)addVideoInput
{
NSError *error;
self.captureVideoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:self.captureDevice error:&error];
if (error) {
return;
}
if ([self.captureSession canAddInput:self.captureVideoDeviceInput]) {
[self.captureSession addInput:self.captureVideoDeviceInput];
}
}
- (void)addVideoOutput
{
self.metaDataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.metaDataOutput setMetadataObjectsDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)];
if ([self.captureSession canAddOutput:self.metaDataOutput]) {
[self.captureSession addOutput:self.metaDataOutput];
}
self.metaDataOutput.metadataObjectTypes = @[AVMetadataObjectTypeFace];
//设置链接管理对象
self.captureConnection = [self.metaDataOutput connectionWithMediaType:AVMediaTypeVideo];
//视频旋转方向设置
self.captureConnection.videoScaleAndCropFactor = self.captureConnection.videoMaxScaleAndCropFactor;;
//视频稳定设置
if ([self.captureConnection isVideoStabilizationSupported]) {
self.captureConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
}
}
- (void)addPreviewLayer
{
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
self.previewLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.previewLayer];
}
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
if (metadataObjects.count > 0) {
NSLog(@"检测到了人脸,数目为%ld", metadataObjects.count);
NSLog(@"%@", metadataObjects);
}
}
@end
下面看一下部分输出结果
2018-01-31 15:59:56.128966+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.129194+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x10158e0d0, faceID=2, bounds={0.1,0.3 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255234842406416>"
)
2018-01-31 15:59:56.162409+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.162630+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x1014a4930, faceID=2, bounds={0.1,0.3 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255234875712416>"
)
2018-01-31 15:59:56.195745+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.195976+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x101491800, faceID=2, bounds={0.1,0.3 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255234909018416>"
)
2018-01-31 15:59:56.228822+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.229070+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x1014aa890, faceID=2, bounds={0.1,0.3 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255234942324458>"
)
2018-01-31 15:59:56.262194+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.262442+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x101589740, faceID=2, bounds={0.1,0.3 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255234975630458>"
)
2018-01-31 15:59:56.295511+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.295749+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x10158de70, faceID=2, bounds={0.1,0.3 0.4x0.6}, rollAngle=300.0, yawAngle=0.0, time=255235008936541>"
)
2018-01-31 15:59:56.328751+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.328978+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x10158e0d0, faceID=2, bounds={0.0,0.4 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255235042242708>"
)
2018-01-31 15:59:56.362188+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.362413+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x1015810f0, faceID=2, bounds={0.0,0.4 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255235075548583>"
)
2018-01-31 15:59:56.395353+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.395606+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x10158de70, faceID=2, bounds={0.0,0.4 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255235108854666>"
)
2018-01-31 15:59:56.428496+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.428747+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x101580fb0, faceID=2, bounds={0.0,0.4 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255235142160875>"
)
2018-01-31 15:59:56.462256+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.462490+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x1015810f0, faceID=2, bounds={0.0,0.4 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255235175466875>"
)
2018-01-31 15:59:56.495412+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.495651+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x10158de70, faceID=2, bounds={0.0,0.4 0.3x0.6}, rollAngle=300.0, yawAngle=0.0, time=255235208772750>"
)
2018-01-31 15:59:56.528425+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.528668+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x101535360, faceID=2, bounds={0.0,0.4 0.4x0.6}, rollAngle=300.0, yawAngle=0.0, time=255235242078875>"
)
2018-01-31 15:59:56.561876+0800 JJFaceDetector_demo3[4747:1474803] 检测到了人脸,数目为1
2018-01-31 15:59:56.562123+0800 JJFaceDetector_demo3[4747:1474803] (
"<AVMetadataFaceObject: 0x10158e0d0, faceID=2, bounds={0.0,0.3 0.4x0.6}, rollAngle=300.0, yawAngle=0.0, time=255235275384791>"
)
这里检测到人脸就会有输出,检测不到就不会有输出,比如我们不出现在视频中就没有输出,或者将对象类型从人脸修改为二维码,那么即使你出现在视频中,也不会有任何输出。
self.metaDataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
这里就不给大家展示效果了,原因只有一个 —— 因为长的太丑。
几个问题
1. info.plist增加相机权限的key
这个是老问题,一笔带过而已,不多说。
2. metaDataOutput.metadataObjectTypes的设置时机
假如我们向下面这么写代码
- (void)addVideoOutput
{
self.metaDataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.metaDataOutput setMetadataObjectsDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)];
self.metaDataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
if ([self.captureSession canAddOutput:self.metaDataOutput]) {
[self.captureSession addOutput:self.metaDataOutput];
}
... ...
}
运行一下就会崩溃,输出消息如下
2018-01-31 17:09:55.220854+0800 JJFaceDetector_demo3[4760:1487855] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] Unsupported type found - use -availableMetadataObjectTypes'
*** First throw call stack:
(0x185fb2364 0x1851f8528 0x18b988308 0x1004495d0 0x100449148 0x18f493ae8 0x18f4936c0 0x18f49a28c 0x18f4974ec 0x18f5063a0 0x18f6f3d10 0x18f6f8aa8 0x18f9942d4 0x18fc6c12c 0x18f993f38 0x18f994798 0x1901197d0 0x190119674 0x18fe872dc 0x1900203dc 0x18fe8718c 0x18fc6b78c 0x18f6f71ec 0x18fb0cde8 0x1886761f0 0x18867eaf8 0x10081128c 0x10081d9e4 0x1886aa7f8 0x1886aa49c 0x1886aaa38 0x185f5a97c 0x185f5a8fc 0x185f5a184 0x185f57d5c 0x185e77e58 0x187d24f84 0x18f4f767c 0x10044a0f8 0x18599456c)
libc++abi.dylib: terminating with uncaught exception of type NSException
在网上也碰到了别人类似的遭遇,只需要更改设置时机就可以了,崩溃的原因是:metadataObjectTypes
要在AVCaptureSession
类对象[self.captureSession addOutput:self.metaDataOutput];
即添加了AVCaptureMetadataOutput
的对象后,方可对其进行设置。如下所示:
- (void)addVideoOutput
{
self.metaDataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.metaDataOutput setMetadataObjectsDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)];
if ([self.captureSession canAddOutput:self.metaDataOutput]) {
[self.captureSession addOutput:self.metaDataOutput];
}
self.metaDataOutput.metadataObjectTypes = @[AVMetadataObjectTypeFace];
... ...
}
这样设置就可以了。
后记
本篇已结束,后面更精彩~~~