Monday, September 28, 2026
HomeiOS Developmentios - VNFaceObservation crop face in portrait mode

ios – VNFaceObservation crop face in portrait mode

[ad_1]

I’ve a photograph and that i run VNDetectFaceRectanglesRequest to detect faces on the picture(the mannequin runs on a resized photograph):

    UIImage * imageToInspect = ....;
    NSData * fullSizeImageData = ...;
    
    VNDetectFaceRectanglesRequest * request = [VNDetectFaceRectanglesRequest new];
    VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCGImage:imageToInspect.CGImage choices:@{}];
    BOOL success = [handler performRequests:imageRequests error:&imageRequestError];

Then I need to crop the faces from the unique photograph
UIImage * fullSizeImage = [[UIImage alloc] initWithData:fullSizeImageData];
CGFloat heightInPoints = fullSizeImage.dimension.peak;
CGFloat heightInPixels = heightInPoints * fullSizeImage.scale;

CGFloat widthInPoints = fullSizeImage.dimension.width;
CGFloat widthInPixels = widthInPoints * fullSizeImage.scale;
    
for (int index = 0; index < facesFeatures.depend ; index++) {
    VNFaceObservation * face = facesFeatures[index];
    NSString * faceNumber    = [[NSNumber numberWithInt:index] stringValue];
    
    CGRect fullRect = VNImageRectForNormalizedRect(face.boundingBox, widthInPixels, heightInPixels);

    CGFloat squareSize = (fullRect.dimension.width > fullRect.dimension.peak) ? fullRect.dimension.width : fullRect.dimension.peak;
                
    UIImage * fullSizeFace = [fullSizeImage crop:CGRectMake(fullRect.origin.x, heightInPixels - fullRect.origin.y - fullRect.size.height, squareSize, squareSize)];    
    
    // Create path.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSTimeInterval nowEpochSeconds = [[NSDate date] timeIntervalSince1970];
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"Image-%f-%d.png",nowEpochSeconds,index]];

    // Save picture.
    [UIImagePNGRepresentation(fullSizeFace) writeToFile:filePath atomically:YES];
}

That is the crop technique:

- (UIImage *)crop:(CGRect)rect {
    if (self.scale > 1.0f) {
        rect = CGRectMake(rect.origin.x * self.scale,
                          rect.origin.y * self.scale,
                          rect.dimension.width * self.scale,
                          rect.dimension.peak * self.scale);
    }

    CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    UIImage *consequence = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(imageRef);
    return consequence;
}

Then once I examine the output picture, when the unique photograph is a panorama I can see all of the faces, however when the unique photograph is in portrait mode, the crop not working and I do not see any face within the output picture.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments