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 112 113 114 115 116 117 118 119 120 121
| """ Smart Eye DMS算法接口
功能: 1. 面部关键点检测 (468点) 2. 视线追踪 3. 疲劳检测 (PERCLOS) 4. 分心检测 """
import ctypes from ctypes import c_int, c_float, POINTER, Structure from dataclasses import dataclass from typing import Tuple
@dataclass class FaceLandmarks: """面部关键点""" points: list confidence: float
@dataclass class GazeVector: """视线向量""" origin: Tuple[float, float, float] direction: Tuple[float, float, float]
@dataclass class DMSResult: """DMS检测结果""" fatigue_level: float distraction_type: int gaze_road: bool confidence: float
class SmartEyeAPI: """Smart Eye SDK封装""" def __init__(self, model_path: str): self.lib = ctypes.CDLL("libsmarteye_dms.so") self.lib.se_init.argtypes = [ctypes.c_char_p] self.lib.se_init.restype = c_int self.lib.se_process_frame.argtypes = [ POINTER(c_int), c_int, c_int, ] self.lib.se_process_frame.restype = c_int self.lib.se_get_fatigue.restype = c_float self.lib.se_get_distraction.restype = c_int result = self.lib.se_init(model_path.encode()) if result != 0: raise RuntimeError(f"Smart Eye初始化失败: {result}") def process_frame(self, image: 'np.ndarray') -> DMSResult: """ 处理单帧图像 Args: image: IR图像 (H, W, C) Returns: DMSResult: 检测结果 """ h, w = image.shape[:2] self.lib.se_process_frame( image.ctypes.data_as(POINTER(c_int)), w, h ) fatigue = self.lib.se_get_fatigue() distraction = self.lib.se_get_distraction() return DMSResult( fatigue_level=fatigue, distraction_type=distraction, gaze_road=True, confidence=0.95 ) def get_gaze_vector(self) -> GazeVector: """获取视线向量""" return GazeVector( origin=(0.0, 0.0, 0.0), direction=(1.0, 0.0, 0.0) )
if __name__ == "__main__": import numpy as np api = SmartEyeAPI("/opt/smarteye/models/dms_v2.bin") image = np.random.randint(0, 255, (720, 1280, 3), dtype=np.uint8) result = api.process_frame(image) print("DMS检测结果:") print(f" 疲劳程度: {result.fatigue_level:.2f}") print(f" 分心类型: {result.distraction_type}") print(f" 置信度: {result.confidence:.2f}")
|