feat: refine AI focus detection and fallback logic
This commit is contained in:
1 parent
c9588cc167
commit
d1baf00964
4 files changed
+61
-13
No files matched your search
@@ -93,6 +93,7 @@ class Camera2Controller(private val context: Context) {
|
||||
private const val FOCUS_LOCK_SETTLE_FRAMES = 5 // 对焦锁定后等待镜头稳定的帧数
|
||||
private const val SCENE_CHANGE_CONFIRM_FRAMES = 3 // 连续 N 帧检测到变化才确认
|
||||
private const val AI_SUBJECT_RECENT_MS = 1800L
|
||||
private const val AI_FOCUS_FALLBACK_FRAMES = 6
|
||||
}
|
||||
|
||||
private val cameraManager: CameraManager by lazy {
|
||||
@@ -212,6 +213,7 @@ class Camera2Controller(private val context: Context) {
|
||||
private var focusLockedReferenceDistance: Float = 0f
|
||||
private var focusLockSettleFrames = 0 // 对焦锁定后等待镜头稳定的帧数
|
||||
private var sceneChangeFrameCount = 0
|
||||
private var aiFocusFallbackFrames = 0
|
||||
private var aiSubjectLastSeenElapsedMs: Long = 0L
|
||||
private var aiSubjectLastSeenX: Float = -1f
|
||||
private var aiSubjectLastSeenY: Float = -1f
|
||||
@@ -339,16 +341,29 @@ class Camera2Controller(private val context: Context) {
|
||||
if (_state.value.isFocusing) {
|
||||
when (afState) {
|
||||
CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED -> {
|
||||
aiFocusFallbackFrames = 0
|
||||
_state.value = _state.value.copy(isFocusing = false, focusSuccess = true)
|
||||
// 只在首次锁定时记录一次,后续 AF 狩猎重新锁定不再覆盖
|
||||
if (!isFocusLockedWaitingForSceneChange) recordFocusLockExposure(result)
|
||||
}
|
||||
|
||||
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED -> {
|
||||
aiFocusFallbackFrames = 0
|
||||
_state.value = _state.value.copy(isFocusing = false, focusSuccess = false)
|
||||
if (!isFocusLockedWaitingForSceneChange) recordFocusLockExposure(result)
|
||||
}
|
||||
}
|
||||
if (_state.value.isFocusing &&
|
||||
_state.value.focusPointSource == FocusPointSource.AI &&
|
||||
aiFocusFallbackFrames > 0
|
||||
) {
|
||||
aiFocusFallbackFrames--
|
||||
if (aiFocusFallbackFrames == 0) {
|
||||
PLog.d(TAG, "AI focus fallback complete")
|
||||
_state.value = _state.value.copy(isFocusing = false, focusSuccess = true)
|
||||
if (!isFocusLockedWaitingForSceneChange) recordFocusLockExposure(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 场景变化检测:对焦锁定后持续监测曝光变化和焦距跳变
|
||||
@@ -669,7 +684,7 @@ class Camera2Controller(private val context: Context) {
|
||||
fun openCamera(surfaceTexture: SurfaceTexture, preserveVideoRecording: Boolean = false) {
|
||||
// 先关闭旧的相机和资源,防止资源泄漏
|
||||
closeCamera(preserveVideoRecording = preserveVideoRecording)
|
||||
previewAiFocusProcessor.resetForPreviewRestart()
|
||||
resetAiFocusForCameraOpen()
|
||||
val openGeneration = ++cameraOpenGeneration
|
||||
|
||||
// 确保在权限已授予后才发现相机(延迟初始化)
|
||||
@@ -2743,6 +2758,7 @@ class Camera2Controller(private val context: Context) {
|
||||
focusLockedReferenceExposureNs = 0L
|
||||
focusLockedReferenceDistance = 0f
|
||||
focusLockSettleFrames = 0
|
||||
aiFocusFallbackFrames = 0
|
||||
|
||||
previewRequestBuilder?.apply {
|
||||
set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE)
|
||||
@@ -2760,6 +2776,27 @@ class Camera2Controller(private val context: Context) {
|
||||
restoreContinuousAf()
|
||||
}
|
||||
|
||||
private fun resetAiFocusForCameraOpen() {
|
||||
PLog.d(TAG, "Reset AI focus state for camera open")
|
||||
previewAiFocusProcessor.resetForPreviewRestart()
|
||||
aiSubjectLastSeenElapsedMs = 0L
|
||||
aiSubjectLastSeenX = 0.5f
|
||||
aiSubjectLastSeenY = 0.5f
|
||||
isFocusLockedWaitingForSceneChange = false
|
||||
sceneChangeFrameCount = 0
|
||||
focusLockedReferenceIso = 0
|
||||
focusLockedReferenceExposureNs = 0L
|
||||
focusLockedReferenceDistance = 0f
|
||||
focusLockSettleFrames = 0
|
||||
aiFocusFallbackFrames = 0
|
||||
_state.value = _state.value.copy(
|
||||
focusPoint = null,
|
||||
focusPointSource = FocusPointSource.MANUAL,
|
||||
isFocusing = false,
|
||||
focusSuccess = null
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 点击对焦
|
||||
*/
|
||||
@@ -2797,6 +2834,7 @@ class Camera2Controller(private val context: Context) {
|
||||
isFocusing = true,
|
||||
focusSuccess = null
|
||||
)
|
||||
aiFocusFallbackFrames = if (source == FocusPointSource.AI) AI_FOCUS_FALLBACK_FRAMES else 0
|
||||
|
||||
// 根据传感器方向转换坐标
|
||||
// 传感器坐标系与UI坐标系可能不同,需要旋转
|
||||
@@ -3524,7 +3562,6 @@ class Camera2Controller(private val context: Context) {
|
||||
fun closeCamera(preserveVideoRecording: Boolean = false) {
|
||||
try {
|
||||
cameraOpenGeneration++
|
||||
previewAiFocusProcessor.resetForPreviewRestart()
|
||||
val keepVideoRecording = preserveVideoRecording && _state.value.videoRecordingState.isRecording
|
||||
if (keepVideoRecording) {
|
||||
PLog.d(TAG, "Closing camera while keeping active video recording")
|
||||
|
||||
@@ -2414,7 +2414,7 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
private fun runAiFocusInputCaptureInternal(sourceTextureId: Int) {
|
||||
if (onAiFocusInputAvailable == null || sourceTextureId == 0) return
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - lastRunAiFocusInputTime < 60) return
|
||||
if (now - lastRunAiFocusInputTime < 200) return
|
||||
lastRunAiFocusInputTime = now
|
||||
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, aiFocusInputFboId)
|
||||
|
||||
@@ -26,7 +26,8 @@ class PreviewAiFocusProcessor(private val context: Context) {
|
||||
private const val PERSON_PRIORITY = 3
|
||||
private const val FACE_REFINEMENT_PRIORITY = 4
|
||||
private const val FACE_REFINEMENT_SMOOTHING = 0.65f
|
||||
private const val FACE_HOLD_MS = 700L
|
||||
private const val FACE_HOLD_MS = 250L
|
||||
private const val FACE_WITH_PERSON_SCORE_THRESHOLD = 0.2f
|
||||
private const val PROCESSING_STALE_MS = 2_000L
|
||||
}
|
||||
|
||||
@@ -70,6 +71,8 @@ class PreviewAiFocusProcessor(private val context: Context) {
|
||||
private var lastRefinedFaceLabel = ""
|
||||
private var heldFaceCandidate: FocusCandidate? = null
|
||||
private var heldFaceTimeMs = 0L
|
||||
private var lastFaceDetectTimeMs = 0L
|
||||
private var lastFaceDetectorUseTimeMs = 0L
|
||||
var onFocusTarget: ((FocusTarget) -> Unit)? = null
|
||||
var onTargetSeen: ((FocusTarget) -> Unit)? = null
|
||||
var onTargetLost: (() -> Unit)? = null
|
||||
@@ -81,9 +84,6 @@ class PreviewAiFocusProcessor(private val context: Context) {
|
||||
try {
|
||||
StartupTrace.mark("PreviewAiFocusProcessor.prewarm start")
|
||||
SharedYoloXObjectDetector.prewarm(context)
|
||||
if (shouldDetectFaceFocus()) {
|
||||
SharedFaceDetLiteFocusDetector.prewarm(context)
|
||||
}
|
||||
isPrewarmed = true
|
||||
StartupTrace.mark("PreviewAiFocusProcessor.prewarm end")
|
||||
} catch (e: Exception) {
|
||||
@@ -158,7 +158,6 @@ class PreviewAiFocusProcessor(private val context: Context) {
|
||||
}
|
||||
|
||||
fun release() {
|
||||
resetForPreviewRestart()
|
||||
scope.launch {
|
||||
SharedYoloXObjectDetector.release()
|
||||
SharedFaceDetLiteFocusDetector.release()
|
||||
@@ -175,6 +174,8 @@ class PreviewAiFocusProcessor(private val context: Context) {
|
||||
lastFocusX = -1f
|
||||
lastFocusY = -1f
|
||||
lastFocusTimeMs = 0L
|
||||
lastFaceDetectTimeMs = 0L
|
||||
lastFaceDetectorUseTimeMs = 0L
|
||||
resetFaceRefinementSmoothing()
|
||||
}
|
||||
|
||||
@@ -182,10 +183,20 @@ class PreviewAiFocusProcessor(private val context: Context) {
|
||||
bitmap: Bitmap,
|
||||
detections: List<YoloXObjectDetector.Detection>,
|
||||
): List<FocusCandidate> {
|
||||
val candidates = detections.map { it.toFocusCandidate() }.toMutableList()
|
||||
val personOnlyUsesFace = shouldDetectFaceFocus()
|
||||
val candidates = detections
|
||||
.filterNot { personOnlyUsesFace && it.priority == PERSON_PRIORITY }
|
||||
.map { it.toFocusCandidate() }
|
||||
.toMutableList()
|
||||
val personDetections = detections.filter { it.priority == PERSON_PRIORITY }
|
||||
if (shouldDetectFaceFocus() && personDetections.isNotEmpty()) {
|
||||
val faceFocus = SharedFaceDetLiteFocusDetector.detect(context, bitmap, scoreThreshold)
|
||||
if (personOnlyUsesFace && personDetections.isNotEmpty()) {
|
||||
lastFaceDetectTimeMs = System.currentTimeMillis()
|
||||
lastFaceDetectorUseTimeMs = lastFaceDetectTimeMs
|
||||
val faceFocus = SharedFaceDetLiteFocusDetector.detect(
|
||||
context = context,
|
||||
bitmap = bitmap,
|
||||
minScore = FACE_WITH_PERSON_SCORE_THRESHOLD,
|
||||
)
|
||||
if (faceFocus != null && faceFocus.isAttachedToPerson(personDetections)) {
|
||||
val faceCandidate = faceFocus.toFocusCandidate().smoothedFaceRefinement()
|
||||
rememberHeldFaceCandidate(faceCandidate)
|
||||
@@ -199,7 +210,8 @@ class PreviewAiFocusProcessor(private val context: Context) {
|
||||
PLog.d(TAG, "AI face focus missing: persons=${personDetections.size} threshold=$scoreThreshold")
|
||||
}*/
|
||||
if (faceFocus == null || !faceFocus.isAttachedToPerson(personDetections)) {
|
||||
getHeldFaceCandidate(personDetections)?.let { candidates += it }
|
||||
val heldFace = getHeldFaceCandidate(personDetections)
|
||||
heldFace?.let { candidates += it }
|
||||
}
|
||||
} else {
|
||||
clearHeldFaceCandidate()
|
||||
|
||||
@@ -1469,7 +1469,6 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
val shouldDisableVideoLog = mode == CaptureMode.PHOTO &&
|
||||
state.value.videoConfig.logProfile != VideoLogProfile.OFF
|
||||
cameraController.setCaptureMode(mode)
|
||||
cameraController.previewAiFocusProcessor.resetForPreviewRestart()
|
||||
currentSurfaceTexture = null
|
||||
cameraController.closeCamera()
|
||||
viewModelScope.launch {
|
||||
|
||||
Reference in new issue
Block a user