python qml opencv显示图像

python qml opencv显示图像,第1张

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

Window {
    id: window
    width: 500
    height: 400
    visible: true
    title: qsTr("主窗体")

    Connections{
       target: backend
    }
    Image {
        id: image
        x: 37
        y: 56
        width: 100
        height: 100
        anchors.fill : parent
        asynchronous:true
        source: "image://myprovider/test.png"+Math.random()
        fillMode: Image.PreserveAspectFit
    }

    Button {
        id: button
        x: 43
        y: 208
        text: qsTr("Button")
        onClicked: {
        image.source = "image://myprovider/"+ Math.random()
        //backend.setImage()
        }
    }
}

main.py

import os
from pathlib import Path
import sys

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtQml import *
from PySide2.QtQuick import *
import cv2

class MyImageProvider(QQuickImageProvider):
    def __init__(self):
        super(MyImageProvider, self).__init__(QQuickImageProvider.Image)
        self.capture = cv2.VideoCapture(0)  # 创建相机

    def requestImage(self,p_str, size,u):

        _,frame = self.capture.read()     # this is a numpy.ndarray object
        height, width, depth = frame.shape
        cvimg = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        cvimg = QImage(cvimg.data, width, height, width * depth, QImage.Format_RGB888)

        print("1")
        #return QPixmap(cvimg)
        return cvimg

if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    main = qmltopy()
    engine.addImageProvider("myprovider", MyImageProvider())
    engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml"))
	if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/568569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存