【问题标题】:face detection iOS from camera来自相机的人脸检测iOS
【发布时间】:2014-05-25 19:31:49
【问题描述】:

我收到一个图像视图

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];

    [self dismissViewControllerAnimated:YES completion:nil];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = info[UIImagePickerControllerOriginalImage];

        //imgvprofileImage.image = image;
        //[self detectForFacesInUIImage:[UIImage imageNamed:@"image00.jpg"]];

        [self detectForFacesInUIImage:image];
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }
}

当我发送这样的照片时

[self detectForFacesInUIImage:[UIImage imageNamed:@"image00.jpg"]]; 

检测效果很好并找到了人脸,但是当我使用从相机返回的图像时它不起作用。

 [self detectForFacesInUIImage:image]

这是我用来检测人脸的函数

-(void)detectForFacesInUIImage:(UIImage *)facePicture
{
    CIImage* image = [CIImage imageWithCGImage:facePicture.CGImage];

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

    NSArray* features = [detector featuresInImage:image];

    if (features.count == 0) {
        NSLog(@"There is no faces in captured image ") ;
    }

    for(CIFaceFeature* faceObject in features)
    {
        CGRect modifiedFaceBounds = faceObject.bounds;
        modifiedFaceBounds.origin.y = facePicture.size.height-faceObject.bounds.size.height-faceObject.bounds.origin.y;

        [self addSubViewWithFrame:facePicture toRect:modifiedFaceBounds] ;
    }
}

【问题讨论】:

    标签: ios objective-c uiimage ios-camera


    【解决方案1】:

    问题在于图像方向。

    不记得我在哪里拿的,但它有效:

    - (void) detectForFaces:(CGImageRef)facePicture orientation:(UIImageOrientation)orientation {
    
    
        CIImage* image = [CIImage imageWithCGImage:facePicture];
    
        CIContext *context = [CIContext contextWithOptions:nil];                    // 1
        NSDictionary *opts = @{ CIDetectorAccuracy : CIDetectorAccuracyLow };      // 2
        CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                                  context:context
                                                  options:opts];                    // 3
    
        int exifOrientation;
        switch (orientation) {
            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;
        }
    
    
        opts = @{ CIDetectorImageOrientation :[NSNumber numberWithInt:exifOrientation
                                               ] };
    
        NSArray *features = [detector featuresInImage:image options:opts];
    
        if ([features count] > 0) {
            CIFaceFeature *face = [features lastObject];
            NSLog(@"%@", NSStringFromCGRect(face.bounds));
        }
    }
    


    使用方法:

    UIImage *image = // some image here;
    [self detectForFaces:image.CGImage orientation:image.imageOrientation];
    

    【讨论】:

    • 如何获取参数 UIImageOrientation ?
    • @mohamed UIImage 有属性imageOrientation
    • UIImage *image = info[UIImagePickerControllerOriginalImage]; [self detectForFaces:image.CGImageorientation:[image get]] 我怎么称呼这是对的??我没找到房产??
    • 和之前的检测不准确??
    • 当我 corp 之前的图像完全是脸部,但现在它根本不准确??
    【解决方案2】:

    这里小修正

    - (void) detectForFaces:(UIImage *)facePicture orientation:(UIImageOrientation)orientation {
    
    
    CIImage* image = [CIImage imageWithCGImage:facePicture.CGImage];
    
    CIContext *context = [CIContext contextWithOptions:nil];                    // 1
    NSDictionary *opts = @{ CIDetectorAccuracy : CIDetectorAccuracyLow };      // 2
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                              context:context
                                              options:opts];                    // 3
    
    int exifOrientation;
    switch (orientation) {
        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;
    }
    
    
    opts = @{ CIDetectorImageOrientation :[NSNumber numberWithInt:exifOrientation
                                           ] };
    
    NSArray *features = [detector featuresInImage:image options:opts];
    
    if ([features count] > 0) {
        CIFaceFeature *face = [features lastObject];
        NSLog(@"%@", NSStringFromCGRect(face.bounds));
    }}
    

    UIImage *image = // some image here; [self detectForFaces:image orientation:image.imageOrientation];

    只发送图片而不是 image.CGImage 这对我有用

    【讨论】:

      猜你喜欢
      • 2015-08-13
      • 2016-09-22
      • 2013-07-16
      • 2013-04-14
      • 2017-05-12
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      相关资源
      最近更新 更多