face detection iOS from camera

后端 未结 2 2010
臣服心动
臣服心动 2021-02-03 13:02

I receive an image view

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaT         


        
相关标签:
2条回答
  • 2021-02-03 13:22

    small Correction here

    - (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];

    send only image not image.CGImage This worked for me

    0 讨论(0)
  • 2021-02-03 13:27

    Problem is in image orientation.

    Can't remember where I took this, but it works:

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


    How to use:

    UIImage *image = // some image here;
    [self detectForFaces:image.CGImage orientation:image.imageOrientation];
    
    0 讨论(0)
提交回复
热议问题