更新接口

This commit is contained in:
2025-10-18 18:21:30 +08:00
parent 09c3117f12
commit cf60d96066
4 changed files with 663 additions and 330 deletions

356
main.py
View File

@@ -1,13 +1,16 @@
import sys
import os
import cv2
import time
import numpy as np
from collections import defaultdict, deque
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, \
QFileDialog, QFrame, QScrollArea, QComboBox
from PyQt5.QtCore import QTimer, Qt, pyqtSignal, QThread
QFileDialog, QFrame, QScrollArea, QComboBox, QListWidget, QListWidgetItem, QLineEdit, QMessageBox, QDialog, \
QDialogButtonBox, QFormLayout, QTextEdit
from PyQt5.QtCore import QTimer, Qt, pyqtSignal, QThread, QDateTime
from PyQt5.QtGui import QImage, QPixmap, QFont, QPainter, QPen, QColor
from yolopart.detector import LicensePlateYOLO
from gate_control import GateController, WhitelistManager
#选择使用哪个模块
# from LPRNET_part.lpr_interface import LPRNmodel_predict
@@ -19,6 +22,38 @@ from yolopart.detector import LicensePlateYOLO
# from CRNN_part.crnn_interface import LPRNmodel_predict
# from CRNN_part.crnn_interface import LPRNinitialize_model
class PlateInputDialog(QDialog):
"""车牌输入对话框"""
def __init__(self, title, default_text=""):
super().__init__()
self.setWindowTitle(title)
self.setFixedSize(300, 100)
self.setWindowModality(Qt.ApplicationModal)
layout = QVBoxLayout()
# 车牌输入框
self.plate_input = QLineEdit()
self.plate_input.setPlaceholderText("请输入车牌号")
self.plate_input.setText(default_text)
self.plate_input.setMaxLength(10)
# 按钮
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(QLabel("车牌号:"))
layout.addWidget(self.plate_input)
layout.addWidget(buttons)
self.setLayout(layout)
def get_plate_number(self):
"""获取输入的车牌号"""
return self.plate_input.text().strip()
class PlateStabilizer:
"""车牌识别结果稳定器"""
@@ -449,13 +484,27 @@ class MainWindow(QMainWindow):
stability_frames=5 # 需要5帧稳定
)
# 初始化道闸控制器和白名单管理器
self.gate_controller = GateController()
self.whitelist_manager = WhitelistManager()
# 记录车牌首次检测时间和上次发送指令时间
self.plate_first_detected = {} # 记录车牌首次检测时间
self.plate_last_command_time = {} # 记录车牌上次发送指令时间
self.init_ui()
self.init_detector()
self.init_camera()
self.init_video()
self.init_gate_control()
# 初始化默认识别方法CRNN的模型
self.change_recognition_method(self.current_recognition_method)
# 设置定时器每30秒清理一次过期的车牌记录
self.cleanup_timer = QTimer(self)
self.cleanup_timer.timeout.connect(self.cleanup_plate_records)
self.cleanup_timer.start(30000) # 30秒
def init_ui(self):
@@ -524,6 +573,156 @@ class MainWindow(QMainWindow):
right_frame.setStyleSheet("QFrame { background-color: #fafafa; border: 2px solid #ddd; }")
right_layout = QVBoxLayout(right_frame)
# 道闸控制区域
gate_frame = QFrame()
gate_frame.setFrameStyle(QFrame.StyledPanel)
gate_frame.setStyleSheet("QFrame { background-color: #f0f8ff; border: 1px solid #b0d4f1; border-radius: 5px; }")
gate_layout = QVBoxLayout(gate_frame)
# 道闸控制标题
gate_title = QLabel("道闸控制")
gate_title.setAlignment(Qt.AlignCenter)
gate_title.setFont(QFont("Arial", 14, QFont.Bold))
gate_title.setStyleSheet("QLabel { color: #1976d2; padding: 5px; }")
# 道闸控制按钮
gate_button_layout = QHBoxLayout()
self.open_gate_button = QPushButton("手动开闸")
self.close_gate_button = QPushButton("手动关闸")
self.open_gate_button.clicked.connect(self.manual_open_gate)
self.close_gate_button.clicked.connect(self.manual_close_gate)
# 设置道闸按钮样式
gate_button_style = """
QPushButton {
background-color: #4CAF50;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
font-weight: bold;
}
QPushButton:hover {
background-color: #45a049;
}
QPushButton:pressed {
background-color: #3d8b40;
}
"""
self.open_gate_button.setStyleSheet(gate_button_style)
close_button_style = """
QPushButton {
background-color: #f44336;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
font-weight: bold;
}
QPushButton:hover {
background-color: #d32f2f;
}
QPushButton:pressed {
background-color: #b71c1c;
}
"""
self.close_gate_button.setStyleSheet(close_button_style)
gate_button_layout.addWidget(self.open_gate_button)
gate_button_layout.addWidget(self.close_gate_button)
# 白名单管理区域
whitelist_layout = QVBoxLayout()
whitelist_label = QLabel("车牌白名单")
whitelist_label.setFont(QFont("Arial", 12, QFont.Bold))
whitelist_label.setStyleSheet("QLabel { color: #333; padding: 5px; }")
# 白名单按钮
whitelist_button_layout = QHBoxLayout()
self.add_plate_button = QPushButton("添加车牌")
self.edit_plate_button = QPushButton("编辑车牌")
self.delete_plate_button = QPushButton("删除车牌")
self.add_plate_button.clicked.connect(self.add_plate_to_whitelist)
self.edit_plate_button.clicked.connect(self.edit_plate_in_whitelist)
self.delete_plate_button.clicked.connect(self.delete_plate_from_whitelist)
# 设置白名单按钮样式
whitelist_button_style = """
QPushButton {
background-color: #2196F3;
color: white;
border: none;
padding: 6px 12px;
border-radius: 4px;
font-weight: bold;
font-size: 11px;
}
QPushButton:hover {
background-color: #1976D2;
}
QPushButton:pressed {
background-color: #0D47A1;
}
"""
self.add_plate_button.setStyleSheet(whitelist_button_style)
self.edit_plate_button.setStyleSheet(whitelist_button_style)
self.delete_plate_button.setStyleSheet(whitelist_button_style)
whitelist_button_layout.addWidget(self.add_plate_button)
whitelist_button_layout.addWidget(self.edit_plate_button)
whitelist_button_layout.addWidget(self.delete_plate_button)
# 白名单列表
self.whitelist_list = QListWidget()
self.whitelist_list.setMaximumHeight(120)
self.whitelist_list.setStyleSheet("""
QListWidget {
border: 1px solid #ddd;
background-color: white;
border-radius: 4px;
padding: 5px;
}
QListWidget::item {
padding: 5px;
border-bottom: 1px solid #eee;
}
QListWidget::item:selected {
background-color: #e3f2fd;
color: #1976d2;
}
""")
# 调试日志区域
log_label = QLabel("调试日志")
log_label.setFont(QFont("Arial", 10, QFont.Bold))
log_label.setStyleSheet("QLabel { color: #333; padding: 5px; }")
self.log_text = QTextEdit()
self.log_text.setMaximumHeight(100)
self.log_text.setReadOnly(True)
self.log_text.setStyleSheet("""
QTextEdit {
border: 1px solid #ddd;
background-color: #f9f9f9;
border-radius: 4px;
padding: 5px;
font-family: 'Consolas', 'Courier New', monospace;
font-size: 10px;
}
""")
# 添加到道闸控制布局
whitelist_layout.addWidget(whitelist_label)
whitelist_layout.addLayout(whitelist_button_layout)
whitelist_layout.addWidget(self.whitelist_list)
gate_layout.addWidget(gate_title)
gate_layout.addLayout(gate_button_layout)
gate_layout.addLayout(whitelist_layout)
gate_layout.addWidget(log_label)
gate_layout.addWidget(self.log_text)
# 标题
title_label = QLabel("检测结果")
title_label.setAlignment(Qt.AlignCenter)
@@ -576,6 +775,7 @@ class MainWindow(QMainWindow):
self.current_method_label.setFont(QFont("Arial", 9))
self.current_method_label.setStyleSheet("QLabel { color: #666; padding: 5px; }")
right_layout.addWidget(gate_frame)
right_layout.addWidget(title_label)
right_layout.addLayout(method_layout)
right_layout.addWidget(self.count_label)
@@ -1014,6 +1214,13 @@ class MainWindow(QMainWindow):
# 更新存储的结果
self.last_plate_results = stable_results
# 处理道闸控制逻辑
for result in stable_results:
plate_number = result.get('plate_number', '')
if plate_number and plate_number != "识别失败":
# 调用道闸控制逻辑
self.process_gate_control(plate_number)
# 清理旧的车牌记录
current_plate_ids = [result['id'] for result in stable_results]
@@ -1111,6 +1318,151 @@ class MainWindow(QMainWindow):
if self.current_frame is not None:
self.process_frame(self.current_frame)
def init_gate_control(self):
"""初始化道闸控制功能"""
# 更新白名单列表显示
self.update_whitelist_display()
# 添加初始日志
self.add_log("道闸控制系统已初始化")
# GateController的IP地址在初始化时已设置默认为192.168.43.12
def manual_open_gate(self):
"""手动开闸"""
self.gate_controller.manual_open_gate()
self.add_log("手动开闸指令已发送")
def manual_close_gate(self):
"""手动关闸"""
self.gate_controller.manual_close_gate()
self.add_log("手动关闸指令已发送")
def cleanup_plate_records(self):
"""清理过期的车牌记录"""
current_time = time.time()
# 清理超过30秒的首次检测记录
expired_plates = []
for plate, first_time in self.plate_first_detected.items():
if current_time - first_time > 30:
expired_plates.append(plate)
for plate in expired_plates:
del self.plate_first_detected[plate]
self.add_log(f"清理过期的首次检测记录: {plate}")
# 清理超过1小时的指令发送记录
expired_commands = []
for plate, last_time in self.plate_last_command_time.items():
if current_time - last_time > 3600:
expired_commands.append(plate)
for plate in expired_commands:
del self.plate_last_command_time[plate]
self.add_log(f"清理过期的指令记录: {plate}")
def add_plate_to_whitelist(self):
"""添加车牌到白名单"""
dialog = PlateInputDialog("添加车牌", "")
if dialog.exec_() == QDialog.Accepted:
plate_number = dialog.get_plate_number()
if plate_number:
self.whitelist_manager.add_plate(plate_number)
self.update_whitelist_display()
self.add_log(f"已添加车牌到白名单: {plate_number}")
def edit_plate_in_whitelist(self):
"""编辑白名单中的车牌"""
current_item = self.whitelist_list.currentItem()
if not current_item:
QMessageBox.warning(self, "提示", "请先选择要编辑的车牌")
return
old_plate = current_item.text()
dialog = PlateInputDialog("编辑车牌", old_plate)
if dialog.exec_() == QDialog.Accepted:
new_plate = dialog.get_plate_number()
if new_plate and new_plate != old_plate:
self.whitelist_manager.remove_plate(old_plate)
self.whitelist_manager.add_plate(new_plate)
self.update_whitelist_display()
self.add_log(f"已修改车牌: {old_plate} -> {new_plate}")
def delete_plate_from_whitelist(self):
"""从白名单中删除车牌"""
current_item = self.whitelist_list.currentItem()
if not current_item:
QMessageBox.warning(self, "提示", "请先选择要删除的车牌")
return
plate = current_item.text()
reply = QMessageBox.question(self, "确认", f"确定要删除车牌 {plate} 吗?",
QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
self.whitelist_manager.remove_plate(plate)
self.update_whitelist_display()
self.add_log(f"已从白名单删除车牌: {plate}")
def update_whitelist_display(self):
"""更新白名单列表显示"""
self.whitelist_list.clear()
for plate in self.whitelist_manager.get_whitelist():
self.whitelist_list.addItem(plate)
def add_log(self, message):
"""添加日志消息"""
current_time = QDateTime.currentDateTime().toString("hh:mm:ss")
log_message = f"[{current_time}] {message}"
self.log_text.append(log_message)
# 限制日志行数,避免内存占用过多
if self.log_text.document().blockCount() > 100:
cursor = self.log_text.textCursor()
cursor.movePosition(cursor.Start)
cursor.select(cursor.BlockUnderCursor)
cursor.removeSelectedText()
cursor.deleteChar() # 删除换行符
def process_gate_control(self, plate_number):
"""处理道闸控制逻辑"""
# 检查车牌是否在白名单中
if self.whitelist_manager.is_whitelisted(plate_number):
current_time = time.time()
# 检查是否在10秒内已发送过指令
if plate_number in self.plate_last_command_time:
time_since_last_command = current_time - self.plate_last_command_time[plate_number]
if time_since_last_command < 10: # 10秒内不再发送指令
self.add_log(f"车牌 {plate_number} 在10秒内已发送过指令跳过")
return
# 记录车牌首次检测时间
if plate_number not in self.plate_first_detected:
self.plate_first_detected[plate_number] = current_time
self.add_log(f"车牌 {plate_number} 首次检测等待2秒稳定确认")
return
# 检查是否已稳定2秒
time_since_first_detected = current_time - self.plate_first_detected[plate_number]
if time_since_first_detected >= 2: # 稳定2秒后发送指令
# 使用GateController的auto_open_gate方法它会自动处理时间差
self.gate_controller.auto_open_gate(plate_number)
self.add_log(f"车牌 {plate_number} 验证通过,已发送开闸指令")
# 更新上次发送指令时间
self.plate_last_command_time[plate_number] = current_time
# 清除首次检测时间,以便下次重新检测
if plate_number in self.plate_first_detected:
del self.plate_first_detected[plate_number]
else:
# 还未稳定2秒继续等待
self.add_log(f"车牌 {plate_number} 检测中,已等待 {time_since_first_detected:.1f}")
else:
# 不在白名单中,发送禁行指令
self.gate_controller.deny_access(plate_number)
self.add_log(f"车牌 {plate_number} 不在白名单中,已发送禁行指令")
def closeEvent(self, event):
"""窗口关闭事件"""
if self.camera_thread and self.camera_thread.running: