Using VNGenerateForegroundInstanceMaskRequest on Obj-C to remove background of image

204 Views Asked by At

So the new Vision and VisionKit Frameworks on iOS 17 allow for (what seems like) relatively simple removal of background from any image, but every video and guide is only shown on Swift, despite, having it on Obj-C as well. I have tried working this out, but have issues still with it not finding results, or knowing what needs to be set for this to work properly on my Obj-C app.

https://developer.apple.com/documentation/vision/vngenerateforegroundinstancemaskrequest?language=objc

 VNGenerateForegroundInstanceMaskRequest *request = [VNGenerateForegroundInstanceMaskRequest new];

    VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCIImage:image options:@{}];
    @try {
        [handler performRequests:@[request] error:nil];

        VNPixelBufferObservation *result = request.results.firstObject;
        if (!result) {
            NSLog(@"No result from foreground mask request");
            return;
        }

        VNPixelBufferObservation *maskedImage = [result generateMaskedImageOfInstances:result.allInstances fromHandler:handler croppedToInstancesExtent:YES error:nil];

        // Handle the masked image as needed
        // ...

    }
0

There are 0 best solutions below