mrousavy / react-native-vision-camera

πŸ“Έ A powerful, high-performance React Native Camera library.
https://react-native-vision-camera.com
MIT License
7.21k stars 1.06k forks source link

πŸ’­ Is there a way to get the focal length of the camera? #3093

Closed seadeep42 closed 1 month ago

seadeep42 commented 1 month ago

Question

I'm using the front camera with a frame processor. Is there a way to get the focal length of the image either as a camera parameter or a property of the frame?

What I tried

No response

VisionCamera Version

4.5.0

Additional information

maintenance-hans[bot] commented 1 month ago

Guten Tag, Hans here.

[!NOTE] New features, bugfixes, updates and other improvements are all handled mostly by @mrousavy in his free time. To support @mrousavy, please consider πŸ’– sponsoring him on GitHub πŸ’–. Sponsored issues will be prioritized.

mrousavy commented 1 month ago

No, there is not at the moment.

You can create a feature request for this - or even better - a PR to implement this, if you want :)

mrousavy commented 1 month ago

On iOS we have lensPosition - not sure if this is actually what we want:

A lens position value doesn’t correspond to an exact physical distance, nor does it represent a consistent focus distance from device to device.

It only exposes a value from 0.0 to 1.0, no exact millimeters. So I don't think the underlying iOS camera API supports getting the focal length.

Why do you need focal length anyways?

mrousavy commented 1 month ago

I found this code somewhere in a gist:

func getFocalLengthInMillimeters(captureDevice: AVCaptureDevice) -> Float? {
    guard let format = captureDevice.activeFormat else {
        return nil
    }

    // Get the intrinsic matrix, which provides focal length in pixels
    let dimensions = CMVideoFormatDescriptionGetDimensions(format.formatDescription)
    let sensorWidth = format.sensorDimensions.width
    let sensorHeight = format.sensorDimensions.height
    let intrinsicMatrix = format.formatDescription.intrinsicMatrix

    // Calculate the focal length in millimeters
    let focalLengthInPixels = intrinsicMatrix[0][0] // This is the fx value
    let sensorPhysicalWidth = format.sensorPhysicalSize.width
    let sensorPhysicalHeight = format.sensorPhysicalSize.height
    let focalLengthInMillimeters = (focalLengthInPixels / Float(dimensions.width)) * Float(sensorPhysicalWidth)

    return focalLengthInMillimeters
}

But it looks very hacky and depends on activeFormat, I don't think this can work reliably in getAvailableCameraDevices().

This might only work if we pass it alongside with Frame, but I kinda want to avoid that since the intrinsic matrix and focal length has nothing to do with a Frame, but more with a Camera device - it's contextually in the wrong place.

mrousavy commented 1 month ago

Also maybe related to https://github.com/mrousavy/react-native-vision-camera/issues/3032