2013-07-18 108 views
1

我想将脸部检测添加到我的应用程序,并且我添加的代码给了我一个CGRect,它与脸部无关。iOS,人脸检测,检测远离脸部的位置

下面的代码

CIImage *cIImage = [CIImage imageWithCGImage:self.imageView.image.CGImage]; 
CIDetector* faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace 
              context:nil options:[NSDictionary 
     dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]]; 
NSArray *features = [faceDetector featuresInImage:cIImage]; 
for(CIFaceFeature* faceObject in features) 
{ 
    FaceLocation.x = faceObject.bounds.origin.x; 
    FaceLocation.y = faceObject.bounds.origin.y; 
}// Here face location is far off away form the actual face 

但这个代码给我一个位置远离实际的脸,我究竟做错了什么?

+0

什么关于边界的大小?你为什么不看完整的界限?当然,原点将在角落。这就是为什么它被称为起源而不是中心 –

回答

2

问题来自UIImage和CIDetectorImageOrientation中的方向之间的差异。从iOS的文档:

CIDetectorImageOrientation

用于指定要检测其特征的图像的显示方向键。此密钥 是一个NSNumber对象,具有与TIFF和 EXIF规范相同的值;值的范围可以从1到8.值 指定图像的原点(0,0)的位置。如果不存在 ,则默认值为1,这表示图像 的原点位于左上方。有关每个值指定的图像原点的详细信息,请参阅kCGImagePropertyOrientation。

适用于iOS 5.0及更高版本。

在CIDetector.h中声明。

您必须指定CIDetectorImageOrientation。这是我做的事:被检测的特征

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 }; 
CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:detectorOptions]; 

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

之后,还需要地图坐标到UIImage的观点,在这里用我的要点是:https://gist.github.com/laoyang/5747004转换的坐标系

+0

嗨,我不知道如何实施这个要点!应该只是复制并粘贴所有内容?我是xcode的新手。 – funkycoldmedia

+0

它已经实施。只需复制该文件,然后使用它。这是CIFaceFeature –

+0

thnx的一个类别,它做到了。 – funkycoldmedia

0

iOS版10斯威夫特3

如果没有兴趣的脸特征

您可以查看苹果example可以人脸检测它的工作非常好,方向

您可以选择面metedata使摄像机跟踪脸部,并显示在脸上的黄色盒子

其具有良好的性能比这example