GPUImage实现人脸实时识别
最近在研究OC的生物活检方面的实现,发现SDK中自带有相应的功能类,则进行了调研与实现。
实现过程中发现一个比较坑人的一个地方,就是GPUIMAGE这个框架里面对于视频采集使用的YUV格式,而YUV格式无法与OC的类库进行配合实现实时识别。
现在我们来剖析一下GPUImageVideoCamera的实现:
-
@interface GPUImageVideoCamera : GPUImageOutput <AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate>
-
-
-
- (id)initWithSessionPreset:(NSString *)sessionPreset cameraPosition:(AVCaptureDevicePosition)cameraPosition;
可以看到提供了一个初始化方法,此初始化方法内部的代码如下:
-
- (id)initWithSessionPreset:(NSString *)sessionPreset cameraPosition:(AVCaptureDevicePosition)cameraPosition;
-
{
-
if (!(self = [super init]))
-
{
-
return nil;
-
}
-
-
cameraProcessingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
-
audioProcessingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0);
-
-
frameRenderingSemaphore = dispatch_semaphore_create(1);
-
-
_frameRate = 0; // This will not set frame rate unless this value gets set to 1 or above
-
_runBenchmark = NO;
-
capturePaused = NO;
-
outputRotation = kGPUImageNoRotation;
-
internalRotation = kGPUImageNoRotation;
-
captureAsYUV = YES;
-
_preferredConversion = kColorConversion709;
-
-
// Grab the back-facing or front-facing camera
-
_inputCamera = nil;
-
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
-
for (AVCaptureDevice *device in devices)
-
{
-
if ([device position] == cameraPosition)
-
{
-
_inputCamera = device;
-
}
-
}
-
-
if (!_inputCamera) {
-
return nil;
-
}
-
-
// Create the capture session
-
_captureSession = [[AVCaptureSession alloc] init];
-
-
[_captureSession beginConfiguration];
-
-
// Add the video input
-
NSError *error = nil;
-
videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:_inputCamera error:&error];
-
if ([_captureSession canAddInput:videoInput])
-
{
-
[_captureSession addInput:videoInput];
-
}
-
-
// Add the video frame output
-
videoOutput = [[AVCaptureVideoDataOutput alloc] init];
-
[videoOutput setAlwaysDiscardsLateVideoFrames:NO];
-
-
// if (captureAsYUV && [GPUImageContext deviceSupportsRedTextures])
-
if (captureAsYUV && [GPUImageContext supportsFastTextureUpload])
-
{
-
BOOL supportsFullYUVRange = NO;
-
NSArray *supportedPixelFormats = videoOutput.availableVideoCVPixelFormatTypes;
-
for (NSNumber *currentPixelFormat in supportedPixelFormats)
-
{
-
if ([currentPixelFormat intValue] == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
-
{
-
supportsFullYUVRange = YES;
-
}
-
}
-
-
if (supportsFullYUVRange)
-
{
-
[videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
-
isFullYUVRange = YES;
-
}
-
else
-
{
-
[videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
-
isFullYUVRange = NO;
-
}
-
}
-
else
-
{
-
[videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
-
}
-
-
runSynchronouslyOnVideoProcessingQueue(^{
-
-
if (captureAsYUV)
-
{
-
[GPUImageContext useImageProcessingContext];
-
// if ([GPUImageContext deviceSupportsRedTextures])
-
// {
-
// yuvConversionProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:kGPUImageVertexShaderString fragmentShaderString:kGPUImageYUVVideoRangeConversionForRGFragmentShaderString];
-
// }
-
// else
-
// {
-
if (isFullYUVRange)
-
{
-
yuvConversionProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:kGPUImageVertexShaderString fragmentShaderString:kGPUImageYUVFullRangeConversionForLAFragmentShaderString];
-
}
-
else
-
{
-
yuvConversionProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:kGPUImageVertexShaderString fragmentShaderString:kGPUImageYUVVideoRangeConversionForLAFragmentShaderString];
-
}
-
-
// }
-
-
if (!yuvConversionProgram.initialized)
-
{
-
[yuvConversionProgram addAttribute:@"position"];
-
[yuvConversionProgram addAttribute:@"inputTextureCoordinate"];
-
-
if (![yuvConversionProgram link])
-
{
-
NSString *progLog = [yuvConversionProgram programLog];
-
NSLog(@"Program link log: %@", progLog);
-
NSString *fragLog = [yuvConversionProgram fragmentShaderLog];
-
NSLog(@"Fragment shader compile log: %@", fragLog);
-
NSString *vertLog = [yuvConversionProgram vertexShaderLog];
-
NSLog(@"Vertex shader compile log: %@", vertLog);
-
yuvConversionProgram = nil;
-
NSAssert(NO, @"Filter shader link failed");
-
}
-
}
-
-
yuvConversionPositionAttribute = [yuvConversionProgram attributeIndex:@"position"];
-
yuvConversionTextureCoordinateAttribute = [yuvConversionProgram attributeIndex:@"inputTextureCoordinate"];
-
yuvConversionLuminanceTextureUniform = [yuvConversionProgram uniformIndex:@"luminanceTexture"];
-
yuvConversionChrominanceTextureUniform = [yuvConversionProgram uniformIndex:@"chrominanceTexture"];
-
yuvConversionMatrixUniform = [yuvConversionProgram uniformIndex:@"colorConversionMatrix"];
-
-
[GPUImageContext setActiveShaderProgram:yuvConversionProgram];
-
-
glEnableVertexAttribArray(yuvConversionPositionAttribute);
-
glEnableVertexAttribArray(yuvConversionTextureCoordinateAttribute);
-
}
-
});
-
-
[videoOutput setSampleBufferDelegate:self queue:cameraProcessingQueue];
-
if ([_captureSession
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgaback
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13