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())
相关推荐
梧桐树042925 分钟前
python常用内建模块:collections
python
Dream_Snowar33 分钟前
速通Python 第三节
开发语言·python
mahuifa2 小时前
混合开发环境---使用编程AI辅助开发Qt
人工智能·vscode·qt·qtcreator·编程ai
冷眼看人间恩怨2 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
蓝天星空2 小时前
Python调用open ai接口
人工智能·python
jasmine s2 小时前
Pandas
开发语言·python
郭wes代码2 小时前
Cmd命令大全(万字详细版)
python·算法·小程序
leaf_leaves_leaf2 小时前
win11用一条命令给anaconda环境安装GPU版本pytorch,并检查是否为GPU版本
人工智能·pytorch·python
夜雨飘零12 小时前
基于Pytorch实现的说话人日志(说话人分离)
人工智能·pytorch·python·声纹识别·说话人分离·说话人日志
404NooFound3 小时前
Python轻量级NoSQL数据库TinyDB
开发语言·python·nosql