程序示例精选
Python+Qt人脸识别统计人数窗体
如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!
这篇博客针对《Python+Qt人脸识别统计人数窗体》编写代码,功能包括人脸识别,数量统计。代码整洁,规则,易读。应用推荐首选。
代码如下(示例):
# coding:utf-8
import sys
#从转换的.py文件内调用类
import cv2
from untitled import Ui_Dialog
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
代码如下(示例):
class myWin(QtWidgets.QWidget, Ui_Dialog):
def __init__(self):
super(myWin, self).__init__()
self.setupUi(self)
def openFileButton(self):
imgName, imgType = QFileDialog.getOpenFileName(self,"打开文件","./","files(*.*)")
img = cv2.imread(imgName)
cv2.imwrite("temp/original.jpg", img)
height, width, pixels = img.shape
print("width,height",width,height)
print("self.label.width()",self.label.width())
print("self.label.height()",self.label.height())
if width>(self.label.width()):
rwidth=self.label.width()
print("rwidth-if,rheight-if", width, rheight)
elif height>(self.label.height()):
rheight=self.label.height()
print("rwidth-elif,rheight-elfi", rwidth, rheight)
elif ((self.label.height())-height)<((self.label.width())-width):
rheight=self.label.height()
print("rwidth-elif,rheight-elfi", rwidth, rheight)
else:
print("rheight,rwidth", height, width)
rheight = height
rwidth = width
frame = cv2.resize(img, (int(rwidth), int(rheight)))
print("rwidth-elif,rheight-elfi", rwidth, 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) #设置图片显示
def saveFileButton(self):
img = cv2.imread("temp/original.jpg")
file_path = QFileDialog.getSaveFileName(self, "save file", "./save/test","jpg files (*.jpg);;all files(*.*)")
print(file_path[0])
cv2.imwrite(file_path[0], img)
cv2.waitKey(0)
cv2.destroyAllWindows()
代码如下(示例):
def recogPerson(self):
import cv2
img = cv2.imread("temp/original.jpg")
face_detect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_detect = cv2.CascadeClassifier('haarcascade_eye.xml')
# 灰度处理
gray = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY)
# 检查人脸 按照1.1倍放到 周围最小像素为5
face_zone = face_detect.detectMultiScale(gray,1.3,5)
# print ('识别人脸的信息:\n',face_zone)
ints = 0
# 绘制矩形和圆形检测人脸
for x, y, w, h in face_zone:
ints += 1
# 绘制矩形人脸区域
if w < 101:
cv2.rectangle(img, pt1=(x, y), pt2=(x + w, y + h), color=[0, 0, 255], thickness=2)
# 绘制圆形人脸区域 radius表示半径
cv2.circle(img, center=(x + w // 2, y + h // 2), radius=w // 2, color=[0, 255, 0], thickness=2)
roi_face = gray[y:y + h, x:x + w] # 灰度图
roi_color = img[y:y + h, x:x + w] # 彩色图
eyes = eye_detect.detectMultiScale(roi_face)
font1 = "Current number:";
font2 = "pcs";
font = cv2.FONT_HERSHEY_TRIPLEX # 使用默认字体
cv2.putText(img, font1 + str(ints) + font2, (10, 28), font, 1.2, (0, 255, 0), 1)
#cv2.namedWindow("Easmount-CSDN", 0)
#cv2.imshow("Easmount-CSDN", img)
cv2.imwrite("save/recognPerson.jpg", img)
#cv2.waitKey(0)
#显示人数到窗体
self.textEdit.setPlainText(str(ints))
#self.textEdit.setPlainText('Hello PyQt5!\n单击按钮')
#显示相片到label_2
img = cv2.imread("save/recognPerson.jpg")
rheight = height
rwidth = width
frame = cv2.resize(img, (int(rwidth), int(rheight)))
img2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # opencv读取的bgr格式图片转换成rgb格式
jpg_out = QtGui.QPixmap(_image).scaled(rwidth, rheight) #设置图片大小
self.label_2.setPixmap(jpg_out) #设置图片显示
if __name__=="__main__":
app=QtWidgets.QApplication(sys.argv)
Widget=myWin()
Widget.show()
sys.exit(app.exec_())
如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!