1

我使用 CIDetector 和 CIFaceFeature 来检测前置摄像头上的人脸。还试图在头上戴一顶帽子。头部笔直时,帽子放置得很好。如果我倾斜我的头,帽子会变小并远离头部。

代码用于添加帽子,

self.hatImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:selectedImageName]];
self.hatImgView.contentMode = UIViewContentModeScaleAspectFit;
[self.previewView addSubview:self.hatImgView];

检测面部和运动,

- (void)detectedFaceController:(DetectFace *)controller features:(NSArray *)featuresArray forVideoBox:(CGRect)clap withPreviewBox:(CGRect)previewBox
{
for (CIFaceFeature *ff in featuresArray) {
    CGRect faceRect = [ff bounds];

    //isMirrored because we are using front camera
    faceRect = [DetectFace convertFrame:faceRect previewBox:previewBox forVideoBox:clap isMirrored:YES];

    float hat_width = self.hatImgView.image.size.width;
    float hat_height = self.hatImgView.image.size.height;
    int head_start_y = 330.0; //part of hat image is transparent
    int head_start_x = 60.0;

    float width = faceRect.size.width * (hat_width / (hat_width - head_start_x));
    float height = width * hat_height/hat_width;
    int y = faceRect.origin.y - (height * head_start_y) / hat_height;
    int x = faceRect.origin.x - (head_start_x * width/hat_width);
    [UIView animateWithDuration:0.3 animations:^{
            [self.hatImgView setTransform:CGAffineTransformRotate(CGAffineTransformIdentity, (DegreesToRadians(-ff.faceAngle)))];
            self.hatImgView.frame =  CGRectMake(x, y, width + 60.0f, height + 60.0f);
     }];
} 
}

在此处输入图像描述 在此处输入图像描述

有谁知道如何用头变换帽子?感谢所有帮助!

4

0 回答 0