持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第19天,点击查看活动详情
目前许多车辆检测实例已经非常完善,yolov5对于车辆的检测准确度已经非常高,但是其框架相对来说还是比较大的,相对于opencv来说显得就有些笨重了,许多时候我们利用预训练好的文件,使用opencv也可以完成一系列的检测过程,例如人脸检测时,opencv自带的一些关于人脸的权重数据,我们直接可以使用,车辆检测也一样,关于下面代码中所使用的车辆检测的预训练文件我放到了百度网盘,有需要的可以自行下载。(链接:pan.baidu.com/s/14V0VCSKU… 提取码:jxzv)
下面先上程序的界面文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'carDetect.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(810, 638)
self.horizontalLayoutWidget = QtWidgets.QWidget(Form)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 791, 31))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.openImg = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.openImg.setObjectName("openImg")
self.horizontalLayout.addWidget(self.openImg)
self.carDetect = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.carDetect.setObjectName("carDetect")
self.horizontalLayout.addWidget(self.carDetect)
self.closeWindow = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.closeWindow.setObjectName("closeWindow")
self.horizontalLayout.addWidget(self.closeWindow)
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Form)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(10, 40, 791, 591))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.retranslateUi(Form)
self.closeWindow.clicked.connect(Form.close) # type: ignore
self.carDetect.clicked.connect(Form.detect) # type: ignore
self.openImg.clicked.connect(Form.openImage) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.openImg.setText(_translate("Form", "打开图片"))
self.carDetect.setText(_translate("Form", "车辆检测"))
self.closeWindow.setText(_translate("Form", "关闭窗口"))
self.label.setText(_translate("Form", "TextLabel"))
相对来说整个工程还是非常简单的,下面是主函数
from carDetect import Ui_Form
from PyQt5.QtWidgets import QApplication, QMainWindow, QCheckBox
from PyQt5.QtWidgets import QFileDialog
import sys
import cv2
from PyQt5.QtGui import *
class MainWindow(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
self.imgPath = None
def openImage(self):
try:
self.imgPath = QFileDialog.getOpenFileNames(self, "select file", "./", "*.*")
except Exception as e:
print(e)
img = cv2.imread(self.imgPath[0][0])
img_dis = QImage(img, img.shape[1], img.shape[0], QImage.Format_RGB888)
img = QPixmap(img_dis)
self.label.setScaledContents(True)
self.label.setPixmap(img)
def detect(self):
car_cascade = cv2.CascadeClassifier('cars.xml')
frames = cv2.imread(self.imgPath[0][0])
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray, 1.1, 9)
for (x, y, w, h) in cars:
plate = frames[y:y + h, x:x + w]
cv2.rectangle(frames, (x, y), (x + w, y + h), (51, 51, 255), 2)
cv2.rectangle(frames, (x, y - 40), (x + w, y), (51, 51, 255), -2)
cv2.putText(frames, 'Car', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
img_dis = QImage(frames, frames.shape[1], frames.shape[0], QImage.Format_RGB888)
img = QPixmap(img_dis)
self.label.setScaledContents(True)
self.label.setPixmap(img)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
程序运行时的界面如下
估计是因为训练权重文件的问题,导致检测的结果并不理想,但是在训练时看结果感觉还可以,保存后通过opencv调用并识别的结果并不是很理想。
可以通过自己的训练,将文件替换为自己的测试一下,可能是训练过程不理想导致的。