1523 lines
		
	
	
		
			59 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			1523 lines
		
	
	
		
			59 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import sys
 | 
						||
import os
 | 
						||
import cv2
 | 
						||
import numpy as np
 | 
						||
import time
 | 
						||
from datetime import datetime
 | 
						||
from collections import defaultdict, deque
 | 
						||
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, \
 | 
						||
    QFileDialog, QFrame, QScrollArea, QComboBox, QLineEdit, QListWidget, QListWidgetItem
 | 
						||
from PyQt5.QtCore import QTimer, Qt, pyqtSignal, QThread
 | 
						||
from PyQt5.QtGui import QImage, QPixmap, QFont, QPainter, QPen, QColor
 | 
						||
from yolopart.detector import LicensePlateYOLO
 | 
						||
from communicate import send_command
 | 
						||
 | 
						||
#选择使用哪个模块
 | 
						||
# from LPRNET_part.lpr_interface import LPRNmodel_predict
 | 
						||
# from LPRNET_part.lpr_interface import LPRNinitialize_model
 | 
						||
#使用OCR
 | 
						||
# from OCR_part.ocr_interface import LPRNmodel_predict
 | 
						||
# from OCR_part.ocr_interface import LPRNinitialize_model
 | 
						||
# 使用CRNN
 | 
						||
# from CRNN_part.crnn_interface import LPRNmodel_predict
 | 
						||
# from CRNN_part.crnn_interface import LPRNinitialize_model
 | 
						||
 | 
						||
class PlateStabilizer:
 | 
						||
    """车牌识别结果稳定器"""
 | 
						||
    
 | 
						||
    def __init__(self, history_size=10, confidence_threshold=0.6, stability_frames=10):
 | 
						||
        self.history_size = history_size  # 历史帧数量
 | 
						||
        self.confidence_threshold = confidence_threshold  # 置信度阈值
 | 
						||
        self.stability_frames = stability_frames  # 稳定帧数要求
 | 
						||
        
 | 
						||
        # 存储每个车牌的历史识别结果
 | 
						||
        self.plate_histories = defaultdict(lambda: deque(maxlen=history_size))
 | 
						||
        # 存储当前稳定的车牌结果
 | 
						||
        self.stable_results = {}
 | 
						||
        # 车牌ID计数器
 | 
						||
        self.plate_id_counter = 0
 | 
						||
        # 车牌位置追踪
 | 
						||
        self.plate_positions = {}
 | 
						||
        # 车牌最后检测时间戳
 | 
						||
        self.plate_last_seen = {}
 | 
						||
        # 车牌帧计数器,用于跟踪每个车牌被检测的帧数
 | 
						||
        self.plate_frame_count = {}
 | 
						||
        
 | 
						||
    def calculate_plate_distance(self, pos1, pos2):
 | 
						||
        """计算两个车牌位置的距离"""
 | 
						||
        if pos1 is None or pos2 is None:
 | 
						||
            return float('inf')
 | 
						||
        
 | 
						||
        # 计算中心点距离
 | 
						||
        center1 = ((pos1[0] + pos1[2]) / 2, (pos1[1] + pos1[3]) / 2)
 | 
						||
        center2 = ((pos2[0] + pos2[2]) / 2, (pos2[1] + pos2[3]) / 2)
 | 
						||
        
 | 
						||
        return np.sqrt((center1[0] - center2[0])**2 + (center1[1] - center2[1])**2)
 | 
						||
    
 | 
						||
    def match_plates_to_history(self, current_detections):
 | 
						||
        """将当前检测结果匹配到历史记录"""
 | 
						||
        import time
 | 
						||
        current_time = time.time()
 | 
						||
        matched_plates = {}
 | 
						||
        used_ids = set()
 | 
						||
        
 | 
						||
        for detection in current_detections:
 | 
						||
            bbox = detection.get('bbox', [0, 0, 0, 0])
 | 
						||
            best_match_id = None
 | 
						||
            min_distance = float('inf')
 | 
						||
            
 | 
						||
            # 寻找最佳匹配的历史车牌
 | 
						||
            for plate_id, last_pos in self.plate_positions.items():
 | 
						||
                if plate_id in used_ids:
 | 
						||
                    continue
 | 
						||
                
 | 
						||
                # 检查时间间隔,如果超过1秒则不匹配
 | 
						||
                last_seen_time = self.plate_last_seen.get(plate_id, 0)
 | 
						||
                if current_time - last_seen_time > 1.0:  # 超过1秒不匹配
 | 
						||
                    continue
 | 
						||
                    
 | 
						||
                distance = self.calculate_plate_distance(bbox, last_pos)
 | 
						||
                if distance < min_distance and distance < 50:  # 降低距离阈值,提高匹配精度
 | 
						||
                    min_distance = distance
 | 
						||
                    best_match_id = plate_id
 | 
						||
            
 | 
						||
            if best_match_id is not None:
 | 
						||
                matched_plates[best_match_id] = detection
 | 
						||
                used_ids.add(best_match_id)
 | 
						||
                self.plate_positions[best_match_id] = bbox
 | 
						||
                self.plate_last_seen[best_match_id] = current_time  # 更新时间戳
 | 
						||
                # 增加帧计数
 | 
						||
                self.plate_frame_count[best_match_id] = self.plate_frame_count.get(best_match_id, 0) + 1
 | 
						||
            else:
 | 
						||
                # 创建新的车牌ID
 | 
						||
                new_id = f"plate_{self.plate_id_counter}"
 | 
						||
                self.plate_id_counter += 1
 | 
						||
                matched_plates[new_id] = detection
 | 
						||
                self.plate_positions[new_id] = bbox
 | 
						||
                self.plate_last_seen[new_id] = current_time  # 设置时间戳
 | 
						||
                # 初始化帧计数
 | 
						||
                self.plate_frame_count[new_id] = 1
 | 
						||
        
 | 
						||
        return matched_plates
 | 
						||
    
 | 
						||
    def calculate_confidence(self, plate_text, detection_quality=1.0):
 | 
						||
        """计算识别结果的置信度"""
 | 
						||
        if not plate_text or plate_text == "识别失败":
 | 
						||
            return 0.0
 | 
						||
        
 | 
						||
        # 基础置信度基于文本长度和字符类型
 | 
						||
        base_confidence = 0.5
 | 
						||
        
 | 
						||
        # 长度合理性检查
 | 
						||
        if 7 <= len(plate_text) <= 8:
 | 
						||
            base_confidence += 0.2
 | 
						||
        
 | 
						||
        # 字符类型检查(中文+字母+数字的组合)
 | 
						||
        has_chinese = any('\u4e00' <= char <= '\u9fff' for char in plate_text)
 | 
						||
        has_letter = any(char.isalpha() for char in plate_text)
 | 
						||
        has_digit = any(char.isdigit() for char in plate_text)
 | 
						||
        
 | 
						||
        if has_chinese and has_letter and has_digit:
 | 
						||
            base_confidence += 0.2
 | 
						||
        
 | 
						||
        # 检测质量影响
 | 
						||
        confidence = base_confidence * detection_quality
 | 
						||
        
 | 
						||
        return min(confidence, 1.0)
 | 
						||
    
 | 
						||
    def update_and_get_stable_result(self, current_detections, corrected_images, plate_texts):
 | 
						||
        """更新历史记录并返回稳定的识别结果"""
 | 
						||
        if not current_detections:
 | 
						||
            return []
 | 
						||
        
 | 
						||
        # 匹配当前检测到历史记录
 | 
						||
        matched_plates = self.match_plates_to_history(current_detections)
 | 
						||
        
 | 
						||
        stable_results = []
 | 
						||
        
 | 
						||
        for plate_id, detection in matched_plates.items():
 | 
						||
            # 获取对应的矫正图像和识别文本
 | 
						||
            detection_idx = current_detections.index(detection)
 | 
						||
            corrected_image = corrected_images[detection_idx] if detection_idx < len(corrected_images) else None
 | 
						||
            plate_text = plate_texts[detection_idx] if detection_idx < len(plate_texts) else "识别失败"
 | 
						||
            
 | 
						||
            # 计算置信度
 | 
						||
            confidence = self.calculate_confidence(plate_text)
 | 
						||
            
 | 
						||
            # 添加到历史记录
 | 
						||
            history_entry = {
 | 
						||
                'text': plate_text,
 | 
						||
                'confidence': confidence,
 | 
						||
                'detection': detection,
 | 
						||
                'corrected_image': corrected_image
 | 
						||
            }
 | 
						||
            self.plate_histories[plate_id].append(history_entry)
 | 
						||
            
 | 
						||
            # 计算稳定结果
 | 
						||
            stable_text = self.get_stable_text(plate_id)
 | 
						||
            
 | 
						||
            if stable_text and stable_text != "识别失败":
 | 
						||
                stable_results.append({
 | 
						||
                    'id': plate_id,
 | 
						||
                    'class_name': detection['class_name'],
 | 
						||
                    'corrected_image': corrected_image,
 | 
						||
                    'plate_number': stable_text,
 | 
						||
                    'detection': detection
 | 
						||
                })
 | 
						||
        
 | 
						||
        return stable_results
 | 
						||
    
 | 
						||
    def get_stable_text(self, plate_id):
 | 
						||
        """获取指定车牌的稳定识别结果"""
 | 
						||
        history = self.plate_histories[plate_id]
 | 
						||
        
 | 
						||
        if len(history) < 3:  # 历史记录太少,返回最新结果
 | 
						||
            return history[-1]['text'] if history else "识别失败"
 | 
						||
        
 | 
						||
        # 统计各种识别结果的加权投票
 | 
						||
        text_votes = defaultdict(float)
 | 
						||
        total_confidence = 0
 | 
						||
        
 | 
						||
        for entry in history:
 | 
						||
            text = entry['text']
 | 
						||
            confidence = entry['confidence']
 | 
						||
            
 | 
						||
            if text != "识别失败" and confidence > 0.3:
 | 
						||
                text_votes[text] += confidence
 | 
						||
                total_confidence += confidence
 | 
						||
        
 | 
						||
        if not text_votes:
 | 
						||
            return "识别失败"
 | 
						||
        
 | 
						||
        # 找到得票最高的结果
 | 
						||
        best_text = max(text_votes.items(), key=lambda x: x[1])
 | 
						||
        
 | 
						||
        # 检查是否足够稳定(得票率超过阈值)
 | 
						||
        vote_ratio = best_text[1] / total_confidence if total_confidence > 0 else 0
 | 
						||
        
 | 
						||
        if vote_ratio >= self.confidence_threshold:
 | 
						||
            return best_text[0]
 | 
						||
        else:
 | 
						||
            # 不够稳定,返回最近的高置信度结果
 | 
						||
            recent_high_conf = [entry for entry in list(history)[-5:] 
 | 
						||
                              if entry['confidence'] > 0.5 and entry['text'] != "识别失败"]
 | 
						||
            
 | 
						||
            if recent_high_conf:
 | 
						||
                return recent_high_conf[-1]['text']
 | 
						||
            else:
 | 
						||
                return history[-1]['text']
 | 
						||
    
 | 
						||
    def clear_old_plates(self, current_plate_ids):
 | 
						||
        """清理不再出现的车牌历史记录"""
 | 
						||
        import time
 | 
						||
        current_time = time.time()
 | 
						||
        
 | 
						||
        # 移除超过一定时间未更新的车牌
 | 
						||
        plates_to_remove = []
 | 
						||
        
 | 
						||
        # 基于时间戳清理过期的车牌(超过2秒未检测到)
 | 
						||
        for plate_id in list(self.plate_last_seen.keys()):
 | 
						||
            last_seen_time = self.plate_last_seen.get(plate_id, 0)
 | 
						||
            if current_time - last_seen_time > 2.0:  # 超过2秒清理
 | 
						||
                plates_to_remove.append(plate_id)
 | 
						||
        
 | 
						||
        # 也清理当前帧中不存在的车牌
 | 
						||
        for plate_id in self.plate_histories.keys():
 | 
						||
            if plate_id not in current_plate_ids:
 | 
						||
                plates_to_remove.append(plate_id)
 | 
						||
        
 | 
						||
        # 去重
 | 
						||
        plates_to_remove = list(set(plates_to_remove))
 | 
						||
        
 | 
						||
        for plate_id in plates_to_remove:
 | 
						||
            if plate_id in self.plate_histories:
 | 
						||
                del self.plate_histories[plate_id]
 | 
						||
            if plate_id in self.plate_positions:
 | 
						||
                del self.plate_positions[plate_id]
 | 
						||
            if plate_id in self.stable_results:
 | 
						||
                del self.stable_results[plate_id]
 | 
						||
            if plate_id in self.plate_last_seen:
 | 
						||
                del self.plate_last_seen[plate_id]
 | 
						||
            if plate_id in self.plate_frame_count:
 | 
						||
                del self.plate_frame_count[plate_id]
 | 
						||
 | 
						||
