1

I'm trying to detect faces on the webcam feed with Vision API. The CPU usage is very high, like 60% or 80%. Is there any way to reduce it?

I've tried receding the frames per second which I pass from the webcam feed. However that didn't help. Here's how I am trying to detect faces. This has to be updated in real time.

try? VNSequenceRequestHandler().perform([VNDetectFaceRectanglesRequest()], on: image)
if let results = VNDetectFaceRectanglesRequest().results as? [VNFaceObservation] {
    DispatchQueue.main.async {
        // update UI
    }
}
Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64

1 Answers1

2

Vision

  • Try defining a regionOfInterest in VNDetectFaceRectanglesRequest
  • Verify that your request "[…] request is free to leverage the GPU to accelerate its processing." with the usesCPUOnly property
  • If your usage permit it try preferBackgroundProcessing

AVFoundation

Also you can choose a lower quality for your capture devices if your are using AVCapture

Like choosing the right settings based on ProcessInfo.processInfo.thermalState :

  • 'AVCaptureSession.sessionPreset'
  • Analysing a max number of face per secondes
  • Disabling HDR if is not need for your AVCaptureDevice

Other

  • Reduce the number of update of your CVPixelBuffer
    • Allocating a fixed size of it
    • If you display it for the user, only use the displayed part
  • If you're doing some post image processing consider using Accelerate

Avoid as much work on the main thread

Sombre Osmo'z
  • 165
  • 1
  • 8