我正在尝试让 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。不会给出错误,但也不会启动相机:
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/