1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| """ Cosmos 3 Edge 推理客户端示例
基于NVIDIA官方openpi_client协议 适配座舱监控场景 """
import numpy as np from PIL import Image from openpi_client.websocket_client_policy import WebsocketClientPolicy
class CabinMonitoringInference: """座舱监控推理客户端""" def __init__(self, host="localhost", port=8000): self.client = WebsocketClientPolicy(host=host, port=port) def infer(self, dms_image, oms_image, vehicle_state): """ 座舱监控推理 Args: dms_image: DMS摄像头帧 (H,W,3), RGB oms_image: OMS摄像头帧 (H,W,3), RGB vehicle_state: 车辆状态 dict Returns: result: { 'fatigue_level': 0-3, 'distraction_type': str, 'cpd_detected': bool, 'oop_detected': bool, 'warning_level': 0-3, 'action_chunk': 32步预测 } """ observation = { "prompt": "monitor cabin safety: detect fatigue, distraction, CPD, OOP", "observation/dms_image": np.asarray(dms_image), "observation/oms_image": np.asarray(oms_image), "observation/vehicle_speed": np.float32(vehicle_state.get("speed", 0)), "observation/steering_angle": np.float32(vehicle_state.get("steering", 0)), } result = self.client.infer(observation) actions = result["action"] return { "fatigue_level": int(actions[0, 0]), "distraction_type": ["none", "phone", "conversation", "daydreaming"][int(actions[0, 1]) % 4], "cpd_detected": bool(actions[0, 2] > 0.5), "oop_detected": bool(actions[0, 3] > 0.5), "warning_level": int(actions[0, 4]), "action_chunk": actions }
if __name__ == "__main__": print("=== Cosmos 3 Edge 座舱监控推理测试 ===\n") dms_img = Image.new("RGB", (640, 480)) oms_img = Image.new("RGB", (320, 240)) vehicle = {"speed": 60.0, "steering": 0.5} print(f"DMS图像: {dms_img.size}") print(f"OMS图像: {oms_img.size}") print(f"车辆状态: {vehicle}") print(f"推理协议: WebSocket (openpi_client)") print(f"输出: 32步action chunk @ 15Hz") print(f"覆盖时间: {32/15:.2f}秒") print(f"\n注: 需先部署Cosmos 3 Edge到Jetson Thor") print(f"并启动policy server: python -m cosmos_framework.scripts.action_policy_server")
|