Search code examples
iosswiftavcapturedevice

Set resolution for camera in IOS for AI


I am working on a project where I start the camera and then capture images live to image processing. This project has guided me TensorFlow - Image Classification. One thing I can't figure out despite looking in the documentation and searching on Google is how to set the resolution of the camera.

Is it possible to either get it, or to set it programmatically? Here is the code where I create the camera:

 private func addVideoDeviceInput() -> Bool {
        
        /**Tries to get the default back camera.
         */
        guard let camera  = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else {
            return false
        }
        do {
            let videoDeviceInput = try AVCaptureDeviceInput(device: camera)
            if session.canAddInput(videoDeviceInput) {
                session.addInput(videoDeviceInput)
                return true
            }
            else {
                return false
            }
        }
        catch {
            fatalError("Cannot create video device input")
        }
    }

Solution

  • Yes, check out sessionPreset.

    session.sessionPreset = .photo /// here!
    if session.canAddInput(videoDeviceInput) {
        session.addInput(videoDeviceInput)
        return true
    }