google-ar / arcore-ios-sdk

ARCore SDK for iOS
https://developers.google.com/ar/
Apache License 2.0
283 stars 84 forks source link

The mask is unstable when using the rear camera #96

Closed petrovichppp closed 3 months ago

petrovichppp commented 8 months ago

Hello.

I opened demo and switched camera from front to back. If I make small movements then the mask disappears or twitches. Is there any way to fix this? Maybe I need to configure the camera in some other way?

Thanks

kinjalbhavsar commented 8 months ago

Hi Pavel, which sample app are you using when you observe this?

petrovichppp commented 8 months ago

Hi Kinjal Bhavsar.

This sample app https://github.com/google-ar/arcore-ios-sdk/tree/master/Examples/AugmentedFacesExample

I changed 145 line in FacesViewController.swift

from AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front) to AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)

15kingben commented 8 months ago

You may want to try changing the rotation of the image to ensure the image is facing up: https://github.com/google-ar/arcore-ios-sdk/blob/master/Examples/AugmentedFacesExample/AugmentedFacesExample/FacesViewController.swift#L288

The rear camera may be 180 degrees off the front one

petrovichppp commented 3 months ago

@15kingben thank you for your answer! I wrote the following code and the rear camera started working better! Sometimes the mask flickers while the camera is rotating, maybe there is something else to fix it.

In general I am satisfied with the result. If you can give feedback on the code, I would appreciate it.

    private enum Constants {
        static let maxDegrees: Double = 360
        static let halfDegrees: Double = 180
    }

    private func getLandscapeLeftRotationDegrees(deviceMotion: CMDeviceMotion) -> UInt  {
        // Use the device's gravity vector to determine which direction is up for a face. This is the
        // positive counter-clockwise rotation of the device relative to landscape left orientation.
        let rotation = .two * .pi - atan2(deviceMotion.gravity.x, deviceMotion.gravity.y) + .pi / .two
        let rotationDegrees = (UInt)(rotation * Constants.halfDegrees / .pi) % UInt(Constants.maxDegrees)

        return rotationDegrees
    }

    private func getLandscapeRightRotationDegrees(deviceMotion: CMDeviceMotion) -> UInt  {
        let rotation = atan2(deviceMotion.gravity.x, deviceMotion.gravity.y) + .pi / .two
        let rotationDegrees = rotation * Constants.halfDegrees / .pi

        let normalizedRotationDegrees = rotationDegrees < .zero 
            ? rotationDegrees + Constants.maxDegrees
            : rotationDegrees

        let resultRotationDegrees = UInt(normalizedRotationDegrees) % UInt(Constants.maxDegrees)

        return resultRotationDegrees
    }