我正在尝试使用USB摄像头来显示PyQt5应用程序的实时视频。我有一个wx包装器的工作代码,但我需要在PyQt5中工作。经过多次搜索,我只是找不到正确的语法。

这是有效的WX代码:

import wx
from wx.lib.activexwrapper import MakeActiveXClass
from win32com.client import gencache

class mainFrm( wx.Frame ):
    def __init__( self, *args, **kwds ):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__( self, *args, **kwds )

        self.dcamModule = gencache.EnsureModule( '{6B9BD678-9710-44D9-9282-A088094E4216}', 0, 1, 0 )
        dcamClass = MakeActiveXClass( self.dcamModule.ActiveGeni, eventObj = self )
        self.camera = dcamClass( self, -1 )

        self.camera.SetSize(( 752, 480 ))
        self.SetClientSize( ( 752, 480 ))

        self.camera.Acquire = True
        self.camera.Display = True


if __name__ == '__main__':
    GUI = wx.PySimpleApp( 0 )
    frame_1 = mainFrm( None, -1, "" )
    GUI.SetTopWindow( frame_1 )
    frame_1.Show()
    GUI.MainLoop()


当我调试发生的事情时,这就是构建对象时得到的:

print(self.dcamModule)
<module 'win32com.gen_py.6B9BD678-9710-44D9-9282-A088094E4216x0x1x0' from '...\\AppData\\Local\\Temp\\3\\gen_py\\3.5\\6B9BD678-9710-44D9-9282-A088094E4216x0x1x0.py'>

print(dcamClass)
<class 'wx.lib.activexwrapper.AXControl_ActiveGeni'>

print(self.camera)
<win32com.gen_py.None.AXControl_ActiveGeni>


这是我尝试过的PyQt5。 Is不会给出错误,但也不会启动相机:

import sys
from PyQt5 import uic, QtWidgets
from PyQt5.QAxContainer import QAxWidget

qtCreatorFile = "ui\\camera_form.ui"
LandingPageUI, LandingPageBase = uic.loadUiType(qtCreatorFile)

class cameraForm(LandingPageBase, LandingPageUI):

    def __init__(self,  parent=None):
        QtWidgets.QMainWindow.__init__(self)
        LandingPageBase.__init__(self)
        self.setupUi(self)

        self.ocx = QAxWidget("'{6B9BD678-9710-44D9-9282-A088094E4216}', 0, 1, 0 ")
#Is there something else to do here?
        self.ocx.Acquire = True
        self.ocx.Display = True
        self.axWidget = self.ocx     #axWidget is the QaXWidget on the form


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

    window = cameraForm()
    window.show()
    sys.exit(app.exec_())




当我尝试PyQt版本时,这是调试时得到的信息:

print(self.axWidget)
<PyQt5.QAxContainer.QAxWidget object at 0x036C4C60>


看来wx的MakeActiveXClass步骤正在执行PyQt不能完成的工作,但是我不知道应该怎么做。

到目前为止,我已经引用了一些资源:

win32.Dispatch vs win32.gencache in Python. What are the pros and cons?

What can you do with COM/ActiveX in Python?

我也尝试过QCamera,但它无法识别相机。

最佳答案

制作self.axWidget = self.ocx不会导致self.ocx替换窗口中的self.axWidget,解决方案是通过使用setControl()方法设置控件来使用self.axWidget

class cameraForm(LandingPageBase, LandingPageUI):
    def __init__(self, parent=None):
        super(cameraForm, self).__init__(parent)
        self.setupUi(self)

        self.axWidget.setControl("{6B9BD678-9710-44D9-9282-A088094E4216}")
        self.axWidget.setProperty("Acquire", True)
        self.axWidget.setProperty("Display", True)


(代码未经测试)

关于python - ActiveX对象的PyQt5包装器,而不是WX包装器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60123343/

10-11 17:05