expression-lab-classroom 1.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- expression_lab/__init__.py +13 -0
- expression_lab/__main__.py +17 -0
- expression_lab/api.py +192 -0
- expression_lab/backend.py +166 -0
- expression_lab/bootstrap.py +36 -0
- expression_lab/cameras.py +172 -0
- expression_lab/core.py +259 -0
- expression_lab/data/images/01_neutral.png +0 -0
- expression_lab/data/images/provenance.json +23 -0
- expression_lab/data/licenses/LibreFace-USC.txt +14 -0
- expression_lab/data/licenses/MediaPipe-Apache-2.0.txt +218 -0
- expression_lab/data/licenses/README.md +10 -0
- expression_lab/data/model_manifest.json +37 -0
- expression_lab/download_models.py +132 -0
- expression_lab/resources.py +40 -0
- expression_lab/ui.py +528 -0
- expression_lab/worker.py +206 -0
- expression_lab_classroom-1.3.0.dist-info/METADATA +80 -0
- expression_lab_classroom-1.3.0.dist-info/RECORD +23 -0
- expression_lab_classroom-1.3.0.dist-info/WHEEL +4 -0
- expression_lab_classroom-1.3.0.dist-info/entry_points.txt +3 -0
- expression_lab_classroom-1.3.0.dist-info/licenses/licenses/LibreFace-USC.txt +14 -0
- expression_lab_classroom-1.3.0.dist-info/licenses/licenses/MediaPipe-Apache-2.0.txt +218 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""高中人脸表情实验室:本地 CPU 模型与桌面可视化。"""
|
|
2
|
+
|
|
3
|
+
__version__ = "1.3.0"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def __getattr__(name):
|
|
7
|
+
# 几何计算和模型自检不需要初始化 Qt 或打开摄像头。
|
|
8
|
+
if name == "FaceCamera":
|
|
9
|
+
from .bootstrap import ensure_runtime
|
|
10
|
+
ensure_runtime()
|
|
11
|
+
from .api import FaceCamera
|
|
12
|
+
return FaceCamera
|
|
13
|
+
raise AttributeError(name)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from . import FaceCamera
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def main():
|
|
5
|
+
camera = FaceCamera()
|
|
6
|
+
camera.window.result.setToolTip('AU12 的 f1 ≥ 7 且 AU6 的 f1 ≥ 5 → 微笑;AU26 的 f1 ≥ 6 → 张嘴;其余 → 自然')
|
|
7
|
+
for f in camera:
|
|
8
|
+
expression = '自然'
|
|
9
|
+
if f[1] >= 7 and f[2] >= 5:
|
|
10
|
+
expression = '微笑'
|
|
11
|
+
elif f[3] >= 6:
|
|
12
|
+
expression = '张嘴'
|
|
13
|
+
camera.show(expression)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
if __name__ == '__main__':
|
|
17
|
+
main()
|
expression_lab/api.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""学生使用的极简接口;迭代器内部处理事件、模型和资源释放。"""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import ast
|
|
5
|
+
import queue
|
|
6
|
+
import sys
|
|
7
|
+
import time
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from PySide6.QtCore import QEventLoop, QTimer
|
|
11
|
+
from PySide6.QtGui import QFont
|
|
12
|
+
from PySide6.QtWidgets import QApplication
|
|
13
|
+
|
|
14
|
+
from .cameras import CameraDevice, CameraMonitor
|
|
15
|
+
from .core import feature_values
|
|
16
|
+
from .resources import sample_image
|
|
17
|
+
from .ui import LabWindow
|
|
18
|
+
from .worker import InferenceWorker
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def rule_description(path):
|
|
22
|
+
try:
|
|
23
|
+
tree = ast.parse(Path(path).read_text(encoding='utf-8-sig'))
|
|
24
|
+
descriptions = []
|
|
25
|
+
for node in ast.walk(tree):
|
|
26
|
+
if not isinstance(node, ast.If):
|
|
27
|
+
continue
|
|
28
|
+
for statement in node.body:
|
|
29
|
+
if (isinstance(statement, ast.Assign) and isinstance(statement.value, ast.Constant)
|
|
30
|
+
and any(isinstance(target, ast.Name) and target.id == 'expression'
|
|
31
|
+
for target in statement.targets)):
|
|
32
|
+
condition = ast.unparse(node.test).replace(' and ', ' 且 ').replace(' or ', ' 或 ')
|
|
33
|
+
descriptions.append(f'{condition} → {statement.value.value}')
|
|
34
|
+
if descriptions:
|
|
35
|
+
return ';'.join(descriptions) + ';其余 → 默认值'
|
|
36
|
+
return '按学生主程序中的条件判断'
|
|
37
|
+
except (OSError, SyntaxError):
|
|
38
|
+
return '按学生主程序中的条件判断'
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def parse_options(args):
|
|
42
|
+
parser = argparse.ArgumentParser(description='离线人脸 AU 表情实验室(Python 3.12)')
|
|
43
|
+
source = parser.add_mutually_exclusive_group()
|
|
44
|
+
source.add_argument('--camera', type=int, default=0, help='摄像头序号,默认 0')
|
|
45
|
+
source.add_argument('--image', '--video', dest='source', help='改用本地图片或循环视频')
|
|
46
|
+
source.add_argument('--sample', action='store_true', help='使用随包离线人脸图片')
|
|
47
|
+
parser.add_argument('--model-dir', type=Path, help='本地模型目录')
|
|
48
|
+
parser.add_argument('--fps', type=int, default=15, choices=range(1, 31), metavar='1–30', help='推理帧率上限,默认 15')
|
|
49
|
+
return parser.parse_args(args)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class FaceCamera:
|
|
53
|
+
def __init__(self, camera=0, *, source=None, model_dir=None, fps=15, args=None, values='features'):
|
|
54
|
+
if values not in ('features', 'au'):
|
|
55
|
+
raise ValueError('values 应为 features(0–20整数分)或 au(原始0–1强度)')
|
|
56
|
+
self._values = values
|
|
57
|
+
options = parse_options(sys.argv[1:] if args is None else args)
|
|
58
|
+
if source is None:
|
|
59
|
+
source = sample_image() if options.sample else options.source
|
|
60
|
+
if source is None:
|
|
61
|
+
source = options.camera if options.camera != 0 else camera
|
|
62
|
+
self._model_dir = model_dir if model_dir is not None else options.model_dir
|
|
63
|
+
self._fps = options.fps if options.fps != 15 else fps
|
|
64
|
+
self._app = QApplication.instance() or QApplication([])
|
|
65
|
+
self._app.setFont(QFont('Microsoft YaHei' if sys.platform == 'win32' else 'PingFang SC', 10))
|
|
66
|
+
self._app.setQuitOnLastWindowClosed(False)
|
|
67
|
+
self._loop = QEventLoop()
|
|
68
|
+
self._timer = QTimer()
|
|
69
|
+
self._timer.setInterval(16)
|
|
70
|
+
self._timer.timeout.connect(self._loop.quit)
|
|
71
|
+
self._timer.timeout.connect(self._poll_cameras)
|
|
72
|
+
self.window = LabWindow()
|
|
73
|
+
self.window.result.setToolTip(rule_description(Path(sys.argv[0])))
|
|
74
|
+
self._closed = False
|
|
75
|
+
self.current = None
|
|
76
|
+
self.worker = None
|
|
77
|
+
self._retired_workers = []
|
|
78
|
+
self._camera_monitor = CameraMonitor()
|
|
79
|
+
self._camera_monitor.start()
|
|
80
|
+
self.window.closed.connect(self.close)
|
|
81
|
+
self.window.calibrate_requested.connect(lambda: self.worker.commands.put('calibrate'))
|
|
82
|
+
self.window.reset_requested.connect(lambda: self.worker.commands.put('reset'))
|
|
83
|
+
self.window.pause_changed.connect(self._pause)
|
|
84
|
+
self.window.source_requested.connect(self._start_worker)
|
|
85
|
+
self.window.show()
|
|
86
|
+
self._start_worker(source)
|
|
87
|
+
self._timer.start()
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def is_closed(self):
|
|
91
|
+
return self._closed
|
|
92
|
+
|
|
93
|
+
def _pause(self, paused):
|
|
94
|
+
if paused:
|
|
95
|
+
self.worker.paused.set()
|
|
96
|
+
else:
|
|
97
|
+
# 丢弃暂停前尚未显示的结果,继续后等新一帧。
|
|
98
|
+
try:
|
|
99
|
+
self.worker.results.get_nowait()
|
|
100
|
+
except queue.Empty:
|
|
101
|
+
pass
|
|
102
|
+
self.worker.paused.clear()
|
|
103
|
+
|
|
104
|
+
def _start_worker(self, source):
|
|
105
|
+
if self._closed:
|
|
106
|
+
return
|
|
107
|
+
if self.worker is not None:
|
|
108
|
+
self.worker.stop.set()
|
|
109
|
+
self._retired_workers.append(self.worker)
|
|
110
|
+
# 摄像头驱动可能迟迟不返回。主线程只发取消信号,绝不 join/read/release。
|
|
111
|
+
self._retired_workers = [worker for worker in self._retired_workers if worker.is_alive()]
|
|
112
|
+
self._last_result_at = time.monotonic()
|
|
113
|
+
self._has_result = False
|
|
114
|
+
self._stale_notified = False
|
|
115
|
+
self.current = None
|
|
116
|
+
self.window.last_observation = None
|
|
117
|
+
self.window.chart.history.clear()
|
|
118
|
+
self.window.pause_button.setChecked(False)
|
|
119
|
+
self.window.canvas.observation = None
|
|
120
|
+
self.window.canvas.placeholder = '正在加载本地 CPU 模型…'
|
|
121
|
+
self.window.canvas.mirror = isinstance(source, (int, CameraDevice))
|
|
122
|
+
self.window.set_source(source)
|
|
123
|
+
self.window.show_error('正在加载本地 CPU 模型…')
|
|
124
|
+
self.worker = InferenceWorker(source, model_dir=self._model_dir, fps=self._fps)
|
|
125
|
+
self.worker.start()
|
|
126
|
+
|
|
127
|
+
def _poll_cameras(self):
|
|
128
|
+
try:
|
|
129
|
+
devices, error = self._camera_monitor.results.get_nowait()
|
|
130
|
+
except queue.Empty:
|
|
131
|
+
return
|
|
132
|
+
self.window.set_cameras(devices, error)
|
|
133
|
+
|
|
134
|
+
def __iter__(self):
|
|
135
|
+
try:
|
|
136
|
+
while not self._closed:
|
|
137
|
+
observation = self._wait_for_face()
|
|
138
|
+
if observation is None:
|
|
139
|
+
return
|
|
140
|
+
self.current = observation
|
|
141
|
+
yield feature_values(observation.geometric_au) if self._values == 'features' else observation.au.copy()
|
|
142
|
+
finally:
|
|
143
|
+
self.close()
|
|
144
|
+
|
|
145
|
+
def _wait_for_face(self):
|
|
146
|
+
while not self._closed:
|
|
147
|
+
if not self.window.paused:
|
|
148
|
+
try:
|
|
149
|
+
result = self.worker.results.get_nowait()
|
|
150
|
+
except queue.Empty:
|
|
151
|
+
result = None
|
|
152
|
+
if isinstance(result, dict):
|
|
153
|
+
self._stale_notified = True
|
|
154
|
+
self.window.show_error(result.get('progress', result.get('error', '正在准备模型…')))
|
|
155
|
+
elif result is not None:
|
|
156
|
+
self._last_result_at = time.monotonic()
|
|
157
|
+
self._has_result = True
|
|
158
|
+
self._stale_notified = False
|
|
159
|
+
if result.points is not None and (self._values == 'au' or result.geometric_au):
|
|
160
|
+
return result
|
|
161
|
+
self.window.present(result, None)
|
|
162
|
+
elif not self._stale_notified:
|
|
163
|
+
timeout = 2 if self._has_result else 8
|
|
164
|
+
if time.monotonic() - self._last_result_at > timeout:
|
|
165
|
+
self.current = None
|
|
166
|
+
self.window.show_error('等待摄像头画面,请切换来源。')
|
|
167
|
+
self._stale_notified = True
|
|
168
|
+
# Qt 事件循环等待下一次定时器或关窗事件,不使用占满 CPU 的忙循环。
|
|
169
|
+
self._loop.exec()
|
|
170
|
+
return None
|
|
171
|
+
|
|
172
|
+
def show(self, expression):
|
|
173
|
+
if not self._closed and self.current is not None:
|
|
174
|
+
self.window.present(self.current, str(expression))
|
|
175
|
+
|
|
176
|
+
def close(self):
|
|
177
|
+
if self._closed:
|
|
178
|
+
return
|
|
179
|
+
self._closed = True
|
|
180
|
+
self._timer.stop()
|
|
181
|
+
self._camera_monitor.stop.set()
|
|
182
|
+
self._loop.quit()
|
|
183
|
+
for worker in [self.worker, *self._retired_workers]:
|
|
184
|
+
if worker is not None:
|
|
185
|
+
worker.stop.set()
|
|
186
|
+
self.window.hide()
|
|
187
|
+
|
|
188
|
+
def __enter__(self):
|
|
189
|
+
return self
|
|
190
|
+
|
|
191
|
+
def __exit__(self, *_):
|
|
192
|
+
self.close()
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""本地 MediaPipe + LibreFace ONNX CPU 推理;初始化时准备模型缓存。"""
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import json
|
|
5
|
+
import time
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import cv2
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
from .core import decode_intensities, face_coordinates
|
|
13
|
+
from .download_models import prepare_models
|
|
14
|
+
from .resources import model_directory
|
|
15
|
+
|
|
16
|
+
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
|
17
|
+
REQUIRED_MODELS = ('face_landmarker.task', 'LibreFace_AU_Encoder.onnx', 'LibreFace_AU_Intensity.onnx')
|
|
18
|
+
EYE_LEFT = (33, 7, 163, 144, 145, 153, 154, 155, 133, 246, 161, 160, 159, 158, 157, 173)
|
|
19
|
+
EYE_RIGHT = (263, 249, 390, 373, 374, 380, 381, 382, 362, 466, 388, 387, 386, 385, 384, 398)
|
|
20
|
+
MOUTH = (61, 146, 91, 181, 84, 17, 314, 405, 321, 375, 291, 185, 40, 39, 37, 0, 267,
|
|
21
|
+
269, 270, 409, 78, 95, 88, 178, 87, 14, 317, 402, 318, 324, 308, 191, 80, 81,
|
|
22
|
+
82, 13, 312, 311, 310, 415)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ModelFiles:
|
|
26
|
+
def __init__(self, directory=None):
|
|
27
|
+
self.directory = Path(directory) if directory is not None else model_directory()
|
|
28
|
+
|
|
29
|
+
def __getitem__(self, name):
|
|
30
|
+
return self.directory / name
|
|
31
|
+
|
|
32
|
+
def validate(self, verify_hashes=False):
|
|
33
|
+
for name in REQUIRED_MODELS:
|
|
34
|
+
path = self[name]
|
|
35
|
+
if not path.is_file() or path.stat().st_size < 10_000:
|
|
36
|
+
raise FileNotFoundError(
|
|
37
|
+
f'缺少或损坏本地模型:{path}。pip 版请运行 expression-lab-models,'
|
|
38
|
+
'离线包请恢复 models 文件夹。')
|
|
39
|
+
if verify_hashes:
|
|
40
|
+
manifest = json.loads((self.directory / 'manifest.json').read_text(encoding='utf-8'))
|
|
41
|
+
for name in REQUIRED_MODELS:
|
|
42
|
+
if hashlib.sha256(self[name].read_bytes()).hexdigest() != manifest['files'][name]['sha256']:
|
|
43
|
+
raise ValueError(f'本地模型校验失败:{name}。请从完整离线包恢复。')
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def aligned_face(frame_bgr, points):
|
|
47
|
+
"""LibreFace/FFHQ 的眼睛—嘴部四边形对齐,256 图中心裁成 224。
|
|
48
|
+
|
|
49
|
+
保持上游的轴与尺度;超出画面的部分用反射填充(不做上游的边缘模糊)。
|
|
50
|
+
"""
|
|
51
|
+
left = points[list(EYE_LEFT)].mean(axis=0)
|
|
52
|
+
right = points[list(EYE_RIGHT)].mean(axis=0)
|
|
53
|
+
eye_average = (left + right) / 2
|
|
54
|
+
eye_to_eye = right - left
|
|
55
|
+
lips = points[list(MOUTH)]
|
|
56
|
+
mouth_average = (lips[np.argmin(lips[:, 0])] + lips[np.argmax(lips[:, 0])]) / 2
|
|
57
|
+
eye_to_mouth = mouth_average - eye_average
|
|
58
|
+
x_axis = eye_to_eye - np.array([-eye_to_mouth[1], eye_to_mouth[0]])
|
|
59
|
+
length = np.linalg.norm(x_axis)
|
|
60
|
+
if length < 1e-6:
|
|
61
|
+
raise ValueError('人脸对齐失败,请正对摄像头')
|
|
62
|
+
x_axis = x_axis / length * max(np.linalg.norm(eye_to_eye) * 2, np.linalg.norm(eye_to_mouth) * 1.8)
|
|
63
|
+
y_axis = np.array([-x_axis[1], x_axis[0]])
|
|
64
|
+
center = eye_average + eye_to_mouth * 0.1
|
|
65
|
+
source = np.float32([center - x_axis - y_axis, center - x_axis + y_axis, center + x_axis - y_axis])
|
|
66
|
+
destination = np.float32([[0, 0], [0, 255], [255, 0]])
|
|
67
|
+
transform = cv2.getAffineTransform(source, destination)
|
|
68
|
+
aligned = cv2.warpAffine(frame_bgr, transform, (256, 256), flags=cv2.INTER_LINEAR,
|
|
69
|
+
borderMode=cv2.BORDER_REFLECT_101)
|
|
70
|
+
return np.ascontiguousarray(aligned[16:240, 16:240])
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def image_tensor(aligned_bgr):
|
|
74
|
+
rgb = cv2.cvtColor(aligned_bgr, cv2.COLOR_BGR2RGB).astype(np.float32) / 255
|
|
75
|
+
normalized = (rgb - np.array([0.485, 0.456, 0.406], np.float32)) / np.array(
|
|
76
|
+
[0.229, 0.224, 0.225], np.float32)
|
|
77
|
+
return np.ascontiguousarray(normalized.transpose(2, 0, 1)[None])
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dataclass
|
|
81
|
+
class Observation:
|
|
82
|
+
frame: np.ndarray
|
|
83
|
+
timestamp: float
|
|
84
|
+
points: np.ndarray | None = None
|
|
85
|
+
coordinates: np.ndarray | None = None
|
|
86
|
+
au: dict = field(default_factory=dict)
|
|
87
|
+
inference_ms: float = 0
|
|
88
|
+
status: str = '请正对摄像头'
|
|
89
|
+
baseline: np.ndarray | None = None
|
|
90
|
+
motion: dict = field(default_factory=dict)
|
|
91
|
+
geometric_au: dict = field(default_factory=dict)
|
|
92
|
+
geometry: dict = field(default_factory=dict)
|
|
93
|
+
standard_baseline: np.ndarray | None = None
|
|
94
|
+
calibration_count: int = 0
|
|
95
|
+
collecting: bool = False
|
|
96
|
+
fps: float = 0
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class FaceEngine:
|
|
100
|
+
def __init__(self, model_dir=None, threads=2):
|
|
101
|
+
# 先准备本地资源;pip 版首次缺少缓存时由 BOS 下载,显式目录始终只读本地。
|
|
102
|
+
files = ModelFiles(prepare_models() if model_dir is None else model_dir)
|
|
103
|
+
files.validate(verify_hashes=True)
|
|
104
|
+
import mediapipe as mp
|
|
105
|
+
import onnxruntime as ort
|
|
106
|
+
|
|
107
|
+
self._mp = mp
|
|
108
|
+
self._last_timestamp = -1
|
|
109
|
+
options = ort.SessionOptions()
|
|
110
|
+
options.intra_op_num_threads = threads
|
|
111
|
+
options.inter_op_num_threads = 1
|
|
112
|
+
options.add_session_config_entry('session.intra_op.allow_spinning', '0')
|
|
113
|
+
self._encoder = ort.InferenceSession(str(files['LibreFace_AU_Encoder.onnx']), options,
|
|
114
|
+
providers=['CPUExecutionProvider'])
|
|
115
|
+
self._intensity = ort.InferenceSession(str(files['LibreFace_AU_Intensity.onnx']), options,
|
|
116
|
+
providers=['CPUExecutionProvider'])
|
|
117
|
+
self._encoder_input = self._encoder.get_inputs()[0].name
|
|
118
|
+
self._intensity_input = self._intensity.get_inputs()[0].name
|
|
119
|
+
landmarker_options = mp.tasks.vision.FaceLandmarkerOptions(
|
|
120
|
+
# 从本地读成 bytes,也支持 Windows 中文目录,避开原生文件名编码差异。
|
|
121
|
+
base_options=mp.tasks.BaseOptions(model_asset_buffer=files['face_landmarker.task'].read_bytes(),
|
|
122
|
+
delegate=mp.tasks.BaseOptions.Delegate.CPU),
|
|
123
|
+
running_mode=mp.tasks.vision.RunningMode.VIDEO,
|
|
124
|
+
num_faces=1,
|
|
125
|
+
min_face_detection_confidence=0.5,
|
|
126
|
+
min_face_presence_confidence=0.5,
|
|
127
|
+
min_tracking_confidence=0.5,
|
|
128
|
+
)
|
|
129
|
+
self._landmarker = mp.tasks.vision.FaceLandmarker.create_from_options(landmarker_options)
|
|
130
|
+
|
|
131
|
+
def process(self, frame_bgr, timestamp=None):
|
|
132
|
+
if frame_bgr is None or frame_bgr.ndim != 3 or frame_bgr.shape[2] != 3:
|
|
133
|
+
raise ValueError('需要有效的 BGR 摄像头画面')
|
|
134
|
+
started = time.perf_counter()
|
|
135
|
+
now = time.monotonic() if timestamp is None else timestamp
|
|
136
|
+
timestamp_ms = max(self._last_timestamp + 1, int(now * 1000))
|
|
137
|
+
self._last_timestamp = timestamp_ms
|
|
138
|
+
rgb = np.ascontiguousarray(cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB))
|
|
139
|
+
result = self._landmarker.detect_for_video(
|
|
140
|
+
self._mp.Image(image_format=self._mp.ImageFormat.SRGB, data=rgb), timestamp_ms)
|
|
141
|
+
observation = Observation(frame=frame_bgr, timestamp=now)
|
|
142
|
+
if result.face_landmarks:
|
|
143
|
+
h, w = frame_bgr.shape[:2]
|
|
144
|
+
points = np.array([(point.x * w, point.y * h) for point in result.face_landmarks[0]])
|
|
145
|
+
tensor = image_tensor(aligned_face(frame_bgr, points))
|
|
146
|
+
features = self._encoder.run(None, {self._encoder_input: tensor})[0].reshape(1, -1)
|
|
147
|
+
output = self._intensity.run(None, {self._intensity_input: features})[0]
|
|
148
|
+
observation.points = points
|
|
149
|
+
observation.coordinates = face_coordinates(points)
|
|
150
|
+
observation.au = decode_intensities(output)
|
|
151
|
+
observation.status = '已识别人脸'
|
|
152
|
+
else:
|
|
153
|
+
observation.status = '未检测到人脸 · 请正对摄像头'
|
|
154
|
+
observation.inference_ms = (time.perf_counter() - started) * 1000
|
|
155
|
+
return observation
|
|
156
|
+
|
|
157
|
+
def close(self):
|
|
158
|
+
if self._landmarker is not None:
|
|
159
|
+
self._landmarker.close()
|
|
160
|
+
self._landmarker = None
|
|
161
|
+
|
|
162
|
+
def __enter__(self):
|
|
163
|
+
return self
|
|
164
|
+
|
|
165
|
+
def __exit__(self, *_):
|
|
166
|
+
self.close()
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""只使用标准库,让 py -3 自动转入随项目准备的 Python 3.12 环境。"""
|
|
2
|
+
|
|
3
|
+
import importlib.util
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def ensure_runtime(root=None):
|
|
10
|
+
root = Path(root) if root is not None else Path(__file__).resolve().parents[1]
|
|
11
|
+
environments = (
|
|
12
|
+
(root / '.venv', 'Scripts/python.exe' if sys.platform == 'win32' else 'bin/python'),
|
|
13
|
+
(root / 'runtime/macos_arm64', 'bin/python3.12'),
|
|
14
|
+
)
|
|
15
|
+
for environment, relative_executable in environments:
|
|
16
|
+
if Path(sys.prefix).resolve() == environment.resolve():
|
|
17
|
+
return
|
|
18
|
+
executable = environment / relative_executable
|
|
19
|
+
if executable.is_file():
|
|
20
|
+
arguments = sys.argv
|
|
21
|
+
specification = getattr(sys.modules.get('__main__'), '__spec__', None)
|
|
22
|
+
if specification is not None:
|
|
23
|
+
arguments = ['-m', specification.name, *sys.argv[1:]]
|
|
24
|
+
try:
|
|
25
|
+
status = subprocess.call([str(executable), *arguments])
|
|
26
|
+
except KeyboardInterrupt:
|
|
27
|
+
status = 130
|
|
28
|
+
raise SystemExit(status)
|
|
29
|
+
dependencies = ('numpy', 'cv2', 'mediapipe', 'onnxruntime', 'PySide6')
|
|
30
|
+
if sys.version_info[:2] == (3, 12) and all(importlib.util.find_spec(name) for name in dependencies):
|
|
31
|
+
return
|
|
32
|
+
raise SystemExit(
|
|
33
|
+
'请先双击 安装依赖_Windows.cmd,然后执行 py -3 学生主程序.py。\n'
|
|
34
|
+
'pip 安装版:请在 Python 3.12 中用 python -m pip install 安装完整 wheel 及依赖。\n'
|
|
35
|
+
'Mac 请解压完整的 Apple 芯片离线包,双击 启动实验_macOS.command。'
|
|
36
|
+
)
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""系统原生摄像头名称与序号;枚举不打开摄像头,扫描只在后台运行。"""
|
|
2
|
+
|
|
3
|
+
import ctypes
|
|
4
|
+
import queue
|
|
5
|
+
import sys
|
|
6
|
+
import threading
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from uuid import UUID
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class CameraDevice:
|
|
14
|
+
index: int
|
|
15
|
+
name: str
|
|
16
|
+
uid: str
|
|
17
|
+
|
|
18
|
+
@property
|
|
19
|
+
def label(self):
|
|
20
|
+
return f'摄像头 {self.index} · {self.name}'
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _macos_devices():
|
|
24
|
+
# 和OpenCV 4.11 AVFoundation后端相同:video + muxed,再按uniqueID排序。
|
|
25
|
+
ctypes.CDLL('/System/Library/Frameworks/Foundation.framework/Foundation')
|
|
26
|
+
ctypes.CDLL('/System/Library/Frameworks/AVFoundation.framework/AVFoundation')
|
|
27
|
+
objc = ctypes.CDLL('/usr/lib/libobjc.A.dylib')
|
|
28
|
+
objc.objc_getClass.argtypes = [ctypes.c_char_p]
|
|
29
|
+
objc.objc_getClass.restype = ctypes.c_void_p
|
|
30
|
+
objc.sel_registerName.argtypes = [ctypes.c_char_p]
|
|
31
|
+
objc.sel_registerName.restype = ctypes.c_void_p
|
|
32
|
+
address = ctypes.cast(objc.objc_msgSend, ctypes.c_void_p).value
|
|
33
|
+
|
|
34
|
+
def send(receiver, selector, result=ctypes.c_void_p, args=(), values=()):
|
|
35
|
+
method = ctypes.CFUNCTYPE(result, ctypes.c_void_p, ctypes.c_void_p, *args)(address)
|
|
36
|
+
return method(receiver, objc.sel_registerName(selector.encode()), *values)
|
|
37
|
+
|
|
38
|
+
pool = send(objc.objc_getClass(b'NSAutoreleasePool'), 'new')
|
|
39
|
+
try:
|
|
40
|
+
camera_class = objc.objc_getClass(b'AVCaptureDevice')
|
|
41
|
+
string_class = objc.objc_getClass(b'NSString')
|
|
42
|
+
devices = []
|
|
43
|
+
for media in (b'vide', b'muxx'):
|
|
44
|
+
media_type = send(string_class, 'stringWithUTF8String:', args=(ctypes.c_char_p,), values=(media,))
|
|
45
|
+
array = send(camera_class, 'devicesWithMediaType:', args=(ctypes.c_void_p,), values=(media_type,))
|
|
46
|
+
for offset in range(send(array, 'count', ctypes.c_ulong)):
|
|
47
|
+
device = send(array, 'objectAtIndex:', args=(ctypes.c_ulong,), values=(offset,))
|
|
48
|
+
name = send(send(device, 'localizedName'), 'UTF8String', ctypes.c_char_p).decode('utf-8')
|
|
49
|
+
uid = send(send(device, 'uniqueID'), 'UTF8String', ctypes.c_char_p).decode('utf-8')
|
|
50
|
+
devices.append(CameraDevice(len(devices), name, uid))
|
|
51
|
+
return [CameraDevice(index, device.name, device.uid)
|
|
52
|
+
for index, device in enumerate(sorted(devices, key=lambda device: device.uid))]
|
|
53
|
+
finally:
|
|
54
|
+
send(pool, 'drain', None)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _windows_devices():
|
|
58
|
+
# DirectShow的VideoInputDeviceCategory,顺序与cv2.CAP_DSHOW一致。
|
|
59
|
+
ole = ctypes.OleDLL('ole32')
|
|
60
|
+
automation = ctypes.OleDLL('oleaut32')
|
|
61
|
+
pointer = ctypes.c_void_p
|
|
62
|
+
hresult = ctypes.c_long
|
|
63
|
+
guid_type = ctypes.c_ubyte * 16
|
|
64
|
+
|
|
65
|
+
def guid(value):
|
|
66
|
+
return guid_type.from_buffer_copy(UUID(value).bytes_le)
|
|
67
|
+
|
|
68
|
+
def call(instance, slot, argument_types, *arguments):
|
|
69
|
+
table = ctypes.cast(instance, ctypes.POINTER(ctypes.POINTER(pointer))).contents
|
|
70
|
+
method = ctypes.WINFUNCTYPE(hresult, pointer, *argument_types)(table[slot])
|
|
71
|
+
return method(instance, *arguments)
|
|
72
|
+
|
|
73
|
+
def release(instance):
|
|
74
|
+
if instance.value:
|
|
75
|
+
call(instance, 2, ())
|
|
76
|
+
|
|
77
|
+
def checked(result):
|
|
78
|
+
if result < 0:
|
|
79
|
+
raise OSError(f'DirectShow设备枚举失败:0x{result & 0xffffffff:08x}')
|
|
80
|
+
return result
|
|
81
|
+
|
|
82
|
+
ole.CoInitializeEx.argtypes = [pointer, ctypes.c_ulong]
|
|
83
|
+
ole.CoInitializeEx.restype = hresult
|
|
84
|
+
ole.CoUninitialize.argtypes = []
|
|
85
|
+
ole.CoCreateInstance.argtypes = [pointer, pointer, ctypes.c_ulong, pointer, ctypes.POINTER(pointer)]
|
|
86
|
+
ole.CoCreateInstance.restype = hresult
|
|
87
|
+
automation.VariantClear.argtypes = [pointer]
|
|
88
|
+
automation.VariantClear.restype = hresult
|
|
89
|
+
checked(ole.CoInitializeEx(None, 2))
|
|
90
|
+
enumerator, monikers = pointer(), pointer()
|
|
91
|
+
try:
|
|
92
|
+
checked(ole.CoCreateInstance(guid('62be5d10-60eb-11d0-bd3b-00a0c911ce86'), None, 1,
|
|
93
|
+
guid('29840822-5b84-11d0-bd3b-00a0c911ce86'), ctypes.byref(enumerator)))
|
|
94
|
+
result = checked(call(enumerator, 3, (pointer, ctypes.POINTER(pointer), ctypes.c_ulong),
|
|
95
|
+
guid('860bb310-5d01-11d0-bd3b-00a0c911ce86'), ctypes.byref(monikers), 0))
|
|
96
|
+
if result == 1: # S_FALSE:没有设备
|
|
97
|
+
return []
|
|
98
|
+
devices = []
|
|
99
|
+
while True:
|
|
100
|
+
moniker, bag = pointer(), pointer()
|
|
101
|
+
fetched = ctypes.c_ulong()
|
|
102
|
+
result = checked(call(monikers, 3, (ctypes.c_ulong, ctypes.POINTER(pointer),
|
|
103
|
+
ctypes.POINTER(ctypes.c_ulong)),
|
|
104
|
+
1, ctypes.byref(moniker), ctypes.byref(fetched)))
|
|
105
|
+
if result == 1:
|
|
106
|
+
break
|
|
107
|
+
try:
|
|
108
|
+
checked(call(moniker, 9, (pointer, pointer, pointer, ctypes.POINTER(pointer)),
|
|
109
|
+
None, None, guid('55272a00-42cb-11ce-8135-00aa004bb851'), ctypes.byref(bag)))
|
|
110
|
+
|
|
111
|
+
def read_property(name, property_bag=bag):
|
|
112
|
+
# VARIANT:x64为24字节,x86为16字节;BSTR在第8字节。
|
|
113
|
+
variant = ctypes.create_string_buffer(24 if ctypes.sizeof(pointer) == 8 else 16)
|
|
114
|
+
try:
|
|
115
|
+
hr = call(property_bag, 3, (ctypes.c_wchar_p, pointer, pointer), name, variant, None)
|
|
116
|
+
if hr >= 0 and ctypes.c_ushort.from_buffer(variant).value == 8: # VT_BSTR
|
|
117
|
+
text = pointer.from_buffer(variant, 8).value
|
|
118
|
+
return ctypes.wstring_at(text) if text else ''
|
|
119
|
+
return ''
|
|
120
|
+
finally:
|
|
121
|
+
automation.VariantClear(variant)
|
|
122
|
+
|
|
123
|
+
index = len(devices)
|
|
124
|
+
name = read_property('FriendlyName') or read_property('Description') or f'视频设备 {index}'
|
|
125
|
+
uid = read_property('DevicePath') or f'dshow:{index}:{name}'
|
|
126
|
+
devices.append(CameraDevice(index, name, uid))
|
|
127
|
+
finally:
|
|
128
|
+
release(bag)
|
|
129
|
+
release(moniker)
|
|
130
|
+
return devices
|
|
131
|
+
finally:
|
|
132
|
+
release(monikers)
|
|
133
|
+
release(enumerator)
|
|
134
|
+
ole.CoUninitialize()
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def discover_cameras(platform=None):
|
|
138
|
+
platform = sys.platform if platform is None else platform
|
|
139
|
+
if platform == 'darwin':
|
|
140
|
+
return _macos_devices()
|
|
141
|
+
if platform == 'win32':
|
|
142
|
+
return _windows_devices()
|
|
143
|
+
devices = []
|
|
144
|
+
for path in sorted(Path('/sys/class/video4linux').glob('video*')):
|
|
145
|
+
index = int(path.name.removeprefix('video'))
|
|
146
|
+
if Path(f'/dev/video{index}').exists():
|
|
147
|
+
devices.append(CameraDevice(index, (path / 'name').read_text().strip(), str(path.resolve())))
|
|
148
|
+
return devices
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class CameraMonitor(threading.Thread):
|
|
152
|
+
def __init__(self, scanner=discover_cameras, interval=3):
|
|
153
|
+
super().__init__(name='camera-device-monitor', daemon=True)
|
|
154
|
+
self.stop = threading.Event()
|
|
155
|
+
self.results = queue.Queue(maxsize=1)
|
|
156
|
+
self.scanner = scanner
|
|
157
|
+
self.interval = interval
|
|
158
|
+
|
|
159
|
+
def run(self):
|
|
160
|
+
while not self.stop.is_set():
|
|
161
|
+
try:
|
|
162
|
+
result = (tuple(self.scanner()), '')
|
|
163
|
+
except Exception as error: # noqa: BLE001 -- 系统设备枚举边界,交由界面显示。
|
|
164
|
+
result = ((), str(error))
|
|
165
|
+
if self.stop.is_set():
|
|
166
|
+
return
|
|
167
|
+
try:
|
|
168
|
+
self.results.get_nowait()
|
|
169
|
+
except queue.Empty:
|
|
170
|
+
pass
|
|
171
|
+
self.results.put_nowait(result)
|
|
172
|
+
self.stop.wait(self.interval)
|