1

I use CIDetector and CIFaceFeature to detect face on the front facing camera. Also trying to place a hat on the head. The hat is placing fine when the head is straight. If I tilt my head the hat goes small and goes away from head.

Code use to add the hat,

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

Detecting the face and movement,

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

Does anyone know how to transform the hat with the head? All helps are appreciated!!

Maniganda saravanan
  • 2,188
  • 1
  • 19
  • 35
  • you can estime the location of the hat by using the mouth's and eyes' positions (that describes a triangle for you which defines the _direction_ of the vector to refine the hat's center point), but you have no information about the forehead's or face's sizes to adjust the hat really accurately, but I think that still does the job. – holex Jan 30 '17 at 11:42
  • Thank you for the reply. Im using front camera, while getting the position of the eye position and mouth is mirrored also coordinate points are not correct. Could you pls give me a suggestion? – Maniganda saravanan Jan 30 '17 at 11:52
  • if you know those are mirrored, make a transform on those coordinates to get the real ones. – holex Jan 30 '17 at 12:06
  • Thanks again. Do you know why face angle providing by apple is not accurate? as I used in my code – Maniganda saravanan Jan 30 '17 at 12:09
  • the face _detection_ is happening on the fly on an 8-10 MP image; probably it was a consideration of using resources of device on balancedway (e.g. battery, memory) and presenting the best working solution. you may find much more precise face recognition algorithms on desktops which could work on static photos (not on the fly running different algorithms on it), and thay may prensent more accurate or detailed information about the faces on the photo. I think your expecation are too high from a real-time feature. – holex Jan 30 '17 at 12:38

0 Answers0