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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| import numpy as np from qualcomm_snpe import SNPEModel
class SeeingMachinesDMS: """ Seeing Machines DMS on Snapdragon Ride """ def __init__(self, model_path): self.model = SNPEModel(model_path) self.input_shape = (1, 3, 224, 224) self.output_names = ["fatigue", "distraction", "gaze"] def preprocess(self, image): """ 图像预处理 Args: image: 输入图像 (H, W, 3) Returns: tensor: 预处理后的张量 """ resized = cv2.resize(image, (224, 224)) normalized = resized.astype(np.float32) / 255.0 transposed = np.transpose(normalized, (2, 0, 1)) tensor = np.expand_dims(transposed, 0) return tensor def infer(self, image): """ 推理 Args: image: 输入图像 Returns: results: 检测结果 """ input_tensor = self.preprocess(image) outputs = self.model.execute(input_tensor) results = { "fatigue_score": outputs["fatigue"][0], "distraction_score": outputs["distraction"][0], "gaze_direction": outputs["gaze"][:2] } return results def post_process(self, results, thresholds): """ 后处理 Args: results: 推理结果 thresholds: 阈值配置 Returns: alerts: 警告信息 """ alerts = [] if results["fatigue_score"] > thresholds["fatigue"]: alerts.append({ "type": "FATIGUE", "level": 1 if results["fatigue_score"] < 0.8 else 2, "score": results["fatigue_score"] }) if results["distraction_score"] > thresholds["distraction"]: alerts.append({ "type": "DISTRACTION", "level": 1, "score": results["distraction_score"] }) return alerts
if __name__ == "__main__": dms = SeeingMachinesDMS("/models/dms.dlc") image = np.random.randint(0, 255, (720, 1280, 3), dtype=np.uint8) results = dms.infer(image) alerts = dms.post_process(results, {"fatigue": 0.6, "distraction": 0.5}) print(f"疲劳得分: {results['fatigue_score']:.2f}") print(f"分心得分: {results['distraction_score']:.2f}") print(f"视线方向: {results['gaze_direction']}")
|