• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - objective-C : No Matter what I do CIDetector is always nil

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:58:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

试图通过 Apple 的人脸检测 API 获得一个简单的概念证明。我看过其他几个例子,包括 Apple 的 SquareCam,还有这个 https://github.com/jeroentrappers/FaceDetectionPOC

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

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

-(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] forKeyid)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)captureOutputAVCaptureOutput *)captureOutput didOutputSampleBufferCMSampleBufferRef)sampleBuffer fromConnectionAVCaptureConnection *)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");
});


}



Best Answer-推荐答案


CIDetector *smileDetector = [CIDetector detectorOfType:CIDetectorTypeFace
                            context:context 
                            options{CIDetectorTracking: @YES, 
                                      CIDetectorAccuracy: CIDetectorAccuracyLow}];
NSArray *features = [smileDetector featuresInImage:image options{CIDetectorSmileYES}];
if (([features count] > 0) && (((CIFaceFeature *)features[0]).hasSmile)) {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(didFinishWritingImage), features);
} else {
    self.label.text = @"Say Cheese!"
}

关于ios - objective-C : No Matter what I do CIDetector is always nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19055760/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap