From 1d66fa647359203d1512c48403124947dde7b85b Mon Sep 17 00:00:00 2001 From: MOLVMIN <506644585@qq.com> Date: Mon, 20 Jul 2026 14:49:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(vision):=20=E8=AF=86=E5=88=AB=E5=91=BD?= =?UTF-8?q?=E4=B8=AD=E5=90=8E=E5=9B=9E=E8=B0=83=20NFC=20=E5=86=99=E5=8D=A1?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - vision_handler:识别到手办后调用 _write_figurine_nfc(),POST 主机侧 figurine-nfc-writer 服务(FIGURINE_NFC_WRITER_URL)向公仔卡写角色名; 写卡失败不阻塞语音播报(降级) - docker-compose:新增 FIGURINE_NFC_WRITER_URL 环境变量 (http://host.docker.internal:8005/write) Co-Authored-By: Claude Opus 4.8 --- .../xiaozhi-server/core/api/vision_handler.py | 30 +++++++++++++++++++ main/xiaozhi-server/docker-compose.yml | 3 ++ 2 files changed, 33 insertions(+) diff --git a/main/xiaozhi-server/core/api/vision_handler.py b/main/xiaozhi-server/core/api/vision_handler.py index ecc6a2eda..667fc1277 100644 --- a/main/xiaozhi-server/core/api/vision_handler.py +++ b/main/xiaozhi-server/core/api/vision_handler.py @@ -21,6 +21,10 @@ MAX_FILE_SIZE = 5 * 1024 * 1024 # 手办识别微服务地址(tools/figurine-recognizer),留空则跳过识别直接走VLLM FIGURINE_RECOGNIZER_URL = os.getenv("FIGURINE_RECOGNIZER_URL", "").strip() +# 手办 NFC 写卡服务地址(tools/figurine-nfc-writer,主机原生运行)。 +# 留空则不写卡。容器内访问主机用 http://host.docker.internal:8005/write +FIGURINE_NFC_WRITER_URL = os.getenv("FIGURINE_NFC_WRITER_URL", "").strip() + # 调试用:把设备实拍的照片存到 data/vision_captures/,用于采集设备参考图 SAVE_VISION_CAPTURES = os.getenv("SAVE_VISION_CAPTURES", "").strip() == "1" VISION_CAPTURE_DIR = "/opt/xiaozhi-esp32-server/data/vision_captures" @@ -70,6 +74,30 @@ async def _recognize_figurine(logger, image_data: bytes) -> Optional[str]: return None +async def _write_figurine_nfc(logger, name: str) -> None: + """识别命中后,通知主机侧 NFC 写卡服务向公仔卡写入角色名。 + 写卡失败不应影响语音播报,只记日志(降级)。""" + if not FIGURINE_NFC_WRITER_URL: + return + try: + timeout = aiohttp.ClientTimeout(total=10) + async with aiohttp.ClientSession(timeout=timeout) as session: + async with session.post(FIGURINE_NFC_WRITER_URL, json={"name": name}) as resp: + result = await resp.json() + if result.get("ok"): + logger.bind(tag=TAG).info( + f"NFC 写卡成功:{name} -> 角色名 {result.get('role')} " + f"(UID {result.get('uid')}, 块 {result.get('written_blocks')})" + ) + else: + logger.bind(tag=TAG).warning( + f"NFC 写卡未成功:{name},原因:{result.get('error')}" + ) + except Exception as e: + # 写卡服务不可用(如没插读卡器/服务没起)不应影响语音,降级 + logger.bind(tag=TAG).warning(f"调用 NFC 写卡服务失败,跳过:{e}") + + class VisionHandler(BaseHandler): def __init__(self, config: dict): super().__init__(config) @@ -159,6 +187,8 @@ class VisionHandler(BaseHandler): if figurine_name: # 识别命中:直接返回简短结果,不再调用 VLLM 长篇描述 self.logger.bind(tag=TAG).info(f"手办识别命中:{figurine_name}") + # 识别命中后触发向公仔 NFC 卡写入角色名(失败不阻塞语音播报) + await _write_figurine_nfc(self.logger, figurine_name) # 播报名用空格替换下划线(如 julius_caesar → julius caesar) display_name = figurine_name.replace("_", " ") result = f"这是{display_name}" diff --git a/main/xiaozhi-server/docker-compose.yml b/main/xiaozhi-server/docker-compose.yml index 5743b5bb1..c40577f17 100644 --- a/main/xiaozhi-server/docker-compose.yml +++ b/main/xiaozhi-server/docker-compose.yml @@ -12,6 +12,9 @@ services: - TZ=Asia/Shanghai # 手办识别微服务地址(tools/figurine-recognizer 跑在宿主机 8004 端口) - FIGURINE_RECOGNIZER_URL=http://host.docker.internal:8004/recognize + # 手办 NFC 写卡服务地址(tools/figurine-nfc-writer 跑在宿主机 8005 端口,主机原生运行以访问USB串口) + # 留空则识别命中后不写卡;填上则识别命中自动向公仔卡写角色名 + - FIGURINE_NFC_WRITER_URL=http://host.docker.internal:8005/write # 采集设备实拍照片到 data/vision_captures/(采集完设备参考图后可关掉) - SAVE_VISION_CAPTURES=1 extra_hosts: