Listening to autofocus events

82 views
Skip to first unread message

Javier Gerardo Martinez Salomon

unread,
Aug 9, 2023, 12:28:45 PM8/9/23
to Android CameraX Discussion Group
Hi everyone,

I have a question around the behavior of the default CONTINUOUS_PICTURE and CONTINUOUS_VIDEO focus modes, I understand that these are the default focus modes, and that we can still issue One Shot focus operations using CameraControl and FocusMeteringAction,  it would switch to AF_MODE_AUTO, perform the focus, and revert to its original focus mode.

When using that API, I can also handle the ListenableFuture and basically I'm able to tell when an auto focus operation is ongoing and when it ends. But how can I know when an autofocus operation is in progress for the default CONTINUOUS_PICTURE and CONTINUOUS_VIDEO modes when not issuing focus events myself?

My usecase is analyzing images, and I want to avoid analyzing an image that might be slightly out of focus due to an ongoing auto focus operation, I can prevent analyzing images tracking my own auto focus operations, but how do I prevent the analysis of frames for the default focus modes?

Is there a way to hook-up a listener?

Thanks

Eino-Ville Talvala

unread,
Aug 9, 2023, 12:58:28 PM8/9/23
to Javier Gerardo Martinez Salomon, Android CameraX Discussion Group
Hi Javier -

I don't think there's a standard listener for this, but you can use the camera2/interop package to register a capture listener via setSessionCallback. Then in onCaptureCompleted, you can look at the TotalCaptureResult's CONTROL_AF_STATE field to determine if the autofocus system thinks it's in focus at the moment.

- Eddy

--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/574cec89-3442-427c-aecb-d6b7ae7b2532n%40android.com.

Javier Gerardo Martinez Salomon

unread,
Aug 9, 2023, 5:17:10 PM8/9/23
to Android CameraX Discussion Group, etal...@google.com, Android CameraX Discussion Group, Javier Gerardo Martinez Salomon
Hi Eddy,

Thanks, that API did worked while used with the `Analysis` use case, this is how I define if there's an ongoing focus operation, and it seems to align very well with the timings when using manual FocusMeteringAction requests:

val captureCallback = object : CameraCaptureSession.CaptureCallback() {
override fun onCaptureCompleted(
session: CameraCaptureSession,
request: CaptureRequest,
result: TotalCaptureResult
) {
super.onCaptureCompleted(session, request, result)

result.get(CaptureResult.CONTROL_AF_STATE)?.let { afState ->
val isFocusing = when(afState) {
CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED,
CaptureResult.CONTROL_AF_STATE_INACTIVE,
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED,
CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED,
CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED -> false

CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN,
CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN-> true

else -> false
}
}
}
}


Let me know if you have feedback.
Best,

Eino-Ville Talvala

unread,
Aug 9, 2023, 5:22:48 PM8/9/23
to Javier Gerardo Martinez Salomon, Android CameraX Discussion Group
Hi Javier -

That looks reasonable enough to me!  I think it'll handle the various autofocus modes fairly gracefully as well. 

I guess one possible problem is that `isFocusing` just indicates when the lens is moving - you may have poor focus if AF_STATE is `NOT_FOCUSED_LOCKED` or `PASSIVE_UNFOCUSED` or `INACTIVE`.  But in those cases, AF isn't trying to fix the problem, so it may be persistent (until a change in AF mode, AF is unlocked, or AF is triggered to do a scan).  But if you're generally using CameraX's focus metering action, you'll probably only run into poor focus states if the user is holding the device too close to the scene, or it's too dark. So it may be best to try to run analysis anyway, since the scene may never get better.

Good luck with your project!

- Eddy

Javier Gerardo Martinez Salomon

unread,
Aug 9, 2023, 5:39:53 PM8/9/23
to Android CameraX Discussion Group, etal...@google.com, Android CameraX Discussion Group, Javier Gerardo Martinez Salomon
Hey Eddy,

That's good feedback, currently I've got the CONTINUOUS_AUTO_FOCUS doing the focus work, but I'm analyzing the frames as long as isFocusing is false, the analyzer can detect the rest of bad image quality conditions and either provide instructions on how to improve the scene, or issue a FocusMeteringAction if the frame is blurry for some time. My goal is to avoid analyzing frames that might be "slightly" out of focus due to ongoing focus operations and that are barely good quality images to increase the chances of landing a very good quality image, for context, the final image comes from an analysis frame.

Thanks for your time!
Reply all
Reply to author
Forward
0 new messages