I receive an image view
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaT
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
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];