class CameraThread(QThread):
 | 
						||
    """摄像头线程类"""
 | 
						||
    frame_ready = pyqtSignal(np.ndarray)
 | 
						||
    
 | 
						||
    def __init__(self):
 | 
						||
        super().__init__()
 | 
						||
        self.camera = None
 | 
						||
        self.running = False
 | 
						||
    
 | 
						||
    def start_camera(self):
 | 
						||
        """启动摄像头"""
 | 
						||
        self.camera = cv2.VideoCapture(0)
 | 
						||
        if self.camera.isOpened():
 | 
						||
            self.running = True
 | 
						||
            self.start()
 | 
						||
            return True
 | 
						||
        return False
 | 
						||
    
 | 
						||
    def stop_camera(self):
 | 
						||
        """停止摄像头"""
 | 
						||
        self.running = False
 | 
						||
        if self.camera:
 | 
						||
            self.camera.release()
 | 
						||
        self.quit()
 | 
						||
        self.wait()
 | 
						||
    
 | 
						||
    def run(self):
 | 
						||
        """线程运行函数"""
 | 
						||
        while self.running:
 | 
						||
            if self.camera and self.camera.isOpened():
 | 
						||
                ret, frame = self.camera.read()
 | 
						||
                if ret:
 | 
						||
                    self.frame_ready.emit(frame)
 | 
						||
            self.msleep(30)  # 约30fps
 | 
						||
 | 
						||
class VideoThread(QThread):
 | 
						||
    """视频处理线程类"""
 | 
						||
    frame_ready = pyqtSignal(np.ndarray)
 | 
						||
    video_finished = pyqtSignal()
 | 
						||
    
 | 
						||
    def __init__(self):
 | 
						||
        super().__init__()
 | 
						||
        self.video_path = None
 | 
						||
        self.cap = None
 | 
						||
        self.running = False
 | 
						||
        self.paused = False
 | 
						||
    
 | 
						||
    def load_video(self, video_path):
 | 
						||
        """加载视频文件"""
 | 
						||
        self.video_path = video_path
 | 
						||
        self.cap = cv2.VideoCapture(video_path)
 | 
						||
        return self.cap.isOpened()
 | 
						||
    
 | 
						||
    def start_video(self):
 | 
						||
        """开始播放视频"""
 | 
						||
        if self.cap and self.cap.isOpened():
 | 
						||
            self.running = True
 | 
						||
            self.paused = False
 | 
						||
            self.start()
 | 
						||
            return True
 | 
						||
        return False
 | 
						||
    
 | 
						||
    def pause_video(self):
 | 
						||
        """暂停/继续视频"""
 | 
						||
        self.paused = not self.paused
 | 
						||
        return self.paused
 | 
						||
    
 | 
						||
    def stop_video(self):
 | 
						||
        """停止视频"""
 | 
						||
        self.running = False
 | 
						||
        if self.cap:
 | 
						||
            self.cap.release()
 | 
						||
        self.quit()
 | 
						||
        self.wait()
 | 
						||
    
 | 
						||
    def run(self):
 | 
						||
        """线程运行函数"""
 | 
						||
        while self.running:
 | 
						||
            if not self.paused and self.cap and self.cap.isOpened():
 | 
						||
                ret, frame = self.cap.read()
 | 
						||
                if ret:
 | 
						||
                    self.frame_ready.emit(frame)
 | 
						||
                else:
 | 
						||
                    # 视频播放结束
 | 
						||
                    self.video_finished.emit()
 | 
						||
                    self.running = False
 | 
						||
                    break
 | 
						||
            self.msleep(30)  # 约30fps
 | 
						||
 | 
						||
