桌面客户端点击加载图片,右侧显示加载图片压缩后图片,点击人脸识别按钮,右侧图片框出人脸范围,使用qt生成gui,给出完整python代码
时间: 2023-06-26 16:06:11 浏览: 95
以下是一个简单的使用Qt生成GUI的Python代码,实现了加载图片、压缩图片和人脸识别等功能:
```python
import sys
import cv2
import numpy as np
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QFileDialog
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
class FaceRecognition(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 初始化UI界面
self.setWindowTitle('人脸识别')
self.setGeometry(300, 300, 800, 500)
# 创建控件
self.label_original = QLabel('原图')
self.label_original.setAlignment(Qt.AlignCenter)
self.label_original.setFixedSize(300, 300)
self.label_compressed = QLabel('压缩图')
self.label_compressed.setAlignment(Qt.AlignCenter)
self.label_compressed.setFixedSize(300, 300)
self.label_result = QLabel('识别结果')
self.label_result.setAlignment(Qt.AlignCenter)
self.label_result.setFixedSize(300, 300)
self.btn_load = QPushButton('加载图片')
self.btn_compress = QPushButton('压缩图片')
self.btn_recognize = QPushButton('人脸识别')
# 创建布局
layout_main = QHBoxLayout()
layout_left = QVBoxLayout()
layout_right = QVBoxLayout()
layout_left.addWidget(self.label_original)
layout_left.addWidget(self.btn_load)
layout_left.addWidget(self.btn_compress)
layout_right.addWidget(self.label_compressed)
layout_right.addWidget(self.btn_recognize)
layout_right.addWidget(self.label_result)
layout_main.addLayout(layout_left)
layout_main.addLayout(layout_right)
self.setLayout(layout_main)
# 绑定事件
self.btn_load.clicked.connect(self.load_image)
self.btn_compress.clicked.connect(self.compress_image)
self.btn_recognize.clicked.connect(self.recognize_face)
def load_image(self):
# 加载图片
fname, _ = QFileDialog.getOpenFileName(self, 'Open file', './')
if fname:
img = cv2.imread(fname)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
h, w, ch = img.shape
bytesPerLine = ch * w
qimg = QImage(img.data, w, h, bytesPerLine, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qimg)
self.label_original.setPixmap(pixmap)
# 保存原图
self.image_original = img
def compress_image(self):
# 压缩图片
if hasattr(self, 'image_original'):
self.image_compressed = cv2.resize(self.image_original, (300, 300))
h, w, ch = self.image_compressed.shape
bytesPerLine = ch * w
qimg = QImage(self.image_compressed.data, w, h, bytesPerLine, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qimg)
self.label_compressed.setPixmap(pixmap)
def recognize_face(self):
# 人脸识别
if hasattr(self, 'image_compressed'):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(self.image_compressed, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(self.image_compressed, (x, y), (x + w, y + h), (0, 255, 0), 2)
h, w, ch = self.image_compressed.shape
bytesPerLine = ch * w
qimg = QImage(self.image_compressed.data, w, h, bytesPerLine, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qimg)
self.label_result.setPixmap(pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FaceRecognition()
ex.show()
sys.exit(app.exec_())
```
需要注意的是,上述代码中的人脸识别使用了OpenCV库的CascadeClassifier类,需要使用对应的训练数据文件haarcascade_frontalface_default.xml,可以在OpenCV官网下载。在代码中使用时需要将其放在与Python文件同一目录下。
阅读全文