首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不管我做什么,CIDetector总是零

不管我做什么,CIDetector总是零
EN

Stack Overflow用户
提问于 2013-09-27 16:28:53
回答 2查看 2.7K关注 0票数 2

试图得到一个简单的证明,概念与苹果的脸检测API。我看过其他几个例子,包括苹果的SquareCam和这个https://github.com/jeroentrappers/FaceDetectionPOC

基于这些,我似乎遵循了正确的模式来运行API,但是我被困住了。无论我做什么,我的脸探测器的CIDetector总是零!

我会非常感谢任何帮助,线索-提示-建议!

代码语言:javascript
复制
-(void)initCamera{
session = [[AVCaptureSession alloc]init];

AVCaptureDevice *device;
/*
if([self frontCameraAvailable]){
    device = [self frontCamera];
}else{
    device = [self backCamera];
}*/

device = [self frontCamera];
isUsingFrontFacingCamera = YES;
NSError *error = nil;

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

if(input && [session canAddInput:input]){
    [session addInput:input];
}else{
    NSLog(@"Error %@", error);
    //make this Dlog...
}


videoDataOutput = [[AVCaptureVideoDataOutput alloc]init];
NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject:
                                   [NSNumber numberWithInt:kCMPixelFormat_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
[videoDataOutput setVideoSettings:rgbOutputSettings];
[videoDataOutput setAlwaysDiscardsLateVideoFrames:YES];

videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
[videoDataOutput setSampleBufferDelegate:self queue:videoDataOutputQueue];
[[videoDataOutput connectionWithMediaType:AVMediaTypeVideo]setEnabled:YES];

if ([session canAddOutput:videoDataOutput]) {
    [session addOutput:videoDataOutput];
}




[self embedPreviewInView:self.theImageView];



[session startRunning];



}



-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{

CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(__bridge NSDictionary *)attachments];


if(attachments){
    CFRelease(attachments);
}



UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];


NSDictionary *imageOptions = @{CIDetectorImageOrientation:[self exifOrientation:curDeviceOrientation] };


NSDictionary *detectorOptions = @{CIDetectorAccuracy: CIDetectorAccuracyLow};

CIDetector *faceDetector = [CIDetector detectorOfType:CIFeatureTypeFace context:nil options:detectorOptions];


NSArray *faceFeatures = [faceDetector featuresInImage:ciImage options:imageOptions];
if([faceFeatures count]>0){
    NSLog(@"GOT a face!");
    NSLog(@"%@", faceFeatures);

}


dispatch_async(dispatch_get_main_queue(), ^(void) {
    //NSLog(@"updating main thread");
});


}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-27 19:04:12

我假设您使用的是文章,因为我也是,也遇到了同样的问题。实际上他的代码里有个bug。CIDetector实例化应该如下所示:

代码语言:javascript
复制
CIDetector *smileDetector = [CIDetector detectorOfType:CIDetectorTypeFace
                            context:context 
                            options:@{CIDetectorTracking: @YES, 
                                      CIDetectorAccuracy: CIDetectorAccuracyLow}];

注意,检测器类型是CIDetectorTypeFace,而不是CIDetectorSmile。CIDetectorSmile是一个功能选项,而不是检测器类型,因此要提取微笑(而不是所有面孔),请使用以下代码:

代码语言:javascript
复制
NSArray *features = [smileDetector featuresInImage:image options:@{CIDetectorSmile: @YES}];
票数 0
EN

Stack Overflow用户

发布于 2013-10-09 05:57:16

代码语言:javascript
复制
CIDetector *smileDetector = [CIDetector detectorOfType:CIDetectorTypeFace
                            context:context 
                            options:@{CIDetectorTracking: @YES, 
                                      CIDetectorAccuracy: CIDetectorAccuracyLow}];
NSArray *features = [smileDetector featuresInImage:image options:@{CIDetectorSmile:@YES}];
if (([features count] > 0) && (((CIFaceFeature *)features[0]).hasSmile)) {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(didFinishWritingImage), features);
} else {
    self.label.text = @"Say Cheese!"
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19055760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档