图片车辆识别
======
根据文章搭建好环境后开始进行做项目[link](https://blog.csdn.net/mini_new_w/article/details/108314368)
```cpp
import sys
import cv2
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QPalette, QPixmap, QBrush, QRegExpValidator
class mainWin(QWidget):
def __init__(self):
"""
构造函数
"""
super().__init__()
self.initUI()
self.openBtn.clicked.connect(self.openFile) # 信号和槽
self.grayBtn.clicked.connect(self.imgGray) # 信号和槽
self.carCheckBtn.clicked.connect(self.carCheck)
def initUI(self):
# 设置窗口得大小
self.setFixedSize(860, 600)
# 图标和背景
self.setWindowTitle("车辆检测")
self.setWindowIcon(QIcon("img/icon.jpg")) # 图标
# 标签
self.leftLab = QLabel("原图:", self)
self.leftLab.setGeometry(10, 50, 400, 400) # 设置绝对位置
self.leftLab.setStyleSheet("background:white")
self.newLab = QLabel("新图:", self)
self.newLab.setGeometry(420, 50, 400, 400) # 设置绝对位置
self.newLab.setStyleSheet("background-color:white")
# 按钮
self.openBtn = QPushButton(" 打开文件", self)
self.openBtn.setGeometry(10, 10, 80, 30)
self.grayBtn = QPushButton(" 灰度处理", self)
self.grayBtn.setGeometry(100, 10, 80, 30)
self.carCheckBtn = QPushButton(" 视频检测", self)
self.carCheckBtn.setGeometry(200, 10, 80, 30)
```
打开文件方法
```cpp
def openFile(self):
"""
打开文件的处理函数
:return;
:return:
"""
print("打开图片")
self.img,imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;ALL FILES(*)")
print(self.img)
#jpg = QPixmap(self.img)
self.leftLab.setPixmap(QPixmap(self.img))
self.leftLab.setScaledContents(True)
```
图像变灰度并车辆识别方法
外汇出入金流程https://www.fx61.com/support
```cpp
def imgGray(self):
print("灰度")
img1 = cv2.imread(self.img)
#1. 灰度化处理
img_gray = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
# BGR = cv2.cvtColor(module,cv2.COLOR_BGR2RGB)# 转化为RGB格式
# ret,thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)#二值化
#2. 加载级联分类器
car_detector = cv2.CascadeClassifier("./cars.xml")
"""
image--图片像素数据
scaleFactor=None,缩放比例
minNeighbors=None,2 写2就是3
flags =None, 标志位 用什么来进行检测
minSize=None,最小的尺寸
maxSize=None,最大的尺寸
self, image, scaleFactor=None, minNeighbors=None, flags=None, minSize=None, maxSize=None
"""
#3. 检测车辆 多尺度检测,得到车辆的坐标定位
cars = car_detector.detectMultiScale(img_gray, 1.05, 2, cv2.CASCADE_SCALE_IMAGE, (20,20), (100,100))
print(cars)
#(274 46 28 28) --(x,y,w,h)
#4. 在车的定位上画图
for(x, y, w, h) in cars:
print(x, y, w, h)
#img, pt1, pt2, color, thickness = None, lineType = None, shift = None
cv2.rectangle(img1,(x,y), (x+w, y+h), (255, 255, 255), 1, cv2.LINE_AA)
# 保存图片
img_gray_name = "3.png" # 文件名
cv2.imwrite(img_gray_name, img1) # 保存
# 显示再控件上面
self.newLab.setPixmap(QPixmap(img_gray_name))
self.newLab.setScaledContents(True)
```
视频车辆识别
======
视频打开且识别方法
```cpp
def carCheck(self):
print("车流检测")
# parent: QWidget = None, caption: str = '', directory: str = '', filter:
#1. 选择视频
video, videoType = QFileDialog.getOpenFileName(self, "打开视频", "", "*.mp4")
print(video, videoType)
# video --打开的视频filename
#2. 读取加载视频
cap = cv2.VideoCapture(video)
#3.读取一帧图片
while True:
status,img = cap.read()
if status:
# 灰度
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# 2. 加载级联分类器
car_detector = cv2.CascadeClassifier("./cars.xml")
cars = car_detector.detectMultiScale(gray, 1.2, 2, cv2.CASCADE_SCALE_IMAGE, (25, 25), (200, 200))
# 画框框
for (x, y, w, h) in cars:
print(x, y, w, h)
# img, pt1, pt2, color, thickness = None, lineType = None, shift = None
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 1, cv2.LINE_AA)
print("实时车流量", len(cars))
text = 'car number: '+str(len(cars))
# 添加文字
cv2.putText(img, text, (350, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 255, 0), 2)
cv2.imshow("opencv", img)
key = cv2.waitKey(10) # 延时并且监听按键
if key == 27:
break
else:
break
# 释放资源
cap.release()
cv2.destroyAllWindows()
```
主函数
```cpp
if __name__ == "__main__":
app = QApplication(sys.argv) #创建一个应用程序
win = mainWin() #实例化对象
win.show() #显示窗口
sys.exit(app.exec_())
``` |
|