图像行人检测GUI代码
时间: 2023-11-12 21:12:48 浏览: 99
行人检测代码
5星 · 资源好评率100%
以下是一个基于PyQt5和OpenCV的图像行人检测GUI代码示例:
```python
import sys
import cv2
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QDialog, QFileDialog
from ui_mainwindow import Ui_MainWindow
class MainWindow(QDialog):
def __init__(self):
super().__init__()
# 初始化UI
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# 绑定事件
self.ui.pushButton.clicked.connect(self.open_image)
self.ui.pushButton_2.clicked.connect(self.detect_people)
self.image = None
def open_image(self):
# 打开图像
filename, _ = QFileDialog.getOpenFileName(self, "Open Image", "", "Image Files (*.png *.jpg *.bmp)")
if filename:
self.image = cv2.imread(filename)
self.display_image(self.image)
def detect_people(self):
# 行人检测
if self.image is not None:
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# 缩放图像以提高检测速度
scale = 1.05
min_neighbors = 3
flags = cv2.CASCADE_SCALE_IMAGE
gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
rects, _ = hog.detectMultiScale(gray, scale, minNeighbors=min_neighbors,flags=flags)
# 在图像上绘制检测框
for (x, y, w, h) in rects:
cv2.rectangle(self.image, (x, y), (x+w, y+h), (0, 0, 255), 2)
# 显示结果
self.display_image(self.image)
def display_image(self, image):
# 将OpenCV图像转换为QPixmap
qimage = QImage(image.data, image.shape[1], image.shape[0], QImage.Format_RGB888)
qpixmap = QPixmap.fromImage(qimage)
# 显示图像
self.ui.label.setPixmap(qpixmap)
self.ui.label.setAlignment(Qt.AlignCenter)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
此代码使用了一个名为`ui_mainwindow.py`的UI文件。下面是该文件的内容:
```python
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setText("")
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Open Image"))
self.pushButton_2.setText(_translate("MainWindow", "Detect People"))
```
请注意,此示例仅用于演示目的,实际应用中可能需要进行更多的错误处理和性能优化。
阅读全文