class LicensePlateWidget(QWidget):
 | 
						||
    """单个车牌结果显示组件"""
 | 
						||
    
 | 
						||
    def __init__(self, plate_id, class_name, corrected_image, plate_number):
 | 
						||
        super().__init__()
 | 
						||
        self.plate_id = plate_id
 | 
						||
        self.init_ui(class_name, corrected_image, plate_number)
 | 
						||
    
 | 
						||
    def init_ui(self, class_name, corrected_image, plate_number):
 | 
						||
        layout = QHBoxLayout()
 | 
						||
        layout.setContentsMargins(10, 5, 10, 5)
 | 
						||
        layout.setSpacing(8)  # 设置组件间距
 | 
						||
        
 | 
						||
        # 车牌类型标签
 | 
						||
        type_label = QLabel(class_name)
 | 
						||
        type_label.setFixedWidth(60)
 | 
						||
        type_label.setAlignment(Qt.AlignCenter)
 | 
						||
        type_label.setStyleSheet(
 | 
						||
            "QLabel { "
 | 
						||
            "background-color: #4CAF50 if class_name == '绿牌' else #2196F3; "
 | 
						||
            "color: white; "
 | 
						||
            "border-radius: 5px; "
 | 
						||
            "padding: 5px; "
 | 
						||
            "font-weight: bold; "
 | 
						||
            "}"
 | 
						||
        )
 | 
						||
        if class_name == '绿牌':
 | 
						||
            type_label.setStyleSheet(
 | 
						||
                "QLabel { "
 | 
						||
                "background-color: #4CAF50; "
 | 
						||
                "color: white; "
 | 
						||
                "border-radius: 5px; "
 | 
						||
                "padding: 5px; "
 | 
						||
                "font-weight: bold; "
 | 
						||
                "}"
 | 
						||
            )
 | 
						||
        else:
 | 
						||
            type_label.setStyleSheet(
 | 
						||
                "QLabel { "
 | 
						||
                "background-color: #2196F3; "
 | 
						||
                "color: white; "
 | 
						||
                "border-radius: 5px; "
 | 
						||
                "padding: 5px; "
 | 
						||
                "font-weight: bold; "
 | 
						||
                "}"
 | 
						||
            )
 | 
						||
        
 | 
						||
        # 矫正后的车牌图像
 | 
						||
        image_label = QLabel()
 | 
						||
        image_label.setStyleSheet("border: 1px solid #ddd; background-color: white;")
 | 
						||
        
 | 
						||
        if corrected_image is not None:
 | 
						||
            # 转换numpy数组为QPixmap
 | 
						||
            h, w = corrected_image.shape[:2]
 | 
						||
            if len(corrected_image.shape) == 3:
 | 
						||
                bytes_per_line = 3 * w
 | 
						||
                q_image = QImage(corrected_image.data, w, h, bytes_per_line, QImage.Format_RGB888).rgbSwapped()
 | 
						||
            else:
 | 
						||
                bytes_per_line = w
 | 
						||
                q_image = QImage(corrected_image.data, w, h, bytes_per_line, QImage.Format_Grayscale8)
 | 
						||
            
 | 
						||
            pixmap = QPixmap.fromImage(q_image)
 | 
						||
            
 | 
						||
            # 动态计算显示尺寸,保持车牌的宽高比
 | 
						||
            original_width = pixmap.width()
 | 
						||
            original_height = pixmap.height()
 | 
						||
            
 | 
						||
            # 设置最大显示尺寸限制
 | 
						||
            max_width = 150
 | 
						||
            max_height = 60
 | 
						||
            
 | 
						||
            # 计算缩放比例,确保图像完整显示
 | 
						||
            width_ratio = max_width / original_width if original_width > 0 else 1
 | 
						||
            height_ratio = max_height / original_height if original_height > 0 else 1
 | 
						||
            scale_ratio = min(width_ratio, height_ratio, 1.0)  # 不放大,只缩小
 | 
						||
            
 | 
						||
            # 计算实际显示尺寸
 | 
						||
            display_width = int(original_width * scale_ratio)
 | 
						||
            display_height = int(original_height * scale_ratio)
 | 
						||
            
 | 
						||
            # 确保最小显示尺寸
 | 
						||
            display_width = max(display_width, 80)
 | 
						||
            display_height = max(display_height, 25)
 | 
						||
            
 | 
						||
            # 设置标签尺寸并缩放图像
 | 
						||
            image_label.setFixedSize(display_width, display_height)
 | 
						||
            scaled_pixmap = pixmap.scaled(display_width, display_height, Qt.KeepAspectRatio, Qt.SmoothTransformation)
 | 
						||
            image_label.setPixmap(scaled_pixmap)
 | 
						||
            image_label.setAlignment(Qt.AlignCenter)
 | 
						||
        else:
 | 
						||
            # 当没有图像时,设置固定尺寸显示提示信息
 | 
						||
            image_label.setFixedSize(120, 40)
 | 
						||
            image_label.setText("车牌未完全\n进入摄像头")
 | 
						||
            image_label.setAlignment(Qt.AlignCenter)
 | 
						||
            image_label.setStyleSheet("border: 1px solid #ddd; background-color: #f5f5f5; color: #666;")
 | 
						||
        
 | 
						||
        # 车牌号标签 - 使用自适应宽度
 | 
						||
        number_label = QLabel(plate_number)
 | 
						||
        number_label.setMinimumWidth(120)  # 设置最小宽度
 | 
						||
        number_label.setMaximumWidth(200)  # 设置最大宽度
 | 
						||
        number_label.setAlignment(Qt.AlignCenter)
 | 
						||
        number_label.setStyleSheet(
 | 
						||
            "QLabel { "
 | 
						||
            "border: 1px solid #ddd; "
 | 
						||
            "background-color: white; "
 | 
						||
            "padding: 8px; "
 | 
						||
            "font-family: 'Courier New'; "
 | 
						||
            "font-size: 14px; "
 | 
						||
            "font-weight: bold; "
 | 
						||
            "}"
 | 
						||
        )
 | 
						||
        # 根据文本长度调整宽度
 | 
						||
        font_metrics = number_label.fontMetrics()
 | 
						||
        text_width = font_metrics.boundingRect(plate_number).width()
 | 
						||
        optimal_width = max(120, min(200, text_width + 20))  # 加20像素的边距
 | 
						||
        number_label.setFixedWidth(optimal_width)
 | 
						||
        
 | 
						||
        layout.addWidget(type_label)
 | 
						||
        layout.addWidget(image_label)
 | 
						||
        layout.addWidget(number_label)
 | 
						||
        layout.addStretch()
 | 
						||
        
 | 
						||
        self.setLayout(layout)
 | 
						||
        # 调整整体组件的最小高度以适应动态图像尺寸
 | 
						||
        min_height = max(60, image_label.height() + 20)  # 至少60像素高度
 | 
						||
        self.setMinimumHeight(min_height)
 | 
						||
        self.setStyleSheet(
 | 
						||
            "QWidget { "
 | 
						||
            "background-color: white; "
 | 
						||
            "border: 1px solid #e0e0e0; "
 | 
						||
            "border-radius: 8px; "
 | 
						||
            "margin: 2px; "
 | 
						||
            "}"
 | 
						||
        )
 | 
						||
 | 
						||
