实现精细化人脸识别功能,人脸交互详细, UI部分还需要优化

This commit is contained in:
冯佳
2025-09-15 16:03:17 +08:00
parent babab70845
commit 7205a21a8d
6 changed files with 220 additions and 124 deletions

View File

@ -453,6 +453,7 @@ class QFaceCameraViewPage(PageTemplate):
self.t_video.start()
self.face_verify_result = None # 用于存储人脸验证结果
self.verify_in_progress = False # 标志当前是否有验证在进行中
self.auto_connect_serial()
@ -541,36 +542,104 @@ class QFaceCameraViewPage(PageTemplate):
self.log("[WARN] 摄像头线程已停止")
self.video_worker=None # 自动重连可在这里实现
# 帧处理
def on_frame(self, fr: dict):
self.log("< " + fr["raw"].hex(" "))
"""
接收到一帧数据后的处理
"""
raw_bytes = fr.get("raw", b"")
self.log("< " + raw_bytes.hex(" "))
msg_id = fr.get("msg_id")
data = fr.get("data", b"")
if msg_id == MID_REPLY:
info = parse_reply(data)
self.log(f"[REPLY] {info}")
if info.get("mid") == CMD_ENROLL_ITG:
if info.get("result") == 0x00:
user_id = info.get("user_id")
if user_id:
if save_user(user_id):
self.log(f"[INFO] 用户ID={user_id} 已保存")
else:
inform_box : DialogInform = DialogInform()
inform_box.information("提示", f"用户ID {user_id} 已存在!")
elif info.get("mid") == CMD_VERIFY :
if info.get("result") == 0x00:
user_id = info.get("user_id")
print(f"[INFO] 用户(ID={user_id}) 验证通过")
self.face_verify_result = True
else:
self.face_verify_result = False
mid = info.get("mid")
result = info.get("result")
# 命令分发字典
dispatch_reply = {
CMD_ENROLL_SINGLE: self._handle_enroll_reply,
CMD_VERIFY: self._handle_verify_reply,
CMD_GET_ALL_USERID :self._handle_get_all_userid_reply,
}
handler = dispatch_reply.get(mid)
if handler:
handler(info)
elif msg_id == MID_NOTE:
info = parse_note(data)
self.log(f"[NOTE] {info}")
self._handle_note(info)
# ---------------- NOTE 处理 ----------------
def _handle_note(self, info: dict):
"""
处理 NOTE 消息,给用户交互提示
"""
user_message = info.get("user_message")
nid = info.get("nid")
# 仅在验证进行中时提示 NOTE
if getattr(self, "verify_in_progress", False):
if user_message:
DialogInform(self).information("提示", user_message)
# 日志记录(工程用)
if nid == NID_FACE_STATE:
state = info.get("state")
self.log(f"[INFO] 人脸状态: {state}, yaw={info.get('yaw')}, pitch={info.get('pitch')}, roll={info.get('roll')}")
elif nid == NID_READY:
self.log("[INFO] 模组已就绪")
elif nid == NID_OTA_DONE:
self.log("[INFO] 固件升级完成")
elif nid == NID_UNKNOWNERROR:
self.log("[ERROR] 模组发生未知错误")
# ---------------- REPLY 处理 ----------------
def _handle_enroll_reply(self, info: dict):
user_id = info.get("user_id")
if info.get("result") == 0x00:
if user_id:
if save_user(user_id):
self.refresh()
self.log(f"[INFO] 用户ID={user_id} 已保存")
else:
DialogInform(self).information("提示", f"用户ID {user_id} 已存在!")
else:
self.log(f"[ERROR] 注册失败, 用户ID={user_id}")
def _handle_verify_reply(self, info: dict):
"""
验证结果处理
"""
user_id = info.get("user_id")
result = info.get("result")
# 验证结束,状态机复位
self.verify_in_progress = False
if result == 0x00:
print(f"[INFO] 用户(ID={user_id}) 验证通过")
self.face_verify_result = True
else:
print(f"[INFO] 用户(ID={user_id}) 验证失败")
self.face_verify_result = False
def _handle_get_all_userid_reply(self, info: dict):
count = info.get("count")
result = info.get("result")
user_ids = info.get("user_ids", [])
if result == 0x00:
DialogInform(self).information("提示", f"{count} 个用户\n用户ID 列表:" + ", ".join(map(str, user_ids)))
# 日志
def log(self, s: str):
@ -595,8 +664,6 @@ class QFaceCameraViewPage(PageTemplate):
self.close_serial()
super().closeEvent(e)
#P06故障查询页面 QFaultQueryPage
class QFaultQueryPage(PageTemplate):
def __init__(self, parent_window):
@ -774,7 +841,7 @@ class APPWindow(QMainWindow):
QFaceCameraViewPage.video_worker = None
QFaceCameraViewPage.ser = None
self.showFullScreen()
self.menu_sequence_list : PageTemplate = []

View File

