【问题标题】:iOS Face Detection IssueiOS 人脸检测问题
【发布时间】:2012-02-25 15:28:06
【问题描述】:

我尝试在 iOS 5 中使用 CoreImage 的人脸检测,但它没有检测到任何东西。我正在尝试使用以下代码检测相机刚刚捕获的图像中的人脸:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSDictionary *detectorOptions = [[NSDictionary alloc] initWithObjectsAndKeys:CIDetectorAccuracyHigh, CIDetectorAccuracy, nil];     
    CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:detectorOptions];
    NSArray *features = [faceDetector featuresInImage:image.CIImage];
    NSLog(@"Features = %@", features);
    [self dismissModalViewControllerAnimated:YES];
}

这编译并运行良好,但无论图像中有什么,特征数组始终为空......有什么想法吗?

【问题讨论】:

    标签: iphone ios5 face-detection


    【解决方案1】:

    我无法直接回复你的@14:52 评论 Vic320,但我一直在玩前置摄像头进行人脸检测 - 因为我无法让前置摄像头选择,所以我转了一圈又一圈压在我的脸上……

    事实证明它对旋转非常敏感 - 我注意到当我的 iPad2 竖直放置时(正如您在使用前置摄像头时所期望的那样),我的识别准确率不到 10%。一时兴起,将其侧身转动,前置摄像头获得 100% 的识别率。

    如果您始终以纵向方式使用前置摄像头,解决此问题的简单方法是添加这个小 sn-p:

    NSDictionary* imageOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:6] forKey:CIDetectorImageOrientation];
    NSArray* features = [detector featuresInImage:image options:imageOptions];
    

    其中的 6 会强制探测器以纵向模式运行。 Apple 的SquareCam Sample 有一大堆实用方法,可以在需要动态确定方向时确定您所处的方向。

    【讨论】:

    • 今天仍然适用!绝对精彩
    【解决方案2】:

    好的,仔细阅读文档总是有帮助的。在 UIImage 文档中,在 CIImage 属性下它说:“如果 UIImage 对象是使用 CGImageRef 初始化的,则该属性的值为 nil。”显然, UIImagePickerController 确实从 CGImageRef 初始化图像,因为该属性确实为零。要使上述代码正常工作,您需要添加:

    CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage];
    

    并更改此行:

    NSArray *features = [faceDetector featuresInImage:ciImage];
    

    我注意到的另一件大事是,静态图像中的人脸检测实际上不适用于前置摄像头的低分辨率图像!每次我使用背面的高分辨率相机时,它都能正常工作。也许该算法已针对高分辨率进行了调整...

    【讨论】:

      【解决方案3】:

      尝试关注。假设您在图像变量中加载照片:

      NSDictionary *options = [NSDictionary dictionaryWithObject: CIDetectorAccuracyLow forKey: CIDetectorAccuracy];
                  CIDetector  *detector = [CIDetector detectorOfType: CIDetectorTypeFace context: nil options: options];
      
              CIImage *ciImage = [CIImage imageWithCGImage: [image CGImage]];
              NSNumber *orientation = [NSNumber numberWithInt:[image imageOrientation]+1];
              NSDictionary *fOptions = [NSDictionary dictionaryWithObject:orientation forKey: CIDetectorImageOrientation];
                  NSArray *features = [detector featuresInImage:ciImage options:fOptions];
                  for (CIFaceFeature *f in features) {
      
                      NSLog(@"left eye found: %@", (f. hasLeftEyePosition ? @"YES" : @"NO"));
      
                      NSLog(@"right eye found: %@", (f. hasRightEyePosition ? @"YES" : @"NO"));
      
                      NSLog(@"mouth found: %@", (f. hasMouthPosition ? @"YES" : @"NO"));
      
                      if(f.hasLeftEyePosition)
      
                          NSLog(@"left eye position x = %f , y = %f", f.leftEyePosition.x, f.leftEyePosition.y);
      
                      if(f.hasRightEyePosition)
      
                          NSLog(@"right eye position x = %f , y = %f", f.rightEyePosition.x, f.rightEyePosition.y);
      
                      if(f.hasMouthPosition)
      
                          NSLog(@"mouth position x = %f , y = %f", f.mouthPosition.x, f.mouthPosition.y);
      
                  }
      

      【讨论】:

        【解决方案4】:

        上面的答案都不适合我(ios 8.4)ipad mini 和 ipad air 2

        我的观察结果和 robwormald 一样。旋转 iPad 时人脸检测工作,所以我旋转了 ciImage :)

        let ciImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments)
        let angle = CGFloat(-M_PI/2)
        let rotatedImage = ciImage.imageByApplyingTransform(CGAffineTransformMakeRotation(angle))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-16
          • 2020-05-31
          • 2011-06-25
          • 2023-03-28
          • 2016-06-02
          • 2013-04-14
          • 1970-01-01
          • 2019-11-18
          相关资源
          最近更新 更多