I'm using Firebase ML Kit to recognise text that's visible within a small window. But I want to make it more efficient, helping it to not analyse unnecessary data. So I would like to crop the image being sent to the model.
Firebase ML is taking a VisionImage which takes a CMSampleBuffer.
The only part of the image I would need is the same width, but is only about 100px high, like the blue part here:
I haven't found a good way to do this yet, or is it better to take the image from the preview that is shown to the user and then convert it back?
I'm thinking that this should be done inside the captureOutput function from the AVCaptureVideoDataOutputSampleBufferDelegate. My function looks like this today:
func captureOutput(
_ output: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from connection: AVCaptureConnection
) {
DispatchQueue.main.async {
self.updatePreviewOverlayView()
}
guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
self.lastFrame = sampleBuffer
let visionImage = VisionImage(buffer: sampleBuffer)
let metadata = VisionImageMetadata()
let visionOrientation = VisionDetectorImageOrientation.rightTop
metadata.orientation = visionOrientation
visionImage.metadata = metadata
let imageWidth = CGFloat(CVPixelBufferGetWidth(imageBuffer))
let imageHeight = CGFloat(CVPixelBufferGetHeight(imageBuffer))
self.recognizeTextOnDevice(in: visionImage, width: imageWidth, height: imageHeight)
}