Python在QtSide6(PyQt)上加载网页使用OpenCV进行图像处理

基本思路:

1.在Qt Designer中添加QWebEngineView,该组件可用于加载网页

2.在python中开启Timer事件,每10ms进行一次网页窗口截图(QWidget.grab)

3.将截图(QPixmap)转换为cv.mat,进行图像处理

运行效果,右图为OpenCV画了一个圆

ui.py代码

python 复制代码
# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'untitledNReTow.ui'
##
## Created by: Qt User Interface Compiler version 6.7.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWidgets import (QApplication, QDialog, QPushButton, QSizePolicy,
    QWidget)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        if not Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(974, 687)
        self.webEngineView = QWebEngineView(Dialog)
        self.webEngineView.setObjectName(u"webEngineView")
        self.webEngineView.setGeometry(QRect(0, 10, 961, 611))
        self.webEngineView.setUrl(QUrl(u"https://open.ys7.com/ezopen/h5/iframe?url=ezopen://open.ys7.com/AW6277952/1.live&autoplay=1&accessToken=at.7hthi2pb71l35ssa0j4f7b9e8nmecl8r-2a50tvzyqk-06ad9jw-6yavo9soe"))
        self.pushButton = QPushButton(Dialog)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(890, 660, 75, 24))

        self.retranslateUi(Dialog)

        QMetaObject.connectSlotsByName(Dialog)
    # setupUi

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
        self.pushButton.setText(QCoreApplication.translate("Dialog", u"play", None))
    # retranslateUi

main.py代码

python 复制代码
from symtable import Class

from ui import Ui_Dialog
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton
from PySide6 import QtCore, QtGui
from PySide6.QtGui import QPixmap
from PySide6.QtCore import QRect, QPoint, QSize
from PySide6.QtCore import QTimer
import cv2
import numpy as np

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

    def closeEvent(self, event):
        print("close")

        mytimer.stop()
        cv2.destroyAllWindows()

def getImage():
    # 将QWidget转换为图片
    width = auto_ui_window.webEngineView.size().width()
    height = auto_ui_window.webEngineView.size().height()
    pixmap = auto_ui_window.webEngineView.grab(QRect(QPoint(0, 0), QSize(width,height)))

    # 保存图片到文件
    cvimage = qtpixmap_to_cvimg(pixmap)
    mat_img = cv2.cvtColor(cvimage, cv2.COLOR_BGR2BGRA)
    cv2.circle(mat_img,  (320, 240), 20, (255, 0, 0,255) , 2)
    cv2.imshow("image", mat_img)

def on_clicked_play():
    print("on_clicked_play")
    mytimer.start(10)

def qtpixmap_to_cvimg(qtpixmap):
    qimg = qtpixmap.toImage()
    temp_shape = (qimg.height(), qimg.bytesPerLine() * 8 // qimg.depth())
    temp_shape += (4,)
    ptr = qimg.bits()
    result = np.array(ptr, dtype=np.uint8).reshape(temp_shape)
    result = result[..., :3]
    return result

if __name__ == "__main__":
    app = QApplication(sys.argv)

    mytimer = QTimer()
    mytimer.timeout.connect(getImage)

    main_window = Main()
    auto_ui_window = Ui_Dialog()
    auto_ui_window.setupUi(main_window)

    #事件处理方法1
    auto_ui_window.pushButton.clicked.connect(on_clicked_play)

    # 事件处理方法2
    #QtCore.QObject.connect(auto_ui_window.pushButton, QtCore.SIGNAL("clicked()"), main_window,QtCore.SLOT('on_clicked2()'))
    main_window.show()

    app.exec()
    sys.exit(app.exec())
相关推荐
巷95543 分钟前
OpenCV图像形态学:原理、操作与应用详解
人工智能·opencv·计算机视觉
带娃的IT创业者1 小时前
《Python实战进阶》No39:模型部署——TensorFlow Serving 与 ONNX
pytorch·python·tensorflow·持续部署
Bruce-li__1 小时前
深入理解Python asyncio:从入门到实战,掌握异步编程精髓
网络·数据库·python
阿linlin1 小时前
OpenCV--图像预处理学习01
opencv·学习·计算机视觉
九月镇灵将2 小时前
6.git项目实现变更拉取与上传
git·python·scrapy·scrapyd·gitpython·gerapy
小张学Python2 小时前
AI数字人Heygem:口播与唇形同步的福音,无需docker,无需配置环境,一键整合包来了
python·数字人·heygem
跳跳糖炒酸奶2 小时前
第四章、Isaacsim在GUI中构建机器人(2):组装一个简单的机器人
人工智能·python·算法·ubuntu·机器人
步木木3 小时前
Anaconda和Pycharm的区别,以及如何选择两者
ide·python·pycharm
星始流年3 小时前
解决PyInstaller打包PySide6+QML应用的资源文件问题
python·llm·pyspider
南玖yy3 小时前
Python网络爬虫:从入门到实践
爬虫·python