This commit is contained in:
Viajero 2025-09-04 01:50:49 +08:00
parent 1c8e15bcd8
commit 56e7347c01

45
main.py
View File

@ -3,7 +3,7 @@ import cv2
import numpy as np import numpy as np
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QPushButton, QScrollArea, QFrame, QSizePolicy QLabel, QPushButton, QScrollArea, QFrame, QSizePolicy,QFileDialog
) )
from PyQt5.QtCore import QTimer, Qt, pyqtSignal, QThread from PyQt5.QtCore import QTimer, Qt, pyqtSignal, QThread
from PyQt5.QtGui import QImage, QPixmap, QFont, QPainter, QPen, QColor from PyQt5.QtGui import QImage, QPixmap, QFont, QPainter, QPen, QColor
@ -206,7 +206,12 @@ class MainWindow(QMainWindow):
self.start_button.clicked.connect(self.start_camera) self.start_button.clicked.connect(self.start_camera)
self.stop_button.clicked.connect(self.stop_camera) self.stop_button.clicked.connect(self.stop_camera)
self.stop_button.setEnabled(False) self.stop_button.setEnabled(False)
self.btn_image = QPushButton('选择图片')
self.btn_video = QPushButton('选择视频')
self.btn_image.clicked.connect(self.open_image_file)
self.btn_video.clicked.connect(self.open_video_file)
button_layout.addWidget(self.btn_image)
button_layout.addWidget(self.btn_video)
button_layout.addWidget(self.start_button) button_layout.addWidget(self.start_button)
button_layout.addWidget(self.stop_button) button_layout.addWidget(self.stop_button)
button_layout.addStretch() button_layout.addStretch()
@ -425,6 +430,42 @@ class MainWindow(QMainWindow):
if self.camera_thread: if self.camera_thread:
self.camera_thread.stop_camera() self.camera_thread.stop_camera()
event.accept() event.accept()
def open_image_file(self):
file_path, _ = QFileDialog.getOpenFileName(self, '选择图片', '', '图片文件 (*.jpg *.png)')
if file_path:
image = cv2.imread(file_path)
self.process_image(image)
def open_video_file(self):
file_path, _ = QFileDialog.getOpenFileName(self, '选择视频', '', '视频文件 (*.mp4 *.avi)')
if file_path:
self.cap = cv2.VideoCapture(file_path)
self.video_timer = QTimer()
self.video_timer.timeout.connect(self.process_video_frame)
self.video_timer.start(30)
def process_image(self, image):
self.detections = self.detector.detect_license_plates(image)
display_image = self.draw_detections(image.copy())
self.display_static_image(display_image)
self.update_results_display()
def process_video_frame(self):
ret, frame = self.cap.read()
if ret:
self.process_image(frame)
else:
self.video_timer.stop()
self.cap.release()
def display_static_image(self, image):
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
qt_image = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qt_image)
self.camera_label.setPixmap(pixmap.scaled(self.camera_label.size(), Qt.KeepAspectRatio))
def main(): def main():
app = QApplication(sys.argv) app = QApplication(sys.argv)