gpt4 book ai didi

ios - 从相机进行人脸检测 iOS

转载 作者:可可西里 更新时间:2023-11-01 03:10:13 25 4
gpt4 key购买 nike

我收到一个 ImageView

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

最佳答案

问题出在图像方向上。

不记得我把它放在哪里了,但它有效:

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

关于ios - 从相机进行人脸检测 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23016059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com