• QT+Python人脸表情特征识别


    程序示例精选

    QT+Python人脸表情特征识别

    前言

    QT+Python是非常经典的窗体编程组合,功能完善,可视化界面美观易维护,这篇博客针对人脸表情特征识别方面编写代码,代码整洁,规则,易读,对学习与使用Python有较好的帮助。


    文章目录

            一、所需工具软件

            二、使用步骤

                    1. 引入库

                    2. 打开图像软件

                    3. 识别图像特征

                    4. 运行结果

             三在线协助


    一、所需工具软件

              1. Python3.6以上

              2. Pycharm代码编辑器

              3. Qt, Tensorflow, Pandas库

    二、使用步骤

    1.引入库

    代码如下(示例):

    1. # coding:utf-8
    2. import sys
    3. #从转换的.py文件内调用类
    4. import cv2
    5. import numpy as np
    6. import sys
    7. import tensorflow as tf
    8. from untitled import Ui_Dialog
    9. from PyQt5 import QtWidgets
    10. from PyQt5 import QtWidgets, QtCore, QtGui
    11. from PyQt5.QtCore import *

    2.打开图像文件

    代码如下(示例):

    1. def openFileButton(self):
    2. imgName, imgType = QFileDialog.getOpenFileName(self,"打开文件","./","files(*.*)")
    3. img = cv2.imread(imgName)
    4. cv2.imwrite("temp/original.jpg", img)
    5. height, width, pixels = img.shape
    6. print("width,height",width,height)
    7. print("self.label.width()",self.label.width())
    8. print("self.label.height()",self.label.height())
    9. frame = cv2.resize(img, (int(rwidth), int(rheight)))
    10. print("rwidth-elif,rheight-elfi", rwidth, rheight)
    11. img2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # opencv读取的bgr格式图片转换成rgb格式
    12. _image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3, QtGui.QImage.Format_RGB888)
    13. jpg_out = QtGui.QPixmap(_image).scaled(rwidth, rheight) #设置图片大小
    14. self.label.setPixmap(jpg_out) #设置图片显示

    该处使用的url网络请求的数据。

    3.识别图像特征:

    代码如下(示例):

    1. def recogPerson(self):
    2. import os
    3. import cv2
    4. img = cv2.imread("temp/original.jpg")
    5. cv2.imwrite("save/recognPerson2.jpg", img)
    6. face_detect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    7. eye_detect = cv2.CascadeClassifier('haarcascade_eye.xml')
    8. # 灰度处理
    9. gray = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY)
    10. # 检查人脸 按照1.1倍放到 周围最小像素为5
    11. face_zone = face_detect.detectMultiScale(gray,1.3,5)
    12. # print ('识别人脸的信息:\n',face_zone)
    13. l = len(face_zone)
    14. ints = 0
    15. # 绘制矩形和圆形检测人脸
    16. for x, y, w, h in face_zone:
    17. ints += 1
    18. # 绘制矩形人脸区域
    19. if w < 1000:
    20. cv2.rectangle(img, pt1=(x, y), pt2=(x + w, y + h), color=[0, 0, 255], thickness=2)
    21. # 绘制圆形人脸区域 radius表示半径
    22. cv2.circle(img, center=(x + w // 2, y + h // 2), radius=w // 2, color=[0, 255, 0], thickness=2)
    23. roi_face = gray[y:y + h, x:x + w] # 灰度图
    24. roi_color = img[y:y + h, x:x + w] # 彩色图
    25. eyes = eye_detect.detectMultiScale(roi_face)
    26. for (ex, ey, ew, eh) in eyes:
    27. cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
    28. cv2.imwrite("save/recognPerson.jpg", img)
    29. #cv2.waitKey(0)

    4.运行结果如下: 

     

    三、在线协助:

    如需安装运行环境或远程调试,可加扣2945218359, 或扣905733049由专业技术人员远程协助!

  • 相关阅读:
    INDEMIND:产业升级将至,机器人行业迎来新一轮洗牌,谁能抓住先机?
    正确查询DO基站IP
    Python中8种经典数据结构 之 集合
    公司来了个00后,起薪就是18K,不愧是卷王。。。
    败给 VS Code,GitHub 被微软收购的第四年,“杀死”了代码编辑器 Atom
    微信小程序开发【从入门到精通】——页面导航
    Java常问面试题概要答案
    Python解决图文验证码登录识别(1)
    Codeforces Round 895 (Div. 3) A-F
    (C语言)求解二元一次方程组
  • 原文地址:https://blog.csdn.net/alicema1111/article/details/128073797