程序示例精选
Python停车场车牌识别计费管理系统
如需安装运行环境或远程调试,见文章底部微信名片!
QT+Python是非常经典的窗体编程组合,功能完善,可视化界面美观易维护,这篇博客针对停车场车牌识别计费方面编写代码,代码整洁,规则,易读,对学习与使用Python有较好的帮助。
代码如下(示例):
# coding:utf-8
import csv
import cv2
import numpy as np
from PIL import Image, ImageTk
from tensorflow import keras
from core import locate_and_correct
from Unet import unet_predict
from CNN import cnn_predict
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
代码如下(示例):
def displayPlate(self):
# 显示相片到label_2
imgName, imgType = QFileDialog.getOpenFileName(self,"打开文件","./","files(*.*)")
img = cv2.imread(imgName)
#img = cv2.imread("test_img/aa.jpg")
cv2.imwrite('temp.jpg', img)
height, width, pixels = img.shape
frame = cv2.resize(img, (int(rwidth), int(rheight)))
img2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # opencv读取的bgr格式图片转换成rgb格式
_image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3, QtGui.QImage.Format_RGB888)
jpg_out = QtGui.QPixmap(_image).scaled(rwidth, rheight) # 设置图片大小
self.label.setPixmap(jpg_out) # 设置图片显示
该处使用的url网络请求的数据。
代码如下(示例):
def carPlateRecogIn(self):
print("carPlateRecog start")
self.img_src_path="temp.jpg"
img_src = cv2.imdecode(np.fromfile(self.img_src_path, dtype=np.uint8), -1) # 从中文路径读取时用
h, w = img_src.shape[0], img_src.shape[1]
print("aa")
if h * w <= 240 * 80 and 2 <= w / h <= 5: # 满足该条件说明可能整个图片就是一张车牌,无需定位,直接识别即可
lic = cv2.resize(img_src, dsize=(240, 80), interpolation=cv2.INTER_AREA)[:, :, :3] # 直接resize为(240,80)
img_src_copy, Lic_img = img_src, [lic]
else: # 否则就需通过unet对img_src原图预测,得到img_mask,实现车牌定位,然后进行识别
img_src, img_mask = unet_predict(self.unet, self.img_src_path)
img_src_copy, Lic_img = locate_and_correct(img_src, img_mask) # 利用core.py中的locate_and_correct函数进行车牌定位和矫正
print("aa")
Lic_pred = cnn_predict(self.cnn, Lic_img) # 利用cnn进行车牌的识别预测,Lic_pred中存的是元祖(车牌图片,识别结果)
if Lic_pred:
# 显示相片到label_2
img = cv2.imread("temp.jpg")
self.label.setPixmap(QPixmap(""))
height, width, pixels = img.shape
frame = cv2.resize(img, (int(rwidth), int(rheight)))
img2 = cv2.cvtColor(img_src_copy, cv2.COLOR_BGR2RGB) # opencv读取的bgr格式图片转换成rgb格式
_image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3, QtGui.QImage.Format_RGB888)
jpg_out = QtGui.QPixmap(_image).scaled(rwidth, rheight) # 设置图片大小
self.label.setPixmap(jpg_out) # 设置图片显示
for i, lic_pred in enumerate(Lic_pred):
if i == 0:
print("i: ", i)
print("lic_pred[1]: ",lic_pred[1])
self.textEdit.setPlainText("车牌编号:"+lic_pred[1])
elif i == 1:
print("i: ", i)
self.textEdit.setPlainText("车牌编号:"+lic_pred[1])
elif i == 2:
print("i: ", i)
self.textEdit.setPlainText("车牌编号:"+lic_pred[1])
plateNumber=lic_pred[1]
print("lic_pred[1]2: ", lic_pred[1])
msg_box = QMessageBox(QMessageBox.Warning, '信息', '门已打开,请进入.....')
msg_box.exec_()
else: # Lic_pred为空说明未能识别
print("无车牌发现")
msg_box = QMessageBox(QMessageBox.Warning, '信息', '停车费已收取,请出门.....')
msg_box.exec_()
如需安装运行环境或远程调试,见文章底部微信名片!