@ -3037,7 +3037,7 @@ color: rgb(255, 170, 0);</string>
</rect>
</property>
<property name="statusTip">
<string>Index=7, Action=SetPage5_1,SelectImag=IMxx_00D.png</string>
<string>Index=7, Action=SetPage5_1,SelectImag=IMxx_00D.png,password</string>
</property>
<property name="text">
<string/>

View File

@ -91,39 +91,13 @@
<property name="flat">
<bool>false</bool>
</property>
<widget class="QPushButton" name="btn_save">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>211</width>
<height>41</height>
</rect>
</property>
<property name="statusTip">
<string/>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 0, 0);
color: rgb(170, 0, 0);</string>
</property>
<property name="text">
<string>日志</string>
</property>
<property name="iconSize">
<size>
<width>72</width>
<height>144</height>
</size>
</property>
</widget>
<widget class="QTextEdit" name="txt_log">
<property name="geometry">
<rect>
<x>-20</x>
<y>60</y>
<y>20</y>
<width>701</width>
<height>301</height>
<height>341</height>
</rect>
</property>
</widget>
@ -182,7 +156,7 @@ color: rgb(170, 0, 0);</string>
</rect>
</property>
<property name="statusTip">
<string/>
<string>mian=51</string>
</property>
<property name="title">
<string>命令</string>
@ -197,7 +171,7 @@ color: rgb(170, 0, 0);</string>
</rect>
</property>
<property name="statusTip">
<string>Index=0, Action=Reset,SelectImag=IMxx_00F.png</string>
<string>Index=0, Action=Reset,SelectImag=IMxx_00F.png,groupstart=8</string>
</property>
<property name="text">
<string>复位</string>
@ -213,65 +187,33 @@ color: rgb(170, 0, 0);</string>
</rect>
</property>
<property name="statusTip">
<string>Index=0, Action=VideoMode,SelectImag=IMxx_00F.png</string>
<string>Index=1, Action=VideoMode,SelectImag=IMxx_00F.png</string>
</property>
<property name="text">
<string>视频模式</string>
</property>
</widget>
<widget class="QPushButton" name="btn_verify">
<property name="geometry">
<rect>
<x>12</x>
<y>115</y>
<width>80</width>
<height>23</height>
</rect>
</property>
<property name="statusTip">
<string>Index=0, Action=Verify,password,SelectImag=IMxx_00F.png</string>
</property>
<property name="text">
<string>识别</string>
</property>
</widget>
<widget class="QPushButton" name="btn_enroll">
<property name="geometry">
<rect>
<x>12</x>
<y>144</y>
<x>10</x>
<y>210</y>
<width>80</width>
<height>23</height>
</rect>
</property>
<property name="statusTip">
<string>Index=0, Action=EnrollItgSingle,SelectImag=IMxx_00F.png</string>
<string>Index=5, Action=EnrollItgSingle,SelectImag=IMxx_00F.png</string>
</property>
<property name="text">
<string>ITG注册</string>
</property>
</widget>
<widget class="QPushButton" name="btn_users">
<property name="geometry">
<rect>
<x>12</x>
<y>173</y>
<width>80</width>
<height>23</height>
</rect>
</property>
<property name="statusTip">
<string>Index=0, Action=Users,SelectImag=IMxx_00F.png</string>
</property>
<property name="text">
<string>用户管理</string>
</property>
</widget>
<widget class="QLineEdit" name="FaceRecogTimeoutEdit">
<property name="geometry">
<rect>
<x>0</x>
<y>310</y>
<x>-10</x>
<y>180</y>
<width>120</width>
<height>25</height>
</rect>
@ -286,7 +228,7 @@ color: rgb(170, 0, 0);</string>
<enum>Qt::NoFocus</enum>
</property>
<property name="statusTip">
<string>System=FaceRecogTimeout, Action=ModifySystem, Index=13,SelectImag=P4_ParaSelect.png, password</string>
<string>System=FaceRecogTimeout, Action=ModifySystem, Index=4,SelectImag=P4_ParaSelect.png, password</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgba(85, 170, 127,0);
@ -306,13 +248,13 @@ color: rgb(177, 229, 252);</string>
<property name="geometry">
<rect>
<x>10</x>
<y>210</y>
<y>90</y>
<width>80</width>
<height>23</height>
</rect>
</property>
<property name="statusTip">
<string>Index=0, Action=ConnectCamera,password,SelectImag=IMxx_00F.png</string>
<string>Index=2, Action=ConnectCamera,SelectImag=IMxx_00F.png</string>
</property>
<property name="text">
<string>打开视频</string>
@ -322,13 +264,13 @@ color: rgb(177, 229, 252);</string>
<property name="geometry">
<rect>
<x>10</x>
<y>90</y>
<y>120</y>
<width>80</width>
<height>23</height>
</rect>
</property>
<property name="statusTip">
<string>Index=0, Action=FaceBox,SelectImag=IMxx_00F.png</string>
<string>Index=3, Action=FaceBox,SelectImag=IMxx_00F.png</string>
</property>
<property name="text">
<string>人脸框</string>
@ -344,12 +286,28 @@ color: rgb(177, 229, 252);</string>
</rect>
</property>
<property name="statusTip">
<string>Index=0, Action=DeleteUser,SelectImag=IMxx_00F.png</string>
<string>Index=6, Action=DeleteUser,SelectImag=IMxx_00F.png</string>
</property>
<property name="text">
<string>删除用户</string>
</property>
</widget>
<widget class="QPushButton" name="btn_delete_user_id_2">
<property name="geometry">
<rect>
<x>10</x>
<y>270</y>
<width>80</width>
<height>23</height>
</rect>
</property>
<property name="statusTip">
<string>Index=7, Action=UserCount,SelectImag=IMxx_00F.png,groupend=8</string>
</property>
<property name="text">
<string>查看用户</string>
</property>
</widget>
</widget>
<widget class="QLabel" name="P05_01BG">
<property name="geometry">

