无法通过Iphone相机拍摄的图像中检测到脸部

时间:2012-11-09 04:14:58

标签: iphone ios image

我有问题。我用2张图片。一个是从互联网下载。另一个是由iPhone的相机拍摄的。 我使用 CIDetector 来检测2张图像中的脸部。它从互联网下载的图像工作完美。但另一方面,它无法检测或发现错误。

我检查了很多图片。结果是一样的。

3 个答案:

答案 0 :(得分:0)

试试这个

   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);

        }

如果你一直在使用前置摄像头,请添加此

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

了解更多信息

示例:https://github.com/beetlebugorg/PictureMe

iOS Face Detection Issue

Face Detection issue using CIDetector

https://stackoverflow.com/questions/4332868/detect-face-in-iphone?rq=1

答案 1 :(得分:0)

我尝试上面的代码。它可以检测到Iphone捕获的图像。但它无法检测来自互联网的图像下载。这是我的代码

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

CIImage *ciImage = [CIImage imageWithCGImage: [facePicture CGImage]];
NSDictionary* imageOptions = [NSDictionary dictionaryWithObject:[NSNumber        numberWithInt:6] forKey:CIDetectorImageOrientation];
NSArray *features = [detector featuresInImage:ciImage options:imageOptions];

当它被发现时。我按代码显示

for (CIFaceFeature *feature in features) {

// //设置红色特征颜色

    CGRect faceRect = [feature bounds];

    CGContextSetRGBFillColor(context, 0.0f, 0.0f, 0.0f, 0.5f);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 2.0f * scale);
    CGContextAddRect(context, feature.bounds);
    CGContextDrawPath(context, kCGPathFillStroke);
    CGContextDrawImage(context, faceRect, [imgDraw CGImage]);

这不是正确的立场。它向右移动了一段距离。

答案 2 :(得分:0)

我遇到了同样的问题。您可以在检测之前更改图像的大小。

CGSize size = CGSizeMake(cameraCaptureImage.size.width, cameraCaptureImage.size.height);
UIGraphicsBeginImageContext(size);
[cameraCaptureImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
cameraCaptureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();