这是一个相对复杂的项目,需要使用多个技术和模块来实现。以下是一个简单的示例代码,可以使用 Python 和 PyQt 实现一个简单的智能停车场管理系统。文章来源:https://www.toymoban.com/news/detail-515901.html
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QLineEdit, QPushButton, QMessageBox, QFileDialog
from PyQt5.QtGui import QPixmap
import mysql.connector
import cv2
import pytesseract
import datetime
class LoginWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("智能停车场管理系统-登录")
self.setGeometry(100, 100, 600, 400)
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.username_label = QLabel("用户名:", self.central_widget)
self.username_label.move(150, 100)
self.username_input = QLineEdit(self.central_widget)
self.username_input.move(220, 100)
self.password_label = QLabel("密码:", self.central_widget)
self.password_label.move(150, 150)
self.password_input = QLineEdit(self.central_widget)
self.password_input.move(220, 150)
self.password_input.setEchoMode(QLineEdit.Password)
self.login_button = QPushButton("登录", self.central_widget)
self.login_button.move(250, 200)
self.login_button.clicked.connect(self.login)
def login(self):
username = self.username_input.text()
password = self.password_input.text()
if username == "admin" and password == "123456":
self.hide()
self.main_window = MainWindow()
self.main_window.show()
else:
QMessageBox.warning(self, "错误", "用户名或密码错误!")
class RegisterWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("智能停车场管理系统-注册")
self.setGeometry(100, 100, 600, 400)
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.username_label = QLabel("用户名:", self.central_widget)
self.username_label.move(150, 100)
self.username_input = QLineEdit(self.central_widget)
self.username_input.move(220, 100)
self.password_label = QLabel("密码:", self.central_widget)
self.password_label.move(150, 150)
self.password_input = QLineEdit(self.central_widget)
self.password_input.move(220, 150)
self.password_input.setEchoMode(QLineEdit.Password)
self.confirm_password_label = QLabel("确认密码:", self.central_widget)
self.confirm_password_label.move(150, 200)
self.confirm_password_input = QLineEdit(self.central_widget)
self.confirm_password_input.move(220, 200)
self.confirm_password_input.setEchoMode(QLineEdit.Password)
self.register_button = QPushButton("注册", self.central_widget)
self.register_button.move(250, 250)
self.register_button.clicked.connect(self.register)
def register(self):
username = self.username_input.text()
password = self.password_input.text()
confirm_password = self.confirm_password_input.text()
if password != confirm_password:
QMessageBox.warning(self, "错误", "两次输入的密码不一致!")
else:
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="dbname"
)
mycursor = mydb.cursor()
sql = "INSERT INTO users (username, password) VALUES (%s, %s)"
val = (username, password)
mycursor.execute(sql, val)
mydb.commit()
QMessageBox.information(self, "成功", "注册成功!")
self.hide()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("智能停车场管理系统-主页")
self.setGeometry(100, 100, 800, 600)
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.camera_label = QLabel(self.central_widget)
self.camera_label.setGeometry(50, 50, 640, 480)
self.start_button = QPushButton("开始", self.central_widget)
self.start_button.setGeometry(50, 550, 100, 30)
self.start_button.clicked.connect(self.start_camera)
self.stop_button = QPushButton("停止", self.central_widget)
self.stop_button.setGeometry(200, 550, 100, 30)
self.stop_button.clicked.connect(self.stop_camera)
self.capture_button = QPushButton("拍照", self.central_widget)
self.capture_button.setGeometry(350, 550, 100, 30)
self.capture_button.clicked.connect(self.capture_image)
self.plate_label = QLabel("车牌号码:", self.central_widget)
self.plate_label.setGeometry(500, 550, 100, 30)
self.plate_input = QLineEdit(self.central_widget)
self.plate_input.setGeometry(600, 550, 100, 30)
self.in_button = QPushButton("入库", self.central_widget)
self.in_button.setGeometry(50, 500, 100, 30)
self.in_button.clicked.connect(self.in_park)
self.out_button = QPushButton("出库", self.central_widget)
self.out_button.setGeometry(200, 500, 100, 30)
self.out_button.clicked.connect(self.out_park)
self.logout_button = QPushButton("注销", self.central_widget)
self.logout_button.setGeometry(650, 20, 100, 30)
self.logout_button.clicked.connect(self.logout)
self.timer = None
self.cap = None
self.is_camera_running = False
self.image_count = 0
def start_camera(self):
self.cap = cv2.VideoCapture(0)
self.is_camera_running = True
self.timer = self.central_widget.startTimer(20)
def stop_camera(self):
if self.cap:
self.cap.release()
self.is_camera_running = False
self.timer = None
self.camera_label.clear()
def capture_image(self):
if self.is_camera_running:
ret, frame = self.cap.read()
if ret:
self.image_count += 1
filename = "image_{}.jpg".format(self.image_count)
cv2.imwrite(filename, frame)
self.camera_label.setPixmap(QPixmap(filename))
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 3)
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
plate = pytesseract.image_to_string(gray, config="--psm 7")
self.plate_input.setText(plate.strip())
def in_park(self):
plate = self.plate_input.text()
if plate:
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="dbname"
)
mycursor = mydb.cursor()
now = datetime.datetime.now()
sql = "INSERT INTO park_record (plate, in_time) VALUES (%s, %s)"
val = (plate, now.strftime("%Y-%m-%d %H:%M:%S"))
mycursor.execute(sql, val)
mydb.commit()
QMessageBox.information(self, "成功", "车辆已入库!")
self.plate_input.clear()
def out_park(self):
plate = self.plate_input.text()
if plate:
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="dbname"
)
mycursor = mydb.cursor()
now = datetime.datetime.now()
sql = "UPDATE park_record SET out_time = %s WHERE plate = %s AND out_time IS NULL"
val = (now.strftime("%Y-%m-%d %H:%M:%S"), plate)
mycursor.execute(sql, val)
mydb.commit()
if mycursor.rowcount > 0:
QMessageBox.information(self, "成功", "车辆已出库!")
self.plate_input.clear()
else:
QMessageBox.warning(self, "错误", "车牌号码不存在或已出库!")
def logout(self):
self.hide()
self.login_window = LoginWindow()
self.login_window.show()
def timerEvent(self, event):
if self.is_camera_running:
ret, frame = self.cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = frame.shape
bytesPerLine = ch * w
qImg = QImage(frame.data, w, h, bytesPerLine, QImage.Format_RGB888)
self.camera_label.setPixmap(QPixmap.fromImage(qImg))
if __name__ == '__main__':
app = QApplication(sys.argv)
login_window = LoginWindow()
login_window.show()
sys.exit(app.exec_())
在上面的示例代码中,我们使用了 PyQt5 库来创建 GUI 界面,使用了 OpenCV 库来读取摄像头数据,并使用了 PyTesseract 库来识别车牌号码。同时,我们使用了 MySQL 数据库来存储车辆入库和出库记录。在程序运行时,首先会显示登录界面,用户可以输入用户名和密码来登录系统。如果登录成功,程序将转到主页界面,用户可以在主页界面中开启摄像头、拍照、识别车牌、入库或出库车辆,并可以注销用户退出系统。在程序中,我们使用了多个类来分别实现不同的功能,使代码更加清晰和易于维护。文章来源地址https://www.toymoban.com/news/detail-515901.html
到了这里,关于智能停车场系统:基于 pyqt5,opencv,MySQL的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!