gpt4 book ai didi

ios - CIDetector 和 UIImagePickerController

转载 作者:可可西里 更新时间:2023-11-01 04:07:36 29 4
gpt4 key购买 nike

我正在尝试实现内置的 iOS 5 人脸检测 API。我正在使用 UIImagePickerController 的实例来允许用户拍照,然后我尝试使用 CIDetector 来检测面部特征。不幸的是,featuresInImage 总是返回一个大小为 0 的数组。

代码如下:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* picture = [info objectForKey:UIImagePickerControllerOriginalImage];

NSNumber *orientation = [NSNumber numberWithInt:
[picture imageOrientation]];
NSDictionary *imageOptions =
[NSDictionary dictionaryWithObject:orientation
forKey:CIDetectorImageOrientation];

CIImage *ciimage = [CIImage imageWithCGImage:[picture CGImage]
options:imageOptions];


NSDictionary *detectorOptions =
[NSDictionary dictionaryWithObject:CIDetectorAccuracyLow
forKey:CIDetectorAccuracy];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:detectorOptions];

NSArray *features = [detector featuresInImage:ciimage];
NSLog(@"Feature size: %d", features.count);
}

这总是返回 0 个特征。但是,如果我使用应用程序内置文件中的 UIImage,人脸检测效果很好。

我正在使用这个 Pragmatic Bookshelf article 中的代码.

就其值(value)而言,我认为错误是当我将 UIImage 从相机转换为 CIImage 时,但它可以是任何东西。

最佳答案

@tonyc 您的解决方案仅适用于“UIImageOrientationRight”的特定情况。问题来自 UIImage 和 CIDetectorImageOrientation 中的方向之间的差异。来自 iOS 文档:

CIDetectorImageOrientation

A key used to specify the display orientation of the image whose features you want to detect. This keyis an NSNumber object with the same value as defined by the TIFF andEXIF specifications; values can range from 1 through 8. The valuespecifies where the origin (0,0) of the image is located. If notpresent, the default value is 1, which means the origin of the imageis top, left. For details on the image origin specified by each value,see kCGImagePropertyOrientation.

Available in iOS 5.0 and later.

Declared in CIDetector.h.

所以现在的问题是这两个方向之间的转换,这是我在代码中所做的,我测试过它适用于所有方向:

int exifOrientation;
switch (self.image.imageOrientation) {
case UIImageOrientationUp:
exifOrientation = 1;
break;
case UIImageOrientationDown:
exifOrientation = 3;
break;
case UIImageOrientationLeft:
exifOrientation = 8;
break;
case UIImageOrientationRight:
exifOrientation = 6;
break;
case UIImageOrientationUpMirrored:
exifOrientation = 2;
break;
case UIImageOrientationDownMirrored:
exifOrientation = 4;
break;
case UIImageOrientationLeftMirrored:
exifOrientation = 5;
break;
case UIImageOrientationRightMirrored:
exifOrientation = 7;
break;
default:
break;
}

NSDictionary *detectorOptions = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh }; // TODO: read doc for more tuneups
CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:detectorOptions];

NSArray *features = [faceDetector featuresInImage:[CIImage imageWithCGImage:self.image.CGImage]
options:@{CIDetectorImageOrientation:[NSNumber numberWithInt:exifOrientation]}];

关于ios - CIDetector 和 UIImagePickerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10746212/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com