feat: refine role expression and diagnose camera context in 0.5.2
This commit is contained in:
+2
-2
@@ -45,8 +45,8 @@ android {
|
||||
applicationId 'com.gorios.lanvoice'
|
||||
minSdk 26
|
||||
targetSdk 34
|
||||
versionCode 9
|
||||
versionName '0.5.1'
|
||||
versionCode 10
|
||||
versionName '0.5.2'
|
||||
ndk { abiFilters 'arm64-v8a' }
|
||||
}
|
||||
compileOptions { sourceCompatibility JavaVersion.VERSION_17; targetCompatibility JavaVersion.VERSION_17 }
|
||||
|
||||
@@ -124,6 +124,8 @@ class MainActivity : ComponentActivity() {
|
||||
cameraStatus = expressionState.status,
|
||||
cameraPermissionPending = cameraGate.permissionPending,
|
||||
faceSharing = shareFace,
|
||||
cameraAnalysis = expressions.diagnosticsSnapshot(),
|
||||
faceTransmission = session.faceDiagnosticsSnapshot(),
|
||||
rolePlaying = state.phase == VoicePhase.ANSWERING,
|
||||
role = state.replyEmotion.takeIf { it.turnId != null },
|
||||
previousRole = state.previousReplyEmotion,
|
||||
|
||||
@@ -14,6 +14,9 @@ import com.gorios.lanvoice.net.EventKind
|
||||
import com.gorios.lanvoice.net.ServerEvent
|
||||
import com.gorios.lanvoice.expression.FaceCue
|
||||
import com.gorios.lanvoice.expression.FaceCueObservation
|
||||
import com.gorios.lanvoice.expression.FaceCueReason
|
||||
import com.gorios.lanvoice.expression.FaceSendReason
|
||||
import com.gorios.lanvoice.expression.FaceTransmissionDiagnostics
|
||||
|
||||
enum class VoicePhase { IDLE, LISTENING, RECOGNIZING, THINKING, ANSWERING }
|
||||
|
||||
@@ -59,15 +62,11 @@ class VoiceSession(
|
||||
private var faceSupported = false
|
||||
private var faceCue = FaceCueObservation()
|
||||
private var faceSent = false
|
||||
private val faceDiagnostics = FaceTransmissionDiagnostics()
|
||||
private val faceTick = object : Runnable {
|
||||
override fun run() {
|
||||
if (!faceSharing || released) return
|
||||
if (isActive() && faceSupported && hasTurn) {
|
||||
val age = SystemClock.elapsedRealtime() - faceCue.observedAtMs
|
||||
if (dialogue.sendFaceCue(currentTurn, faceCue, age)) {
|
||||
faceSent = faceCue.cue != FaceCue.UNKNOWN && age in 0..3_000
|
||||
}
|
||||
}
|
||||
sendFaceContext()
|
||||
main.postDelayed(this, 1_000L)
|
||||
}
|
||||
}
|
||||
@@ -215,6 +214,8 @@ class VoiceSession(
|
||||
main.removeCallbacks(faceTick)
|
||||
if (!enabled) clearFaceContext()
|
||||
faceSharing = enabled
|
||||
diagnostic("face sharing enabled=$enabled")
|
||||
if (!enabled) faceDiagnostics.record(FaceSendReason.SHARING_OFF)
|
||||
if (enabled && !released) main.post(faceTick)
|
||||
}
|
||||
|
||||
@@ -224,12 +225,42 @@ class VoiceSession(
|
||||
faceCue = observation
|
||||
}
|
||||
|
||||
private fun clearFaceContext() {
|
||||
private fun clearFaceContext(clearObservation: Boolean = true) {
|
||||
if (faceSent && faceSupported && state.connection == ConnectionStatus.CONNECTED && hasTurn) {
|
||||
dialogue.sendFaceCue(currentTurn, FaceCueObservation(), 0)
|
||||
val sent = dialogue.sendFaceCue(currentTurn, FaceCueObservation(), 0)
|
||||
faceDiagnostics.record(if (sent) FaceSendReason.CLEARED else FaceSendReason.CLEAR_FAILED)
|
||||
}
|
||||
faceSent = false
|
||||
faceCue = FaceCueObservation()
|
||||
if (clearObservation) faceCue = FaceCueObservation()
|
||||
}
|
||||
|
||||
fun faceDiagnosticsSnapshot() = faceDiagnostics.snapshot()
|
||||
|
||||
private fun sendFaceContext() {
|
||||
val now = SystemClock.elapsedRealtime()
|
||||
val age = now - faceCue.observedAtMs
|
||||
val reason = when {
|
||||
!faceSharing -> FaceSendReason.SHARING_OFF
|
||||
!foreground -> FaceSendReason.BACKGROUND
|
||||
state.connection != ConnectionStatus.CONNECTED -> FaceSendReason.NOT_CONNECTED
|
||||
!state.voiceActive -> FaceSendReason.VOICE_INACTIVE
|
||||
!faceSupported -> FaceSendReason.UNSUPPORTED
|
||||
!hasTurn -> FaceSendReason.NO_TURN
|
||||
faceCue.cue == FaceCue.UNKNOWN -> if (faceCue.reason == FaceCueReason.NO_FACE) FaceSendReason.NO_FACE else FaceSendReason.NO_STABLE_CUE
|
||||
age !in 0..3_000 -> FaceSendReason.STALE
|
||||
!faceCue.intensity.isFinite() || faceCue.intensity !in .5f..1f || faceCue.durationMs !in 800..10_000 -> FaceSendReason.INVALID_CUE
|
||||
else -> null
|
||||
}
|
||||
if (reason != null) {
|
||||
// Send a one-shot clear for a previously published observation; no repeated unknown controls.
|
||||
if (reason in setOf(FaceSendReason.STALE, FaceSendReason.INVALID_CUE, FaceSendReason.NO_FACE, FaceSendReason.NO_STABLE_CUE)) clearFaceContext(clearObservation = false)
|
||||
faceDiagnostics.record(reason)
|
||||
} else {
|
||||
val sent = dialogue.sendFaceCue(currentTurn, faceCue, age)
|
||||
if (sent) faceSent = true
|
||||
faceDiagnostics.record(if (sent) FaceSendReason.SENT else FaceSendReason.SEND_FAILED, faceCue.cue)
|
||||
}
|
||||
faceDiagnostics.summary(now)?.let { diagnostic(it) }
|
||||
}
|
||||
|
||||
private fun onConnection(connection: ConnectionStatus, reason: String) {
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.gorios.lanvoice.diag
|
||||
|
||||
import com.gorios.lanvoice.dialogue.ReplyEmotion
|
||||
import com.gorios.lanvoice.expression.ExpressionStatus
|
||||
import com.gorios.lanvoice.expression.CameraAnalysisSnapshot
|
||||
import com.gorios.lanvoice.expression.FaceTransmissionSnapshot
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.ConnectionPool
|
||||
@@ -35,6 +37,8 @@ data class DiagnosticReportInfo(
|
||||
val rolePlaying: Boolean = false,
|
||||
val role: ReplyEmotion? = null,
|
||||
val previousRole: ReplyEmotion? = null,
|
||||
val cameraAnalysis: CameraAnalysisSnapshot = CameraAnalysisSnapshot(),
|
||||
val faceTransmission: FaceTransmissionSnapshot = FaceTransmissionSnapshot(),
|
||||
)
|
||||
|
||||
/** Explicit user submission, isolated from both voice traffic and per-entry debug uploads. */
|
||||
@@ -94,6 +98,15 @@ object DiagnosticReportUpload {
|
||||
.put("role_playing", info.rolePlaying)
|
||||
.put("role_active", roleMetadata(info.role))
|
||||
.put("role_previous", roleMetadata(info.previousRole))
|
||||
.put("camera_analysis", JSONObject().put("received", info.cameraAnalysis.received)
|
||||
.put("processed", info.cameraAnalysis.processed).put("failures", info.cameraAnalysis.failures)
|
||||
.put("faces", info.cameraAnalysis.faceCount).put("cue", info.cameraAnalysis.cue.wire)
|
||||
.put("reason", info.cameraAnalysis.reason.name.lowercase()).put("failure", info.cameraAnalysis.failure.name.lowercase())
|
||||
.put("last_process_ms", info.cameraAnalysis.lastProcessMs))
|
||||
.put("face_transmission", JSONObject().put("attempts", info.faceTransmission.attempts)
|
||||
.put("sent", info.faceTransmission.sent).put("clears", info.faceTransmission.clears)
|
||||
.put("failures", info.faceTransmission.failures).put("reason", info.faceTransmission.reason.name.lowercase())
|
||||
.put("cue", info.faceTransmission.cue.wire))
|
||||
.put("logs", logs)
|
||||
.toString().toRequestBody(json)
|
||||
client.newCall(Request.Builder().url(ENDPOINT).post(body).build()).enqueue(object : Callback {
|
||||
|
||||
@@ -37,6 +37,7 @@ class ExpressionController(
|
||||
}
|
||||
val rules = ExpressionRules()
|
||||
val faceRules = FaceCueRules()
|
||||
val diagnostics = CameraAnalysisDiagnostics()
|
||||
var landmarker: FaceLandmarker? = null // Accessed only on this run's executor.
|
||||
var analysis: ImageAnalysis? = null // Accessed only on main.
|
||||
var provider: ProcessCameraProvider? = null // Accessed only on main.
|
||||
@@ -49,6 +50,16 @@ class ExpressionController(
|
||||
private val main = Handler(Looper.getMainLooper())
|
||||
private var current: Run? = null
|
||||
private var released = false
|
||||
private var lastDiagnostics = CameraAnalysisDiagnostics()
|
||||
private val diagnosticTick = object : Runnable {
|
||||
override fun run() {
|
||||
val run = current ?: return
|
||||
run.diagnostics.summary(SystemClock.elapsedRealtime())?.let { DebugLogUpload.log("audio", it) }
|
||||
main.postDelayed(this, 10_000)
|
||||
}
|
||||
}
|
||||
|
||||
fun diagnosticsSnapshot(): CameraAnalysisSnapshot = lastDiagnostics.snapshot()
|
||||
|
||||
/** Called only after an explicit user toggle and CAMERA permission approval. */
|
||||
fun start() {
|
||||
@@ -60,6 +71,8 @@ class ExpressionController(
|
||||
}
|
||||
val run = Run()
|
||||
current = run
|
||||
lastDiagnostics = run.diagnostics
|
||||
main.post(diagnosticTick)
|
||||
onState(ExpressionUiState(ExpressionStatus.STARTING))
|
||||
// Model loading and all synchronous VIDEO inference stay off the UI/audio threads.
|
||||
run.executor.execute {
|
||||
@@ -148,6 +161,7 @@ class ExpressionController(
|
||||
var bitmap: Bitmap? = null
|
||||
try {
|
||||
if (!run.active) return
|
||||
run.diagnostics.received()
|
||||
val now = SystemClock.elapsedRealtime()
|
||||
if (run.lastSampleMs != Long.MIN_VALUE && now - run.lastSampleMs < SAMPLE_INTERVAL_MS) return
|
||||
run.lastSampleMs = now
|
||||
@@ -158,7 +172,10 @@ class ExpressionController(
|
||||
run.landmarker?.detectForVideo(mpImage, now)
|
||||
} finally {
|
||||
mpImage.close()
|
||||
} ?: return
|
||||
} ?: kotlin.run {
|
||||
run.diagnostics.failed(CameraFailure.NO_RESULT)
|
||||
return
|
||||
}
|
||||
if (!run.active) return
|
||||
val observation = if (result.faceLandmarks().isEmpty()) null else {
|
||||
val categories = result.faceBlendshapes().orElse(emptyList()).firstOrNull().orEmpty()
|
||||
@@ -176,6 +193,7 @@ class ExpressionController(
|
||||
(landmarks.maxOf { it.x() } - landmarks.minOf { it.x() }) *
|
||||
(landmarks.maxOf { it.y() } - landmarks.minOf { it.y() }) >= .06f
|
||||
val faceCue = run.faceRules.evaluate(observation, result.faceLandmarks().size, quality, now)
|
||||
run.diagnostics.processed(result.faceLandmarks().size, faceCue, SystemClock.elapsedRealtime() - now)
|
||||
main.post { if (isCurrent(run)) onFaceCue(faceCue) }
|
||||
// UI receives only <=2 enum labels; 52 coefficients, pose and images stay transient.
|
||||
if (run.lastPublished != visible || run.lastStableCue != faceCue.cue) {
|
||||
@@ -197,6 +215,7 @@ class ExpressionController(
|
||||
}
|
||||
|
||||
private fun fail(run: Run, guidance: String, reason: String) {
|
||||
run.diagnostics.failed(CameraFailure.entries.firstOrNull { it.name.lowercase() == reason } ?: CameraFailure.ANALYSIS)
|
||||
// Prevent another scheduled frame from running before main unbinds the camera.
|
||||
run.active = false
|
||||
main.post {
|
||||
@@ -212,6 +231,7 @@ class ExpressionController(
|
||||
private fun stopCurrent() {
|
||||
val run = current ?: return
|
||||
current = null // Invalidate every pending model/provider/result callback before unbinding.
|
||||
main.removeCallbacks(diagnosticTick)
|
||||
run.active = false
|
||||
try {
|
||||
run.analysis?.clearAnalyzer()
|
||||
@@ -232,6 +252,7 @@ class ExpressionController(
|
||||
run.rules.reset()
|
||||
run.faceRules.reset()
|
||||
run.lastPublished = null
|
||||
run.diagnostics.summary(SystemClock.elapsedRealtime(), force = true)?.let { DebugLogUpload.log("audio", it) }
|
||||
}
|
||||
}
|
||||
run.executor.shutdown()
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.gorios.lanvoice.expression
|
||||
|
||||
enum class FaceCue(val wire: String) { UNKNOWN("unknown"), SMILING("smiling"), BROW_FURROWED("brow_furrowed") }
|
||||
enum class FaceCueReason {
|
||||
NO_FRAME, NO_FACE, MULTIPLE_FACES, POOR_QUALITY, MISSING_COEFFICIENTS,
|
||||
INVALID_COEFFICIENTS, CONFLICT, NO_CLEAR_CUE, STABILIZING, STABLE,
|
||||
}
|
||||
|
||||
/** Rule intensity is an uncalibrated coefficient, never a probability or emotional state. */
|
||||
data class FaceCueObservation(
|
||||
@@ -8,6 +12,7 @@ data class FaceCueObservation(
|
||||
val intensity: Float = 0f,
|
||||
val durationMs: Long = 0,
|
||||
val observedAtMs: Long = 0,
|
||||
val reason: FaceCueReason = FaceCueReason.NO_FRAME,
|
||||
)
|
||||
|
||||
class FaceCueRules {
|
||||
@@ -22,24 +27,26 @@ class FaceCueRules {
|
||||
val last = previous
|
||||
if (last != null && (nowMs <= last || nowMs - last > 500)) reset()
|
||||
previous = nowMs
|
||||
fun unknown(): FaceCueObservation {
|
||||
fun unknown(reason: FaceCueReason): FaceCueObservation {
|
||||
candidate = FaceCue.UNKNOWN; active = false; since = nowMs
|
||||
return FaceCueObservation(observedAtMs = nowMs)
|
||||
return FaceCueObservation(observedAtMs = nowMs, reason = reason)
|
||||
}
|
||||
if (faceCount != 1 || !quality || observation == null) return unknown()
|
||||
if (faceCount == 0 || observation == null) return unknown(FaceCueReason.NO_FACE)
|
||||
if (faceCount != 1) return unknown(FaceCueReason.MULTIPLE_FACES)
|
||||
if (!quality) return unknown(FaceCueReason.POOR_QUALITY)
|
||||
val names = listOf("mouthSmileLeft", "mouthSmileRight", "browDownLeft", "browDownRight")
|
||||
val scores = names.map { observation.blendshapes[it] ?: return unknown() }
|
||||
if (scores.any { !it.isFinite() || it !in 0f..1f }) return unknown()
|
||||
val scores = names.map { observation.blendshapes[it] ?: return unknown(FaceCueReason.MISSING_COEFFICIENTS) }
|
||||
if (scores.any { !it.isFinite() || it !in 0f..1f }) return unknown(FaceCueReason.INVALID_COEFFICIENTS)
|
||||
val smile = minOf(scores[0], scores[1])
|
||||
val brow = minOf(scores[2], scores[3])
|
||||
val smiling = smile >= if (active && candidate == FaceCue.SMILING) .60f else .75f
|
||||
val furrowed = brow >= if (active && candidate == FaceCue.BROW_FURROWED) .50f else .65f
|
||||
if (smiling == furrowed) return unknown() // Conflict or no clear cue.
|
||||
if (smiling == furrowed) return unknown(if (smiling) FaceCueReason.CONFLICT else FaceCueReason.NO_CLEAR_CUE)
|
||||
val next = if (smiling) FaceCue.SMILING else FaceCue.BROW_FURROWED
|
||||
if (candidate != next) { candidate = next; since = nowMs; active = false }
|
||||
val duration = (nowMs - since).coerceIn(0, 10_000)
|
||||
if (duration < 800) return FaceCueObservation(observedAtMs = nowMs)
|
||||
if (duration < 800) return FaceCueObservation(observedAtMs = nowMs, reason = FaceCueReason.STABILIZING)
|
||||
active = true
|
||||
return FaceCueObservation(next, if (smiling) smile else brow, duration, nowMs)
|
||||
return FaceCueObservation(next, if (smiling) smile else brow, duration, nowMs, FaceCueReason.STABLE)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.gorios.lanvoice.expression
|
||||
|
||||
/** Fixed fields only. No image, coefficient, pose, free text, or user speech. */
|
||||
enum class CameraFailure { NONE, MODEL_LOAD, DEVICE_SUPPORT, NO_FRONT_CAMERA, CAMERA_BIND, ANALYSIS, NO_RESULT }
|
||||
data class CameraAnalysisSnapshot(
|
||||
val received: Int = 0, val processed: Int = 0, val failures: Int = 0,
|
||||
val faceCount: Int = 0, val cue: FaceCue = FaceCue.UNKNOWN,
|
||||
val reason: FaceCueReason = FaceCueReason.NO_FRAME, val failure: CameraFailure = CameraFailure.NONE,
|
||||
val lastProcessMs: Long = 0,
|
||||
)
|
||||
|
||||
class CameraAnalysisDiagnostics {
|
||||
private var state = CameraAnalysisSnapshot()
|
||||
private var lastLogMs: Long? = null
|
||||
@Synchronized fun received() { state = state.copy(received = (state.received + 1).coerceAtMost(1_000_000)) }
|
||||
@Synchronized fun processed(count: Int, cue: FaceCueObservation, elapsedMs: Long) {
|
||||
state = state.copy(processed = (state.processed + 1).coerceAtMost(1_000_000),
|
||||
faceCount = count.coerceIn(0, 2), cue = cue.cue, reason = cue.reason,
|
||||
lastProcessMs = elapsedMs.coerceIn(0, 60_000))
|
||||
}
|
||||
@Synchronized fun failed(failure: CameraFailure) {
|
||||
state = state.copy(failures = (state.failures + 1).coerceAtMost(1_000_000), failure = failure)
|
||||
}
|
||||
@Synchronized fun snapshot() = state
|
||||
@Synchronized fun summary(nowMs: Long, force: Boolean = false): String? {
|
||||
val last = lastLogMs
|
||||
if (!force && last != null && nowMs - last < 10_000) return null
|
||||
lastLogMs = nowMs
|
||||
return "camera analysis received=${state.received} processed=${state.processed} failures=${state.failures} faces=${state.faceCount} cue=${state.cue.wire} reason=${state.reason.name.lowercase()} failure=${state.failure.name.lowercase()} process_ms=${state.lastProcessMs}"
|
||||
}
|
||||
}
|
||||
|
||||
enum class FaceSendReason { SHARING_OFF, BACKGROUND, NOT_CONNECTED, VOICE_INACTIVE, UNSUPPORTED, NO_TURN,
|
||||
NO_FACE, NO_STABLE_CUE, STALE, INVALID_CUE, SENT, SEND_FAILED, CLEARED, CLEAR_FAILED }
|
||||
data class FaceTransmissionSnapshot(val attempts: Int = 0, val sent: Int = 0, val clears: Int = 0,
|
||||
val failures: Int = 0, val reason: FaceSendReason = FaceSendReason.SHARING_OFF, val cue: FaceCue = FaceCue.UNKNOWN)
|
||||
class FaceTransmissionDiagnostics {
|
||||
private var state = FaceTransmissionSnapshot()
|
||||
private var lastLogMs: Long? = null
|
||||
fun record(reason: FaceSendReason, cue: FaceCue = FaceCue.UNKNOWN) {
|
||||
val attempt = reason in setOf(FaceSendReason.SENT, FaceSendReason.SEND_FAILED, FaceSendReason.CLEARED, FaceSendReason.CLEAR_FAILED)
|
||||
state = state.copy(reason = reason, cue = cue,
|
||||
attempts = (state.attempts + if (attempt) 1 else 0).coerceAtMost(1_000_000),
|
||||
sent = (state.sent + if (reason == FaceSendReason.SENT) 1 else 0).coerceAtMost(1_000_000),
|
||||
clears = (state.clears + if (reason == FaceSendReason.CLEARED) 1 else 0).coerceAtMost(1_000_000),
|
||||
failures = (state.failures + if (reason in setOf(FaceSendReason.SEND_FAILED, FaceSendReason.CLEAR_FAILED)) 1 else 0).coerceAtMost(1_000_000))
|
||||
}
|
||||
fun snapshot() = state
|
||||
fun summary(nowMs: Long): String? {
|
||||
val last = lastLogMs
|
||||
if (last != null && nowMs - last < 10_000) return null
|
||||
lastLogMs = nowMs
|
||||
return "face transmission attempts=${state.attempts} sent=${state.sent} clears=${state.clears} failures=${state.failures} reason=${state.reason.name.lowercase()} cue=${state.cue.wire}"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user