class MainWindow(QMainWindow):
 | 
						||
    """主窗口类"""
 | 
						||
    
 | 
						||
    def __init__(self):
 | 
						||
        super().__init__()
 | 
						||
        self.detector = None
 | 
						||
        self.camera_thread = None
 | 
						||
        self.video_thread = None
 | 
						||
        self.current_frame = None
 | 
						||
        self.detections = []
 | 
						||
        self.current_mode = "camera"  # 当前模式:camera, video, image
 | 
						||
        self.is_processing = False  # 标志位,表示是否正在处理识别任务
 | 
						||
        self.last_plate_results = []  # 存储上一次的车牌识别结果
 | 
						||
        self.current_recognition_method = "CRNN"  # 当前识别方法
 | 
						||
        
 | 
						||
        # 添加车牌稳定器
 | 
						||
        self.plate_stabilizer = PlateStabilizer(
 | 
						||
            history_size=25,  # 保存15帧历史
 | 
						||
            confidence_threshold=0.8,  # 70%置信度阈值
 | 
						||
            stability_frames=10  # 需要10帧稳定
 | 
						||
        )
 | 
						||
        
 | 
						||
        # 车牌记录存储 - 用于道闸控制
 | 
						||
        self.plate_records = {}  # 格式: {plate_number: {'first_time': datetime, 'sent': bool}}
 | 
						||
        
 | 
						||
        # 识别框命令发送记录 - 防止同一识别框重复发送命令
 | 
						||
        self.frame_command_sent = {}  # 格式: {plate_id: {'plate_number': str, 'command_sent': bool}}
 | 
						||
        
 | 
						||
        # 白名单存储
 | 
						||
        self.whitelist = set()  # 存储白名单车牌号
 | 
						||
        
 | 
						||
        self.init_ui()
 | 
						||
        self.init_detector()
 | 
						||
        self.init_camera()
 | 
						||
        self.init_video()
 | 
						||
 | 
						||
        # 初始化默认识别方法(CRNN)的模型
 | 
						||
        self.change_recognition_method(self.current_recognition_method)
 | 
						||
 | 
						||
    
 | 
						||
    def init_ui(self):
 | 
						||
        """初始化用户界面"""
 | 
						||
        self.setWindowTitle("车牌识别系统")
 | 
						||
        self.setGeometry(100, 100, 1200, 800)
 | 
						||
        
 | 
						||
        # 创建中央widget
 | 
						||
        central_widget = QWidget()
 | 
						||
        self.setCentralWidget(central_widget)
 | 
						||
        
 | 
						||
        # 创建主布局
 | 
						||
        main_layout = QHBoxLayout(central_widget)
 | 
						||
        
 | 
						||
        # 左侧摄像头显示区域
 | 
						||
        left_frame = QFrame()
 | 
						||
        left_frame.setFrameStyle(QFrame.StyledPanel)
 | 
						||
        left_frame.setStyleSheet("QFrame { background-color: #f0f0f0; border: 2px solid #ddd; }")
 | 
						||
        left_layout = QVBoxLayout(left_frame)
 | 
						||
        
 | 
						||
        # 摄像头显示标签
 | 
						||
        self.camera_label = QLabel()
 | 
						||
        self.camera_label.setMinimumSize(640, 480)
 | 
						||
        self.camera_label.setStyleSheet("QLabel { background-color: black; border: 1px solid #ccc; }")
 | 
						||
        self.camera_label.setAlignment(Qt.AlignCenter)
 | 
						||
        self.camera_label.setText("摄像头未启动")
 | 
						||
        self.camera_label.setScaledContents(False)
 | 
						||
        
 | 
						||
        # 控制按钮
 | 
						||
        button_layout = QHBoxLayout()
 | 
						||
        self.start_button = QPushButton("启动摄像头")
 | 
						||
        self.stop_button = QPushButton("停止摄像头")
 | 
						||
        self.start_button.clicked.connect(self.start_camera)
 | 
						||
        self.stop_button.clicked.connect(self.stop_camera)
 | 
						||
        self.stop_button.setEnabled(False)
 | 
						||
        
 | 
						||
        # 视频控制按钮
 | 
						||
        self.open_video_button = QPushButton("打开视频")
 | 
						||
        self.stop_video_button = QPushButton("停止视频")
 | 
						||
        self.pause_video_button = QPushButton("暂停视频")
 | 
						||
        self.open_video_button.clicked.connect(self.open_video_file)
 | 
						||
        self.stop_video_button.clicked.connect(self.stop_video)
 | 
						||
        self.pause_video_button.clicked.connect(self.pause_video)
 | 
						||
        self.stop_video_button.setEnabled(False)
 | 
						||
        self.pause_video_button.setEnabled(False)
 | 
						||
        
 | 
						||
        # 图片控制按钮
 | 
						||
        self.open_image_button = QPushButton("打开图片")
 | 
						||
        self.open_image_button.clicked.connect(self.open_image_file)
 | 
						||
        
 | 
						||
        button_layout.addWidget(self.start_button)
 | 
						||
        button_layout.addWidget(self.stop_button)
 | 
						||
        button_layout.addWidget(self.open_video_button)
 | 
						||
        button_layout.addWidget(self.stop_video_button)
 | 
						||
        button_layout.addWidget(self.pause_video_button)
 | 
						||
        button_layout.addWidget(self.open_image_button)
 | 
						||
        button_layout.addStretch()
 | 
						||
        
 | 
						||
        left_layout.addWidget(self.camera_label)
 | 
						||
        left_layout.addLayout(button_layout)
 | 
						||
        
 | 
						||
        # 右侧结果显示区域
 | 
						||
        right_frame = QFrame()
 | 
						||
        right_frame.setFrameStyle(QFrame.StyledPanel)
 | 
						||
        right_frame.setFixedWidth(460)
 | 
						||
        right_frame.setStyleSheet("QFrame { background-color: #fafafa; border: 2px solid #ddd; }")
 | 
						||
        right_layout = QVBoxLayout(right_frame)
 | 
						||
        
 | 
						||
        # 标题
 | 
						||
        title_label = QLabel("检测结果")
 | 
						||
        title_label.setAlignment(Qt.AlignCenter)
 | 
						||
        title_label.setFont(QFont("Arial", 16, QFont.Bold))
 | 
						||
        title_label.setStyleSheet("QLabel { color: #333; padding: 10px; }")
 | 
						||
        
 | 
						||
        # 识别方法选择
 | 
						||
        method_layout = QHBoxLayout()
 | 
						||
        method_label = QLabel("识别方法:")
 | 
						||
        method_label.setFont(QFont("Arial", 10))
 | 
						||
        
 | 
						||
        self.method_combo = QComboBox()
 | 
						||
        self.method_combo.addItems(["CRNN", "LightCRNN", "OCR"])
 | 
						||
        self.method_combo.setCurrentText("CRNN")  # 默认选择CRNN
 | 
						||
        self.method_combo.currentTextChanged.connect(self.change_recognition_method)
 | 
						||
        
 | 
						||
        method_layout.addWidget(method_label)
 | 
						||
        method_layout.addWidget(self.method_combo)
 | 
						||
        method_layout.addStretch()
 | 
						||
        
 | 
						||
        # 车牌数量显示
 | 
						||
        self.count_label = QLabel("识别到的车牌数量: 0")
 | 
						||
        self.count_label.setAlignment(Qt.AlignCenter)
 | 
						||
        self.count_label.setFont(QFont("Arial", 12))
 | 
						||
        self.count_label.setStyleSheet(
 | 
						||
            "QLabel { "
 | 
						||
            "background-color: #e3f2fd; "
 | 
						||
            "border: 1px solid #2196f3; "
 | 
						||
            "border-radius: 5px; "
 | 
						||
            "padding: 8px; "
 | 
						||
            "color: #1976d2; "
 | 
						||
            "font-weight: bold; "
 | 
						||
            "}"
 | 
						||
        )
 | 
						||
        
 | 
						||
        # 滚动区域用于显示车牌结果
 | 
						||
        scroll_area = QScrollArea()
 | 
						||
        scroll_area.setWidgetResizable(True)
 | 
						||
        scroll_area.setStyleSheet("QScrollArea { border: none; background-color: transparent; }")
 | 
						||
        
 | 
						||
        self.results_widget = QWidget()
 | 
						||
        self.results_layout = QVBoxLayout(self.results_widget)
 | 
						||
        self.results_layout.setAlignment(Qt.AlignTop)
 | 
						||
        
 | 
						||
        scroll_area.setWidget(self.results_widget)
 | 
						||
        
 | 
						||
        # 当前识别任务显示
 | 
						||
        self.current_method_label = QLabel("当前识别方法: CRNN")
 | 
						||
        self.current_method_label.setAlignment(Qt.AlignRight)
 | 
						||
        self.current_method_label.setFont(QFont("Arial", 9))
 | 
						||
        self.current_method_label.setStyleSheet("QLabel { color: #666; padding: 5px; }")
 | 
						||
        
 | 
						||
        # 白名单和手动控制区域 - 重新设计
 | 
						||
        control_frame = QFrame()
 | 
						||
        control_frame.setFrameStyle(QFrame.StyledPanel)
 | 
						||
        control_frame.setStyleSheet("""
 | 
						||
            QFrame {
 | 
						||
                background-color: #f8f9fa;
 | 
						||
                border: 1px solid #dee2e6;
 | 
						||
                border-radius: 8px;
 | 
						||
                padding: 10px;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
        control_layout = QVBoxLayout(control_frame)
 | 
						||
        control_layout.setSpacing(12)
 | 
						||
        
 | 
						||
        # 白名单管理部分
 | 
						||
        whitelist_group = QFrame()
 | 
						||
        whitelist_group.setStyleSheet("""
 | 
						||
            QFrame {
 | 
						||
                background-color: white;
 | 
						||
                border: 1px solid #e9ecef;
 | 
						||
                border-radius: 6px;
 | 
						||
                padding: 8px;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
        whitelist_group_layout = QVBoxLayout(whitelist_group)
 | 
						||
        whitelist_group_layout.setSpacing(8)
 | 
						||
        
 | 
						||
        # 白名单标题
 | 
						||
        whitelist_title = QLabel("车牌白名单")
 | 
						||
        whitelist_title.setFont(QFont("Microsoft YaHei", 11, QFont.Bold))
 | 
						||
        whitelist_title.setStyleSheet("QLabel { color: #495057; margin-bottom: 5px; }")
 | 
						||
        
 | 
						||
        # 白名单输入行
 | 
						||
        input_row = QHBoxLayout()
 | 
						||
        input_row.setSpacing(8)
 | 
						||
        
 | 
						||
        self.whitelist_input = QLineEdit()
 | 
						||
        self.whitelist_input.setPlaceholderText("输入车牌号码")
 | 
						||
        self.whitelist_input.setStyleSheet("""
 | 
						||
            QLineEdit {
 | 
						||
                padding: 8px 12px;
 | 
						||
                border: 1px solid #ced4da;
 | 
						||
                border-radius: 4px;
 | 
						||
                font-size: 12px;
 | 
						||
                background-color: white;
 | 
						||
            }
 | 
						||
            QLineEdit:focus {
 | 
						||
                border-color: #007bff;
 | 
						||
                outline: none;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
        
 | 
						||
        self.add_whitelist_button = QPushButton("添加")
 | 
						||
        self.add_whitelist_button.setFixedSize(60, 32)
 | 
						||
        self.add_whitelist_button.setStyleSheet("""
 | 
						||
            QPushButton {
 | 
						||
                background-color: #28a745;
 | 
						||
                color: white;
 | 
						||
                border: none;
 | 
						||
                border-radius: 4px;
 | 
						||
                font-size: 12px;
 | 
						||
                font-weight: 500;
 | 
						||
            }
 | 
						||
            QPushButton:hover {
 | 
						||
                background-color: #218838;
 | 
						||
            }
 | 
						||
            QPushButton:pressed {
 | 
						||
                background-color: #1e7e34;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
        self.add_whitelist_button.clicked.connect(self.add_to_whitelist)
 | 
						||
        
 | 
						||
        self.remove_whitelist_button = QPushButton("删除")
 | 
						||
        self.remove_whitelist_button.setFixedSize(60, 32)
 | 
						||
        self.remove_whitelist_button.setStyleSheet("""
 | 
						||
            QPushButton {
 | 
						||
                background-color: #dc3545;
 | 
						||
                color: white;
 | 
						||
                border: none;
 | 
						||
                border-radius: 4px;
 | 
						||
                font-size: 12px;
 | 
						||
                font-weight: 500;
 | 
						||
            }
 | 
						||
            QPushButton:hover {
 | 
						||
                background-color: #c82333;
 | 
						||
            }
 | 
						||
            QPushButton:pressed {
 | 
						||
                background-color: #bd2130;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
        self.remove_whitelist_button.clicked.connect(self.remove_from_whitelist)
 | 
						||
        
 | 
						||
        input_row.addWidget(self.whitelist_input)
 | 
						||
        input_row.addWidget(self.add_whitelist_button)
 | 
						||
        input_row.addWidget(self.remove_whitelist_button)
 | 
						||
        
 | 
						||
        # 白名单列表
 | 
						||
        self.whitelist_display = QListWidget()
 | 
						||
        self.whitelist_display.setMaximumHeight(100)
 | 
						||
        self.whitelist_display.setStyleSheet("""
 | 
						||
            QListWidget {
 | 
						||
                border: 1px solid #e9ecef;
 | 
						||
                border-radius: 4px;
 | 
						||
                background-color: #f8f9fa;
 | 
						||
                font-size: 12px;
 | 
						||
                padding: 4px;
 | 
						||
            }
 | 
						||
            QListWidget::item {
 | 
						||
                padding: 6px 8px;
 | 
						||
                border-radius: 3px;
 | 
						||
                margin: 1px;
 | 
						||
            }
 | 
						||
            QListWidget::item:selected {
 | 
						||
                background-color: #007bff;
 | 
						||
                color: white;
 | 
						||
            }
 | 
						||
            QListWidget::item:hover {
 | 
						||
                background-color: #e9ecef;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
        
 | 
						||
        whitelist_group_layout.addWidget(whitelist_title)
 | 
						||
        whitelist_group_layout.addLayout(input_row)
 | 
						||
        whitelist_group_layout.addWidget(self.whitelist_display)
 | 
						||
        
 | 
						||
        # 手动控制部分
 | 
						||
        manual_group = QFrame()
 | 
						||
        manual_group.setStyleSheet("""
 | 
						||
            QFrame {
 | 
						||
                background-color: white;
 | 
						||
                border: 1px solid #e9ecef;
 | 
						||
                border-radius: 6px;
 | 
						||
                padding: 8px;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
        manual_group_layout = QVBoxLayout(manual_group)
 | 
						||
        manual_group_layout.setSpacing(8)
 | 
						||
        
 | 
						||
        # 手动控制标题
 | 
						||
        manual_title = QLabel("手动控制")
 | 
						||
        manual_title.setFont(QFont("Microsoft YaHei", 11, QFont.Bold))
 | 
						||
        manual_title.setStyleSheet("QLabel { color: #495057; margin-bottom: 5px; }")
 | 
						||
        
 | 
						||
        # 手动控制按钮行
 | 
						||
        manual_row = QHBoxLayout()
 | 
						||
        manual_row.setSpacing(12)
 | 
						||
        
 | 
						||
        self.manual_open_button = QPushButton("开闸")
 | 
						||
        self.manual_open_button.setFixedSize(80, 36)
 | 
						||
        self.manual_open_button.setStyleSheet("""
 | 
						||
            QPushButton {
 | 
						||
                background-color: #007bff;
 | 
						||
                color: white;
 | 
						||
                border: none;
 | 
						||
                border-radius: 6px;
 | 
						||
                font-size: 13px;
 | 
						||
                font-weight: 500;
 | 
						||
            }
 | 
						||
            QPushButton:hover {
 | 
						||
                background-color: #0056b3;
 | 
						||
            }
 | 
						||
            QPushButton:pressed {
 | 
						||
                background-color: #004085;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
        self.manual_open_button.clicked.connect(self.manual_open_gate)
 | 
						||
        
 | 
						||
        self.manual_close_button = QPushButton("关闸")
 | 
						||
        self.manual_close_button.setFixedSize(80, 36)
 | 
						||
        self.manual_close_button.setStyleSheet("""
 | 
						||
            QPushButton {
 | 
						||
                background-color: #fd7e14;
 | 
						||
                color: white;
 | 
						||
                border: none;
 | 
						||
                border-radius: 6px;
 | 
						||
                font-size: 13px;
 | 
						||
                font-weight: 500;
 | 
						||
            }
 | 
						||
            QPushButton:hover {
 | 
						||
                background-color: #e8650e;
 | 
						||
            }
 | 
						||
            QPushButton:pressed {
 | 
						||
                background-color: #d35400;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
        self.manual_close_button.clicked.connect(self.manual_close_gate)
 | 
						||
        
 | 
						||
        manual_row.addWidget(self.manual_open_button)
 | 
						||
        manual_row.addWidget(self.manual_close_button)
 | 
						||
        manual_row.addStretch()
 | 
						||
        
 | 
						||
        manual_group_layout.addWidget(manual_title)
 | 
						||
        manual_group_layout.addLayout(manual_row)
 | 
						||
        
 | 
						||
        # 添加到主控制布局
 | 
						||
        control_layout.addWidget(whitelist_group)
 | 
						||
        control_layout.addWidget(manual_group)
 | 
						||
        
 | 
						||
        right_layout.addWidget(title_label)
 | 
						||
        right_layout.addLayout(method_layout)
 | 
						||
        right_layout.addWidget(self.count_label)
 | 
						||
        right_layout.addWidget(scroll_area)
 | 
						||
        right_layout.addWidget(control_frame)
 | 
						||
        right_layout.addWidget(self.current_method_label)
 | 
						||
        
 | 
						||
        # 添加到主布局
 | 
						||
        main_layout.addWidget(left_frame, 2)
 | 
						||
        main_layout.addWidget(right_frame, 1)
 | 
						||
        
 | 
						||
        # 设置样式
 | 
						||
        self.setStyleSheet("""
 | 
						||
            QMainWindow {
 | 
						||
                background-color: #f5f5f5;
 | 
						||
            }
 | 
						||
            QPushButton {
 | 
						||
                background-color: #2196F3;
 | 
						||
                color: white;
 | 
						||
                border: none;
 | 
						||
                padding: 8px 16px;
 | 
						||
                border-radius: 4px;
 | 
						||
                font-weight: bold;
 | 
						||
            }
 | 
						||
            QPushButton:hover {
 | 
						||
                background-color: #1976D2;
 | 
						||
            }
 | 
						||
            QPushButton:pressed {
 | 
						||
                background-color: #0D47A1;
 | 
						||
            }
 | 
						||
            QPushButton:disabled {
 | 
						||
                background-color: #cccccc;
 | 
						||
                color: #666666;
 | 
						||
            }
 | 
						||
        """)
 | 
						||
    
 | 
						||
    def init_detector(self):
 | 
						||
        """初始化检测器"""
 | 
						||
        model_path = os.path.join(os.path.dirname(__file__), "yolopart", "yolo11s-pose42.pt")
 | 
						||
        self.detector = LicensePlateYOLO(model_path)
 | 
						||
    
 | 
						||
    def reset_processing_state(self):
 | 
						||
        """重置处理状态和清理界面"""
 | 
						||
        # 重置处理标志
 | 
						||
        self.is_processing = False
 | 
						||
        
 | 
						||
        # 清空当前帧和检测结果
 | 
						||
        self.current_frame = None
 | 
						||
        self.detections = []
 | 
						||
        
 | 
						||
        # 重置车牌稳定器
 | 
						||
        self.plate_stabilizer = PlateStabilizer(
 | 
						||
            history_size=15,
 | 
						||
            confidence_threshold=0.7,
 | 
						||
            stability_frames=10
 | 
						||
        )
 | 
						||
        
 | 
						||
        # 清空识别框命令发送记录
 | 
						||
        self.frame_command_sent = {}
 | 
						||
        
 | 
						||
        # 清空右侧结果显示
 | 
						||
        self.count_label.setText("识别到的车牌数量: 0")
 | 
						||
        for i in reversed(range(self.results_layout.count())):
 | 
						||
            child = self.results_layout.itemAt(i).widget()
 | 
						||
            if child:
 | 
						||
                child.setParent(None)
 | 
						||
        self.last_plate_results = []
 | 
						||
        
 | 
						||
        print("处理状态已重置,界面已清理")
 | 
						||
    
 | 
						||
    def init_camera(self):
 | 
						||
        """初始化摄像头线程"""
 | 
						||
        self.camera_thread = CameraThread()
 | 
						||
        self.camera_thread.frame_ready.connect(self.process_frame)
 | 
						||
    
 | 
						||
    def init_video(self):
 | 
						||
        """初始化视频线程"""
 | 
						||
        self.video_thread = VideoThread()
 | 
						||
        self.video_thread.frame_ready.connect(self.process_frame)
 | 
						||
        self.video_thread.video_finished.connect(self.on_video_finished)
 | 
						||
    
 | 
						||
    def start_camera(self):
 | 
						||
        """启动摄像头"""
 | 
						||
        # 重置处理状态和清理界面
 | 
						||
        self.reset_processing_state()
 | 
						||
        
 | 
						||
        if self.camera_thread.start_camera():
 | 
						||
            self.current_mode = "camera"
 | 
						||
            self.start_button.setEnabled(False)
 | 
						||
            self.stop_button.setEnabled(True)
 | 
						||
            self.camera_label.setText("摄像头启动中...")
 | 
						||
        else:
 | 
						||
            self.camera_label.setText("摄像头启动失败")
 | 
						||
    
 | 
						||
    def stop_camera(self):
 | 
						||
        """停止摄像头"""
 | 
						||
        self.camera_thread.stop_camera()
 | 
						||
        self.start_button.setEnabled(True)
 | 
						||
        self.stop_button.setEnabled(False)
 | 
						||
        self.camera_label.setText("摄像头已停止")
 | 
						||
        # 只在摄像头模式下清除标签内容
 | 
						||
        if self.current_mode == "camera":
 | 
						||
            self.camera_label.clear()
 | 
						||
    
 | 
						||
    def on_video_finished(self):
 | 
						||
        """视频播放结束时的处理"""
 | 
						||
        self.video_thread.stop_video()
 | 
						||
        self.open_video_button.setEnabled(True)
 | 
						||
        self.stop_video_button.setEnabled(False)
 | 
						||
        self.pause_video_button.setEnabled(False)
 | 
						||
        self.camera_label.setText("视频播放结束")
 | 
						||
        self.current_mode = "camera"
 | 
						||
    
 | 
						||
    def open_video_file(self):
 | 
						||
        """打开视频文件"""
 | 
						||
        # 停止当前模式
 | 
						||
        if self.current_mode == "camera" and self.camera_thread and self.camera_thread.running:
 | 
						||
            self.stop_camera()
 | 
						||
        elif self.current_mode == "video" and self.video_thread and self.video_thread.running:
 | 
						||
            self.stop_video()
 | 
						||
        
 | 
						||
        # 重置处理状态和清理界面
 | 
						||
        self.reset_processing_state()
 | 
						||
        
 | 
						||
        # 选择视频文件
 | 
						||
        video_path, _ = QFileDialog.getOpenFileName(self, "选择视频文件", "", "视频文件 (*.mp4 *.avi *.mov *.mkv)")
 | 
						||
        
 | 
						||
        if video_path:
 | 
						||
            if self.video_thread.load_video(video_path):
 | 
						||
                self.current_mode = "video"
 | 
						||
                self.start_video()
 | 
						||
                self.camera_label.setText(f"正在播放视频: {os.path.basename(video_path)}")
 | 
						||
            else:
 | 
						||
                self.camera_label.setText("视频加载失败")
 | 
						||
    
 | 
						||
    def start_video(self):
 | 
						||
        """开始播放视频"""
 | 
						||
        if self.video_thread.start_video():
 | 
						||
            self.open_video_button.setEnabled(False)
 | 
						||
            self.stop_video_button.setEnabled(True)
 | 
						||
            self.pause_video_button.setEnabled(True)
 | 
						||
            self.pause_video_button.setText("暂停")
 | 
						||
        else:
 | 
						||
            self.camera_label.setText("视频播放失败")
 | 
						||
    
 | 
						||
    def pause_video(self):
 | 
						||
        """暂停/继续视频"""
 | 
						||
        if self.video_thread.pause_video():
 | 
						||
            self.pause_video_button.setText("继续")
 | 
						||
        else:
 | 
						||
            self.pause_video_button.setText("暂停")
 | 
						||
    
 | 
						||
    def stop_video(self):
 | 
						||
        """停止视频"""
 | 
						||
        self.video_thread.stop_video()
 | 
						||
        self.open_video_button.setEnabled(True)
 | 
						||
        self.stop_video_button.setEnabled(False)
 | 
						||
        self.pause_video_button.setEnabled(False)
 | 
						||
        self.camera_label.setText("视频已停止")
 | 
						||
        # 只在视频模式下清除标签内容
 | 
						||
        if self.current_mode == "video":
 | 
						||
            self.camera_label.clear()
 | 
						||
        self.current_mode = "camera"
 | 
						||
    
 | 
						||
    def open_image_file(self):
 | 
						||
        """打开图片文件"""
 | 
						||
        # 停止当前模式
 | 
						||
        if self.current_mode == "camera" and self.camera_thread and self.camera_thread.running:
 | 
						||
            self.stop_camera()
 | 
						||
        elif self.current_mode == "video" and self.video_thread and self.video_thread.running:
 | 
						||
            self.stop_video()
 | 
						||
        
 | 
						||
        # 重置处理状态和清理界面
 | 
						||
        self.reset_processing_state()
 | 
						||
        
 | 
						||
        # 选择图片文件
 | 
						||
        image_path, _ = QFileDialog.getOpenFileName(self, "选择图片文件", "", "图片文件 (*.jpg *.jpeg *.png *.bmp)")
 | 
						||
        
 | 
						||
        if image_path:
 | 
						||
            self.current_mode = "image"
 | 
						||
            try:
 | 
						||
                # 读取图片 - 方法1: 使用cv2.imdecode处理中文路径
 | 
						||
                image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), cv2.IMREAD_COLOR)
 | 
						||
                
 | 
						||
                # 如果方法1失败,尝试方法2: 直接使用cv2.imread
 | 
						||
                if image is None:
 | 
						||
                    image = cv2.imread(image_path)
 | 
						||
                    
 | 
						||
                if image is not None:
 | 
						||
                    print(f"成功加载图片: {image_path}, 尺寸: {image.shape}")
 | 
						||
                    self.process_image(image)
 | 
						||
                    # 不在这里设置文本,避免覆盖图片
 | 
						||
                    # self.camera_label.setText(f"正在显示图片: {os.path.basename(image_path)}")
 | 
						||
                else:
 | 
						||
                    print(f"图片加载失败: {image_path}")
 | 
						||
                    self.camera_label.setText("图片加载失败")
 | 
						||
            except Exception as e:
 | 
						||
                print(f"图片处理异常: {str(e)}")
 | 
						||
                self.camera_label.setText(f"图片处理错误: {str(e)}")
 | 
						||
    
 | 
						||
    def process_image(self, image):
 | 
						||
        """处理图片"""
 | 
						||
        try:
 | 
						||
            print(f"开始处理图片,图片尺寸: {image.shape}")
 | 
						||
            self.current_frame = image.copy()
 | 
						||
            
 | 
						||
            # 进行车牌检测
 | 
						||
            print("正在进行车牌检测...")
 | 
						||
            self.detections = self.detector.detect_license_plates(image)
 | 
						||
            print(f"检测到 {len(self.detections)} 个车牌")
 | 
						||
            
 | 
						||
            # 在图像上绘制检测结果
 | 
						||
            print("正在绘制检测结果...")
 | 
						||
            display_frame = self.draw_detections(image.copy())
 | 
						||
            
 | 
						||
            # 转换为Qt格式并显示
 | 
						||
            print("正在显示图片...")
 | 
						||
            self.display_frame(display_frame)
 | 
						||
            
 | 
						||
            # 更新右侧结果显示
 | 
						||
            print("正在更新结果显示...")
 | 
						||
            self.update_results_display()
 | 
						||
            print("图片处理完成")
 | 
						||
        except Exception as e:
 | 
						||
            print(f"图片处理过程中出错: {str(e)}")
 | 
						||
            import traceback
 | 
						||
            traceback.print_exc()
 | 
						||
    
 | 
						||
    def process_frame(self, frame):
 | 
						||
        """处理摄像头帧"""
 | 
						||
        if frame is None:
 | 
						||
            return
 | 
						||
            
 | 
						||
        self.current_frame = frame.copy()
 | 
						||
        
 | 
						||
        # 先显示原始帧,保证视频流畅播放
 | 
						||
        self.display_frame(frame)
 | 
						||
        
 | 
						||
        # 如果当前没有在处理识别任务,则开始新的识别任务
 | 
						||
        if not self.is_processing:
 | 
						||
            self.is_processing = True
 | 
						||
            # 异步进行车牌检测和识别
 | 
						||
            QTimer.singleShot(0, self.async_detect_and_update)
 | 
						||
    
 | 
						||
    def async_detect_and_update(self):
 | 
						||
        """异步进行车牌检测和识别"""
 | 
						||
        if self.current_frame is None:
 | 
						||
            self.is_processing = False  # 重置标志位
 | 
						||
            return
 | 
						||
            
 | 
						||
        try:
 | 
						||
            # 进行车牌检测
 | 
						||
            self.detections = self.detector.detect_license_plates(self.current_frame)
 | 
						||
            
 | 
						||
            # 在图像上绘制检测结果
 | 
						||
            display_frame = self.draw_detections(self.current_frame.copy())
 | 
						||
            
 | 
						||
            # 更新显示帧(显示带检测结果的帧)
 | 
						||
            # 无论是摄像头模式还是视频模式,都显示检测框
 | 
						||
            self.display_frame(display_frame)
 | 
						||
            
 | 
						||
            # 更新右侧结果显示
 | 
						||
            self.update_results_display()
 | 
						||
        except Exception as e:
 | 
						||
            print(f"异步检测和更新失败: {str(e)}")
 | 
						||
            import traceback
 | 
						||
            traceback.print_exc()
 | 
						||
        finally:
 | 
						||
            # 无论成功或失败,都要重置标志位
 | 
						||
            self.is_processing = False
 | 
						||
    
 | 
						||
    def draw_detections(self, frame):
 | 
						||
        """在图像上绘制检测结果"""
 | 
						||
        # 获取车牌号列表
 | 
						||
        plate_numbers = []
 | 
						||
        for detection in self.detections:
 | 
						||
            # 矫正车牌图像
 | 
						||
            corrected_image = self.correct_license_plate(detection)
 | 
						||
            # 获取车牌号
 | 
						||
            if corrected_image is not None:
 | 
						||
                plate_number = self.recognize_plate_number(corrected_image, detection['class_name'])
 | 
						||
                plate_numbers.append(plate_number)
 | 
						||
            else:
 | 
						||
                plate_numbers.append("识别失败")
 | 
						||
        
 | 
						||
        return self.detector.draw_detections(frame, self.detections, plate_numbers)
 | 
						||
    
 | 
						||
    def display_frame(self, frame):
 | 
						||
        """显示帧到界面"""
 | 
						||
        try:
 | 
						||
            
 | 
						||
            # 方法1: 标准方法
 | 
						||
            try:
 | 
						||
                rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 | 
						||
                h, w, ch = rgb_frame.shape
 | 
						||
                bytes_per_line = ch * w
 | 
						||
                qt_image = QImage(rgb_frame.data, w, h, bytes_per_line, QImage.Format_RGB888)
 | 
						||
                
 | 
						||
                if qt_image.isNull():
 | 
						||
                    print("方法1: QImage为空,尝试方法2")
 | 
						||
                    raise Exception("QImage为空")
 | 
						||
                    
 | 
						||
                pixmap = QPixmap.fromImage(qt_image)
 | 
						||
                if pixmap.isNull():
 | 
						||
                    print("方法1: QPixmap为空,尝试方法2")
 | 
						||
                    raise Exception("QPixmap为空")
 | 
						||
                    
 | 
						||
                # 手动缩放图片以适应标签大小,保持宽高比
 | 
						||
                scaled_pixmap = pixmap.scaled(self.camera_label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
 | 
						||
                self.camera_label.setPixmap(scaled_pixmap)
 | 
						||
                return
 | 
						||
            except Exception as e1:
 | 
						||
                print(f"方法1失败: {str(e1)}")
 | 
						||
            
 | 
						||
            # 方法2: 使用imencode和imdecode
 | 
						||
            try:
 | 
						||
                print("尝试方法2: 使用imencode和imdecode")
 | 
						||
                _, buffer = cv2.imencode('.jpg', frame)
 | 
						||
                rgb_frame = cv2.imdecode(buffer, cv2.IMREAD_COLOR)
 | 
						||
                rgb_frame = cv2.cvtColor(rgb_frame, cv2.COLOR_BGR2RGB)
 | 
						||
                h, w, ch = rgb_frame.shape
 | 
						||
                bytes_per_line = ch * w
 | 
						||
                qt_image = QImage(rgb_frame.data, w, h, bytes_per_line, QImage.Format_RGB888)
 | 
						||
                
 | 
						||
                print(f"方法2: 创建QImage,尺寸: {qt_image.width()}x{qt_image.height()}")
 | 
						||
                if qt_image.isNull():
 | 
						||
                    print("方法2: QImage为空")
 | 
						||
                    raise Exception("QImage为空")
 | 
						||
                    
 | 
						||
                pixmap = QPixmap.fromImage(qt_image)
 | 
						||
                if pixmap.isNull():
 | 
						||
                    print("方法2: QPixmap为空")
 | 
						||
                    raise Exception("QPixmap为空")
 | 
						||
                    
 | 
						||
                # 手动缩放图片以适应标签大小,保持宽高比
 | 
						||
                scaled_pixmap = pixmap.scaled(self.camera_label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
 | 
						||
                self.camera_label.setPixmap(scaled_pixmap)
 | 
						||
                print("方法2: 帧显示完成")
 | 
						||
                return
 | 
						||
            except Exception as e2:
 | 
						||
                print(f"方法2失败: {str(e2)}")
 | 
						||
            
 | 
						||
            # 方法3: 直接使用QImage的构造函数
 | 
						||
            try:
 | 
						||
                print("尝试方法3: 直接使用QImage的构造函数")
 | 
						||
                height, width, channel = frame.shape
 | 
						||
                bytes_per_line = 3 * width
 | 
						||
                q_image = QImage(frame.data, width, height, bytes_per_line, QImage.Format_BGR888)
 | 
						||
                
 | 
						||
                print(f"方法3: 创建QImage,尺寸: {q_image.width()}x{q_image.height()}")
 | 
						||
                if q_image.isNull():
 | 
						||
                    print("方法3: QImage为空")
 | 
						||
                    raise Exception("QImage为空")
 | 
						||
                    
 | 
						||
                pixmap = QPixmap.fromImage(q_image)
 | 
						||
                if pixmap.isNull():
 | 
						||
                    print("方法3: QPixmap为空")
 | 
						||
                    raise Exception("QPixmap为空")
 | 
						||
                    
 | 
						||
                # 手动缩放图片以适应标签大小,保持宽高比
 | 
						||
                scaled_pixmap = pixmap.scaled(self.camera_label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
 | 
						||
                self.camera_label.setPixmap(scaled_pixmap)
 | 
						||
                print("方法3: 帧显示完成")
 | 
						||
                return
 | 
						||
            except Exception as e3:
 | 
						||
                print(f"方法3失败: {str(e3)}")
 | 
						||
                
 | 
						||
            # 所有方法都失败
 | 
						||
            print("所有显示方法都失败")
 | 
						||
            self.camera_label.setText("图片显示失败")
 | 
						||
                
 | 
						||
        except Exception as e:
 | 
						||
            print(f"显示帧过程中出错: {str(e)}")
 | 
						||
            import traceback
 | 
						||
            traceback.print_exc()
 | 
						||
            self.camera_label.setText(f"显示错误: {str(e)}")
 | 
						||
    
 | 
						||
    def update_results_display(self):
 | 
						||
        """更新右侧结果显示(使用稳定化结果)"""
 | 
						||
        
 | 
						||
        if not self.detections:
 | 
						||
            self.count_label.setText("识别到的车牌数量: 0")
 | 
						||
            # 清除显示
 | 
						||
            for i in reversed(range(self.results_layout.count())):
 | 
						||
                child = self.results_layout.itemAt(i).widget()
 | 
						||
                if child:
 | 
						||
                    child.setParent(None)
 | 
						||
            self.last_plate_results = []
 | 
						||
            return
 | 
						||
        
 | 
						||
        # 获取矫正图像和识别文本
 | 
						||
        corrected_images = []
 | 
						||
        plate_texts = []
 | 
						||
        
 | 
						||
        for detection in self.detections:
 | 
						||
            corrected_image = self.correct_license_plate(detection)
 | 
						||
            corrected_images.append(corrected_image)
 | 
						||
            
 | 
						||
            if corrected_image is not None:
 | 
						||
                plate_text = self.recognize_plate_number(corrected_image, detection['class_name'])
 | 
						||
            else:
 | 
						||
                plate_text = "识别失败"
 | 
						||
            plate_texts.append(plate_text)
 | 
						||
        
 | 
						||
        # 使用稳定器获取稳定的识别结果
 | 
						||
        stable_results = self.plate_stabilizer.update_and_get_stable_result(
 | 
						||
            self.detections, corrected_images, plate_texts
 | 
						||
        )
 | 
						||
        
 | 
						||
        # 更新车牌数量显示
 | 
						||
        self.count_label.setText(f"识别到的车牌数量: {len(stable_results)}")
 | 
						||
        print(f"稳定结果数量: {len(stable_results)}")
 | 
						||
        
 | 
						||
        # 检查结果是否发生变化
 | 
						||
        results_changed = self.check_results_changed(stable_results)
 | 
						||
        print(f"结果是否变化: {results_changed}")
 | 
						||
        
 | 
						||
        if results_changed:
 | 
						||
            # 清除之前的结果
 | 
						||
            for i in reversed(range(self.results_layout.count())):
 | 
						||
                child = self.results_layout.itemAt(i).widget()
 | 
						||
                if child:
 | 
						||
                    child.setParent(None)
 | 
						||
            
 | 
						||
            # 添加新的稳定结果
 | 
						||
            for i, result in enumerate(stable_results):
 | 
						||
                plate_widget = LicensePlateWidget(
 | 
						||
                    i + 1,  # 显示序号
 | 
						||
                    result['class_name'],
 | 
						||
                    result['corrected_image'],
 | 
						||
                    result['plate_number']
 | 
						||
                )
 | 
						||
                self.results_layout.addWidget(plate_widget)
 | 
						||
                print(f"添加车牌widget: {result['plate_number']}")
 | 
						||
            
 | 
						||
            # 更新存储的结果
 | 
						||
            self.last_plate_results = stable_results
 | 
						||
        
 | 
						||
        # 处理道闸控制逻辑
 | 
						||
        self.handle_gate_control(stable_results)
 | 
						||
        
 | 
						||
        # 清理旧的车牌记录
 | 
						||
        current_plate_ids = [result['id'] for result in stable_results]
 | 
						||
        self.plate_stabilizer.clear_old_plates(current_plate_ids)
 | 
						||
        
 | 
						||
        # 清理过期的识别框命令发送记录
 | 
						||
        expired_frame_ids = [frame_id for frame_id in self.frame_command_sent.keys() 
 | 
						||
                           if frame_id not in current_plate_ids]
 | 
						||
        for frame_id in expired_frame_ids:
 | 
						||
            del self.frame_command_sent[frame_id]
 | 
						||
        
 | 
						||
        # 清理过期的车牌记录 - 获取当前所有稳定车牌号
 | 
						||
        current_plate_numbers = set()
 | 
						||
        for result in stable_results:
 | 
						||
            plate_number = result.get('plate_number', '')
 | 
						||
            if plate_number and plate_number != "识别失败":
 | 
						||
                current_plate_numbers.add(plate_number)
 | 
						||
        
 | 
						||
        # 清理不再出现的车牌记录
 | 
						||
        expired_plate_numbers = [plate_number for plate_number in self.plate_records.keys() 
 | 
						||
                               if plate_number not in current_plate_numbers]
 | 
						||
        for plate_number in expired_plate_numbers:
 | 
						||
            del self.plate_records[plate_number]
 | 
						||
        
 | 
						||
        print("结果显示更新完成")
 | 
						||
    
 | 
						||
    def check_results_changed(self, new_results):
 | 
						||
        """检查识别结果是否发生变化"""
 | 
						||
        if len(self.last_plate_results) != len(new_results):
 | 
						||
            return True
 | 
						||
        
 | 
						||
        for i, new_result in enumerate(new_results):
 | 
						||
            if i >= len(self.last_plate_results):
 | 
						||
                return True
 | 
						||
            
 | 
						||
            old_result = self.last_plate_results[i]
 | 
						||
            
 | 
						||
            # 比较关键字段
 | 
						||
            if (old_result.get('class_name') != new_result.get('class_name') or 
 | 
						||
                old_result.get('plate_number') != new_result.get('plate_number')):
 | 
						||
                return True
 | 
						||
        
 | 
						||
        return False
 | 
						||
    
 | 
						||
    def correct_license_plate(self, detection):
 | 
						||
        """矫正车牌图像"""
 | 
						||
        if self.current_frame is None:
 | 
						||
            return None
 | 
						||
        
 | 
						||
        # 检查是否为不完整检测
 | 
						||
        if detection.get('incomplete', False):
 | 
						||
            return None
 | 
						||
        
 | 
						||
        # 使用检测器的矫正方法
 | 
						||
        return self.detector.correct_license_plate(
 | 
						||
            self.current_frame, 
 | 
						||
            detection['keypoints']
 | 
						||
        )
 | 
						||
    
 | 
						||
    def recognize_plate_number(self, corrected_image, class_name):
 | 
						||
         """识别车牌号"""
 | 
						||
         if corrected_image is None:
 | 
						||
             return "识别失败"
 | 
						||
         
 | 
						||
         try:
 | 
						||
             # 根据当前选择的识别方法调用相应的函数
 | 
						||
             if self.current_recognition_method == "CRNN":
 | 
						||
                 from CRNN_part.crnn_interface import LPRNmodel_predict
 | 
						||
             elif self.current_recognition_method == "LightCRNN":
 | 
						||
                 from lightCRNN_part.lightcrnn_interface import LPRNmodel_predict
 | 
						||
             elif self.current_recognition_method == "OCR":
 | 
						||
                 from OCR_part.ocr_interface import LPRNmodel_predict
 | 
						||
             
 | 
						||
             # 预测函数(来自模块)
 | 
						||
             result = LPRNmodel_predict(corrected_image)
 | 
						||
             
 | 
						||
             # 将字符列表转换为字符串,支持8位车牌号
 | 
						||
             if isinstance(result, list) and len(result) >= 7:
 | 
						||
                 # 根据车牌类型决定显示位数
 | 
						||
                 if class_name == '绿牌' and len(result) >= 8:
 | 
						||
                     # 绿牌显示8位,过滤掉空字符占位符
 | 
						||
                     plate_chars = [char for char in result[:8] if char != '']
 | 
						||
                     # 如果过滤后确实有8位,显示8位;否则显示7位
 | 
						||
                     if len(plate_chars) == 8:
 | 
						||
                         return ''.join(plate_chars)
 | 
						||
                     else:
 | 
						||
                         return ''.join(plate_chars[:7])
 | 
						||
                 else:
 | 
						||
                     # 蓝牌或其他类型显示前7位,过滤掉空字符
 | 
						||
                     plate_chars = [char for char in result[:7] if char != '']
 | 
						||
                     return ''.join(plate_chars)
 | 
						||
             else:
 | 
						||
                 return "识别失败"
 | 
						||
         except Exception as e:
 | 
						||
             print(f"车牌号识别失败: {e}")
 | 
						||
             return "识别失败"
 | 
						||
    
 | 
						||
    def change_recognition_method(self, method):
 | 
						||
         """切换识别方法"""
 | 
						||
         self.current_recognition_method = method
 | 
						||
         self.current_method_label.setText(f"当前识别方法: {method}")
 | 
						||
         
 | 
						||
         # 初始化对应的模型
 | 
						||
         if method == "CRNN":
 | 
						||
             from CRNN_part.crnn_interface import LPRNinitialize_model
 | 
						||
             LPRNinitialize_model()
 | 
						||
         elif method == "LightCRNN":
 | 
						||
             from lightCRNN_part.lightcrnn_interface import LPRNinitialize_model
 | 
						||
             LPRNinitialize_model()
 | 
						||
         elif method == "OCR":
 | 
						||
             from OCR_part.ocr_interface import LPRNinitialize_model
 | 
						||
             LPRNinitialize_model()
 | 
						||
         
 | 
						||
         # 如果当前有显示的帧,重新处理以更新识别结果
 | 
						||
         if self.current_frame is not None:
 | 
						||
             self.process_frame(self.current_frame)
 | 
						||
    
 | 
						||
    def handle_gate_control(self, stable_results):
 | 
						||
        
 | 
						||
        """处理道闸控制逻辑"""
 | 
						||
        current_time = datetime.now()
 | 
						||
        
 | 
						||
        for result in stable_results:
 | 
						||
            plate_number = result.get('plate_number', '')
 | 
						||
            plate_id = result.get('id')
 | 
						||
 | 
						||
            print(f"车牌ID: {plate_id}, 车牌号: {plate_number}")
 | 
						||
            
 | 
						||
            # 跳过识别失败的车牌
 | 
						||
            if not plate_number or plate_number == "识别失败":
 | 
						||
                continue
 | 
						||
            
 | 
						||
            # 获取该车牌的帧计数,忽略最早的两帧结果
 | 
						||
            frame_count = self.plate_stabilizer.plate_frame_count.get(plate_id, 0)
 | 
						||
            if frame_count <= 2:
 | 
						||
                print(f"车牌ID: {plate_id} 帧数: {frame_count}, 忽略前两帧")
 | 
						||
                continue
 | 
						||
            
 | 
						||
            # 检查该识别框是否已经发送过命令
 | 
						||
            if plate_id in self.frame_command_sent:
 | 
						||
                # 如果该识别框已发送命令,跳过
 | 
						||
                if self.frame_command_sent[plate_id]['command_sent']:
 | 
						||
                    continue
 | 
						||
            else:
 | 
						||
                # 新的识别框,初始化记录
 | 
						||
                self.frame_command_sent[plate_id] = {
 | 
						||
                    'plate_number': plate_number,
 | 
						||
                    'command_sent': False
 | 
						||
                }
 | 
						||
            
 | 
						||
            
 | 
						||
            # 检查白名单
 | 
						||
            if plate_number not in self.whitelist:
 | 
						||
                # 非白名单车牌,发送禁行命令
 | 
						||
                try:
 | 
						||
                    message = f"{plate_number} 禁行"
 | 
						||
                    send_command(4, message)
 | 
						||
                    print(f"发送道闸命令: {message}")
 | 
						||
                    # 标记该识别框已发送命令
 | 
						||
                    self.frame_command_sent[plate_id]['command_sent'] = True
 | 
						||
                except Exception as e:
 | 
						||
                    print(f"发送道闸命令失败: {e}")
 | 
						||
                continue
 | 
						||
            
 | 
						||
            if plate_number in self.plate_records:
 | 
						||
                # 二次识别到同一车牌
 | 
						||
                record = self.plate_records[plate_number]
 | 
						||
                if not record['sent']:
 | 
						||
                    # 计算时间间隔
 | 
						||
                    time_diff = (current_time - record['first_time']).total_seconds()
 | 
						||
                    
 | 
						||
                    # 发送时间间隔命令
 | 
						||
                    message = f"{plate_number} {int(time_diff)}sec"
 | 
						||
                    try:
 | 
						||
                        send_command(1, message)
 | 
						||
                        print(f"发送道闸命令: {message}")
 | 
						||
                        
 | 
						||
                        # 标记该识别框已发送命令
 | 
						||
                        self.frame_command_sent[plate_id]['command_sent'] = True
 | 
						||
                        
 | 
						||
                        # 标记为已发送并清除记录,使第三次识别时重新按首次处理
 | 
						||
                        del self.plate_records[plate_number]
 | 
						||
                        
 | 
						||
                    except Exception as e:
 | 
						||
                        print(f"发送道闸命令失败: {e}")
 | 
						||
            else: 
 | 
						||
                # 首次识别到车牌
 | 
						||
                try:
 | 
						||
                    message = f"{plate_number} 通行"
 | 
						||
                    send_command(1, message)
 | 
						||
                    print(f"发送道闸命令: {message}")
 | 
						||
                    
 | 
						||
                    # 标记该识别框已发送命令
 | 
						||
                    self.frame_command_sent[plate_id]['command_sent'] = True
 | 
						||
                    
 | 
						||
                    # 记录车牌信息
 | 
						||
                    self.plate_records[plate_number] = {
 | 
						||
                        'first_time': current_time,
 | 
						||
                        'sent': False
 | 
						||
                    }
 | 
						||
                    
 | 
						||
                except Exception as e:
 | 
						||
                    print(f"发送道闸命令失败: {e}")
 | 
						||
    
 | 
						||
    def add_to_whitelist(self):
 | 
						||
        """添加车牌到白名单"""
 | 
						||
        plate_number = self.whitelist_input.text().strip().upper()
 | 
						||
        if plate_number and plate_number not in self.whitelist:
 | 
						||
            self.whitelist.add(plate_number)
 | 
						||
            self.whitelist_display.addItem(plate_number)
 | 
						||
            self.whitelist_input.clear()
 | 
						||
            print(f"已添加车牌到白名单: {plate_number}")
 | 
						||
        elif plate_number in self.whitelist:
 | 
						||
            print(f"车牌已在白名单中: {plate_number}")
 | 
						||
        else:
 | 
						||
            print("请输入有效的车牌号")
 | 
						||
    
 | 
						||
    def remove_from_whitelist(self):
 | 
						||
        """从白名单中删除选中的车牌"""
 | 
						||
        current_item = self.whitelist_display.currentItem()
 | 
						||
        if current_item:
 | 
						||
            plate_number = current_item.text()
 | 
						||
            self.whitelist.discard(plate_number)
 | 
						||
            self.whitelist_display.takeItem(self.whitelist_display.row(current_item))
 | 
						||
            print(f"已从白名单中删除车牌: {plate_number}")
 | 
						||
        else:
 | 
						||
            print("请选择要删除的车牌")
 | 
						||
    
 | 
						||
    def manual_open_gate(self):
 | 
						||
        """手动开闸 - 发送命令2"""
 | 
						||
        try:
 | 
						||
            send_command(2, "开闸")
 | 
						||
            print("已发送手动开闸命令")
 | 
						||
        except Exception as e:
 | 
						||
            print(f"发送开闸命令失败: {e}")
 | 
						||
    
 | 
						||
    def manual_close_gate(self):
 | 
						||
        """手动关闸 - 发送命令3"""
 | 
						||
        try:
 | 
						||
            send_command(3, "关闸")
 | 
						||
            print("已发送手动关闸命令")
 | 
						||
        except Exception as e:
 | 
						||
            print(f"发送关闸命令失败: {e}")
 | 
						||
    
 | 
						||
    def closeEvent(self, event):
 | 
						||
        """窗口关闭事件"""
 | 
						||
        if self.camera_thread and self.camera_thread.running:
 | 
						||
            self.camera_thread.stop_camera()
 | 
						||
        if self.video_thread and self.video_thread.running:
 | 
						||
            self.video_thread.stop_video()
 | 
						||
        event.accept()
 | 
						||
 | 
						||
def main():
 | 
						||
    app = QApplication(sys.argv)
 | 
						||
    window = MainWindow()
 | 
						||
    window.show()
 | 
						||
    sys.exit(app.exec_())
 | 
						||
 | 
						||
if __name__ == "__main__":
 | 
						||
    main() |