90 lines
4.5 KiB
Python
90 lines
4.5 KiB
Python
"""Versioned enum-only camera observations, scoped to a live socket/utterance. No images or text."""
|
|
import math
|
|
import time
|
|
|
|
TEMPLATE = (
|
|
'本轮可选摄像头观察(可能误判,不是情绪识别):{cue},规则强度{intensity},持续约{duration}毫秒。'
|
|
'只作为弱辅助信息,用户台词和聊天内容优先;冲突时忽略观察。'
|
|
'微笑可能是礼貌或苦笑,皱眉可能是困惑,不能断言内心情绪、健康状况或危险等级。'
|
|
'不做身份识别,不从闭眼推断疲劳或疾病,不必每句评论表情。'
|
|
)
|
|
|
|
|
|
class FaceContext:
|
|
def __init__(self):
|
|
self.value = None
|
|
self.received = self.accepted = self.rejected = self.clears = 0
|
|
self.receive_reason = 'not_received'
|
|
self.use_reason = 'not_taken'
|
|
self.state_reason = 'no_cue'
|
|
self.used_cue = 'unknown'
|
|
|
|
def clear(self, reason='cleared'):
|
|
self.value = None
|
|
self.state_reason = reason
|
|
|
|
def diagnostics(self):
|
|
return {'received': self.received, 'accepted': self.accepted, 'rejected': self.rejected,
|
|
'clears': self.clears, 'receive_reason': self.receive_reason,
|
|
'use_reason': self.use_reason, 'cue': self.used_cue}
|
|
|
|
def update(self, data, turn, now=None):
|
|
self.clear() # Invalid observations must never leave an older cue latched.
|
|
self.received = min(self.received + 1, 1_000_000)
|
|
def reject(reason):
|
|
self.rejected = min(self.rejected + 1, 1_000_000)
|
|
self.receive_reason = self.state_reason = reason
|
|
return False
|
|
allowed = {'type', 'version', 'turn_id', 'valid', 'cue', 'intensity', 'duration_ms', 'age_ms'}
|
|
if not isinstance(data, dict) or set(data) != allowed:
|
|
return reject('invalid_fields')
|
|
if data['type'] != 'context.face_cue' or type(data['version']) is not int or data['version'] != 1:
|
|
return reject('invalid_version')
|
|
if type(data['turn_id']) is not int or not 0 <= data['turn_id'] <= 0xffffffff or data['turn_id'] != turn:
|
|
return reject('wrong_turn')
|
|
if type(data['valid']) is not bool:
|
|
return reject('invalid_valid')
|
|
age, duration, intensity = data['age_ms'], data['duration_ms'], data['intensity']
|
|
if type(age) is not int or not 0 <= age <= 3000 or type(duration) is not int or not 0 <= duration <= 10000:
|
|
return reject('invalid_age_duration')
|
|
if type(intensity) not in (int, float) or not math.isfinite(intensity) or not 0 <= intensity <= 1:
|
|
return reject('invalid_intensity')
|
|
if not data['valid']:
|
|
if data['cue'] != 'unknown' or intensity != 0 or duration != 0: return reject('invalid_clear')
|
|
self.clears = min(self.clears + 1, 1_000_000)
|
|
self.receive_reason = self.state_reason = 'explicit_clear'
|
|
return True
|
|
if data['cue'] not in ('smiling', 'brow_furrowed') or duration < 800 or intensity < .5:
|
|
return reject('invalid_cue')
|
|
self.accepted = min(self.accepted + 1, 1_000_000)
|
|
self.receive_reason = self.state_reason = 'accepted'
|
|
self.value = (turn, data['cue'], intensity, duration, age, time.monotonic() if now is None else now)
|
|
return True
|
|
|
|
def advance_capture(self, old_turn, new_turn):
|
|
# new_turn names the response to the just-finished capture, not another utterance.
|
|
if self.value and self.value[0] == old_turn:
|
|
self.value = (new_turn, *self.value[1:])
|
|
else:
|
|
self.clear('new_turn')
|
|
|
|
def take(self, turn, now=None):
|
|
value, self.value = self.value, None
|
|
self.used_cue = 'unknown'
|
|
if value is None:
|
|
self.use_reason = self.state_reason
|
|
return None
|
|
if value[0] != turn:
|
|
self.use_reason = self.state_reason = 'wrong_turn'
|
|
return None
|
|
_, cue, intensity, duration, age, received = value
|
|
elapsed = ((time.monotonic() if now is None else now) - received) * 1000
|
|
if elapsed < 0 or age + round(elapsed, 6) > 3000:
|
|
self.use_reason = self.state_reason = 'stale'
|
|
return None
|
|
self.use_reason = 'used'
|
|
self.state_reason = 'consumed'
|
|
self.used_cue = cue
|
|
label = {'smiling': '明显、持续的嘴角上扬', 'brow_furrowed': '明显、持续的双侧眉头收紧'}[cue]
|
|
return {'role': 'system', 'content': TEMPLATE.format(cue=label, intensity=round(intensity, 2), duration=duration)}
|