Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

face detection iOS from camera

I receive an image view

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

When I send a photo like this

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

The detection works well and finds a face but when I use the image returned from the camera it doesn't work.

 [self detectForFacesInUIImage:image]

This is the function i use to detect the face

-(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] ;
    }
}
like image 410
mohamed Avatar asked Apr 11 '14 14:04

mohamed


People also ask

Can a photo be used for iPhone facial recognition?

From Google search: Many people know that Apple's Face ID system is more secure than the default Android facial recognition program. For example, Face ID can't be fooled by a photograph. ... A few more Android phones have adjustable face-unlock settings that can be turned up to prevent being fooled by a photo.

Can cameras detect faces?

Facial analysis algorithms enable security cameras to learn your household's regular faces. This makes for more seamless system control (the camera can "recognize" you at the door and disarm the alarm) and increases the detail of alerts.

Can phone face be fooled by photos?

Turns out you don't even need to go to such hassle as a new study has shown that a high-quality photo of the owner can unlock most of the facial recognition-packing phones around.


1 Answers

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];
like image 119
Volodymyr B. Avatar answered Oct 24 '22 09:10

Volodymyr B.