這是一個相對復(fù)雜的項目,需要使用多個技術(shù)和模塊來實現(xiàn)。以下是一個簡單的示例代碼,可以使用 Python 和 PyQt 實現(xiàn)一個簡單的智能停車場管理系統(tǒng)。文章來源:http://www.zghlxwxcb.cn/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("智能停車場管理系統(tǒng)-登錄")
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("智能停車場管理系統(tǒng)-注冊")
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("確認(rèn)密碼:", 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("智能停車場管理系統(tǒng)-主頁")
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 庫來創(chuàng)建 GUI 界面,使用了 OpenCV 庫來讀取攝像頭數(shù)據(jù),并使用了 PyTesseract 庫來識別車牌號碼。同時,我們使用了 MySQL 數(shù)據(jù)庫來存儲車輛入庫和出庫記錄。在程序運(yùn)行時,首先會顯示登錄界面,用戶可以輸入用戶名和密碼來登錄系統(tǒng)。如果登錄成功,程序?qū)⑥D(zhuǎn)到主頁界面,用戶可以在主頁界面中開啟攝像頭、拍照、識別車牌、入庫或出庫車輛,并可以注銷用戶退出系統(tǒng)。在程序中,我們使用了多個類來分別實現(xiàn)不同的功能,使代碼更加清晰和易于維護(hù)。文章來源地址http://www.zghlxwxcb.cn/news/detail-515901.html
到了這里,關(guān)于智能停車場系統(tǒng):基于 pyqt5,opencv,MySQL的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!