再一次,我很接近,但是没有香蕉。
我正在尝试遵循一些有关面部识别的教程。几乎有以下代码,但我认为它在缩放和UIImageView
在脸部周围放置边框方面缺少一些东西。
我在“照片库”中的照片大小各异(出于某些无法解释的原因),因此我在想象中,他们CIDetector
正在寻找面孔,正在应用CGAffineTransforms
等,然后尝试将其放置在中UIImageView
。但是,从图像(也可以从下面看到)可以看出,它没有被绘制在正确的位置。
的UIImageView
尺寸为280x500,并设置为“填充比例”。
弄清楚正在发生的事情的任何帮助都会很棒!
-(void)detectFaces {
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *image = [CIImage imageWithCGImage:_imagePhotoChosen.image.CGImage options:nil];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];
CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
transform = CGAffineTransformTranslate(transform, 0, -_imagePhotoChosen.image.size.height);
NSArray *features = [detector featuresInImage:image];
NSLog(@"I have found %lu faces", (long unsigned)features.count);
for (CIFaceFeature *faceFeature in features)
{
const CGRect faceRect = CGRectApplyAffineTransform(faceFeature.bounds, transform);
NSLog(@"I have the original frame as: %@", NSStringFromCGRect(faceRect));
const CGFloat scaleWidth = _imagePhotoChosen.frame.size.width/_imagePhotoChosen.image.size.width;
const CGFloat scaleHeight = _imagePhotoChosen.frame.size.height/_imagePhotoChosen.image.size.height;
CGRect faceFrame = CGRectMake(faceRect.origin.x * scaleWidth, faceRect.origin.y * scaleHeight, faceRect.size.width * scaleWidth, faceRect.size.height * scaleHeight);
UIView *faceView = [[UIView alloc] initWithFrame:faceFrame];
NSLog(@"I have the bounds as: %@", NSStringFromCGRect(faceFrame));
faceView.layer.borderColor = [[UIColor redColor] CGColor];
faceView.layer.borderWidth = 1.0f;
[self.view addSubview:faceView];
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
_imagePhotoChosen.image = info[UIImagePickerControllerOriginalImage];
//[_imagePhotoChosen sizeToFit];
[self.view addSubview:_viewChosenPhoto];
[picker dismissViewControllerAnimated:YES completion:nil];
[self detectFaces];
}
我一直在尝试解决数学错误的问题,所以我留在NSLog语句中,但是似乎看不出来是不是!而且我也是数学老师...
再次感谢您为我指明正确方向所做的一切。
响应人们想要知道我如何解决的问题……这对我来说确实是一个愚蠢的错误。
我正在将子视图添加到主窗口,而不是添加到UIImageView
。因此,我删除了这一行:
[self.view addSubview:faceView];
并替换为:
[_imagePhotoChosen addSubview:faceView];
这样就可以将框架放置在正确的位置。公认的解决方案为我提供了线索!因此,更新后的代码(从那时起,我继续前进了一点:
-(void)detectFaces:(UIImage *)selectedImage {
_imagePhotoChosen.image = selectedImage;
CIImage *image = [CIImage imageWithCGImage:selectedImage.CGImage options:nil];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];
CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
transform = CGAffineTransformTranslate(transform, 0, -selectedImage.size.height);
NSArray *features = [detector featuresInImage:image];
int i = 0;
for (CIFaceFeature *faceFeature in features)
{
const CGRect faceRect = CGRectApplyAffineTransform(faceFeature.bounds, transform);
const CGFloat scaleWidth = _imagePhotoChosen.frame.size.width/_imagePhotoChosen.image.size.width;
const CGFloat scaleHeight = _imagePhotoChosen.frame.size.height/_imagePhotoChosen.image.size.height;
CGRect faceFrame = CGRectMake(faceRect.origin.x * scaleWidth, faceRect.origin.y * scaleHeight, faceRect.size.width * scaleWidth, faceRect.size.height * scaleHeight);
UIView *faceView = [[UIView alloc] initWithFrame:faceFrame];
faceView.layer.borderColor = [[UIColor redColor] CGColor];
faceView.layer.borderWidth = 1.0f;
faceView.tag = i;
UITapGestureRecognizer *selectPhotoTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPhoto)];
selectPhotoTap.numberOfTapsRequired = 1;
selectPhotoTap.numberOfTouchesRequired = 1;
[faceView addGestureRecognizer:selectPhotoTap];
[_imagePhotoChosen addSubview:faceView];
i++;
}
}
实际上,您所做的完全正确,只需将这一行替换为
CGRect faceFrame = CGRectMake(_imagePhotoChosen.frame.origin.x+ faceRect.origin.x * scaleWidth,_imagePhotoChosen.frame.origin.y+ faceRect.origin.y * scaleHeight, faceRect.size.width * scaleWidth, faceRect.size.height * scaleHeight);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句