View File

@ -42,7 +42,7 @@ import serial.tools.list_ports
from Shared_CODE.FaceRecognitionProtocol import (
build_reset, build_uvc_view, build_face_view, build_verify,
build_enroll_itg_single, build_delete_all, build_get_all_userid,
build_delete_user, unpack_frame, parse_reply, parse_note,
build_delete_user, unpack_frame, parse_reply, parse_note,build_enroll_single,
MID_REPLY, MID_NOTE, CMD_ENROLL, CMD_ENROLL_ITG
)
@ -829,8 +829,6 @@ class UIFrameWork(QMainWindow, class_comm_mqtt_interface):
self._face_verify_locked = False # 解锁
if face_frame.face_verify_result:
input = True
inform_box = DialogInform()
inform_box.information("提示", "人脸认证成功")
self._after_face_verify(select_object, action_str)
else:
input = False
@ -840,8 +838,6 @@ class UIFrameWork(QMainWindow, class_comm_mqtt_interface):
self._face_verify_timer.stop()
self._face_verify_locked = False # 解锁
input = False
inform_box = DialogInform()
inform_box.information("提示", "人脸认证超时")
# 解绑旧槽,绑定新槽
try:
self._face_verify_timer.timeout.disconnect()
@ -963,16 +959,17 @@ class UIFrameWork(QMainWindow, class_comm_mqtt_interface):
pd_val = 0
timeout_val = system_parameter().get_verify_timeout()
face_send = self.parent_window.P05_01_FaceCameraView
face_send.verify_in_progress = True
face_send.send(build_verify(pd_val, timeout_val))
def do_enroll_itg_single(self):
admin_val = 0
uname = " "
face_dir = 31
timeout_val = system_parameter().get_verify_timeout()
itg_val = 0
self.send(build_enroll_itg_single(admin_val, uname, face_dir, timeout_val, itg_val))
face_send = self.parent_window.P05_01_FaceCameraView
face_send.verify_in_progress = True
self.send(build_enroll_single(admin_val, uname, timeout_val))
def do_manage_users(self):
UserManageDialog(self, self.send).exec_()
@ -1046,7 +1043,7 @@ class UIFrameWork(QMainWindow, class_comm_mqtt_interface):
else:
self.log("[WARN] 串口未连接,无法控制人脸框")
def delete_user_by_id(self, CSV_FILE = CSV_FILE):
def delete_user_by_id(self, CSV_FILE=CSV_FILE):
users = load_users()
# 弹出对话框选择用户ID
@ -1064,22 +1061,27 @@ class UIFrameWork(QMainWindow, class_comm_mqtt_interface):
DialogInform(self).information("提示", "请输入有效数字ID")
return
# 查找用户
user = next((u for u in users if u["user_id"] == user_id), None)
user_id_str = str(user_id)
# 查找用户(用字符串匹配,避免类型问题)
user = next((u for u in users if str(u["user_id"]).strip() == user_id_str), None)
if not user:
DialogInform(self).information("提示", "用户不存在")
return
try:
# 1⃣ 下发删除命令
self.send(build_delete_user(user_id))
# 1⃣ 下发删除命令(串口模块)
try:
self.send(build_delete_user(user_id))
except Exception as e:
DialogInform(self).information("提示", f"警告:串口删除失败,但本地仍会删除\n{e}")
# 2⃣ 删除 CSV 文件对应行
if os.path.exists(CSV_FILE):
with open(CSV_FILE, "r", encoding="utf-8", newline="") as f:
reader = csv.DictReader(f)
fieldnames = reader.fieldnames
new_rows = [row for row in reader if str(row.get("user_id")) != str(user_id)]
new_rows = [row for row in reader if str(row.get("user_id")).strip() != user_id_str]
with open(CSV_FILE, "w", encoding="utf-8", newline="") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
@ -1091,12 +1093,12 @@ class UIFrameWork(QMainWindow, class_comm_mqtt_interface):
return
# 3⃣ 更新内存用户列表
users = [u for u in users if u["user_id"] != user_id]
users = [u for u in users if str(u["user_id"]).strip() != user_id_str]
save_users_list(users)
# 4⃣ 提示删除成功
DialogInform(self).information("提示", f"用户 {user.get('user_name', '')} (ID={user_id}) 已删除")
self.refresh()
def query_user_count(self):
self.send(build_get_all_userid())