使用COM和Python通过DirectShow访问网络摄像头

2024-09-30 06:16:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用DirectShow的IAMVideoProcAmp获得对webcam properties的低级访问。在

有几个Python模块)pywin32pywintypescomtypeswin32compythoncom),它们似乎以某种方式相关。但我不知道从哪里开始。在

我找到了一些示例(hereherehere),但我无法找到如何获得一个IID/CLSID来使用

import win32com.client
clsid='{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
ShellWindows=win32com.client.Dispatch(clsid)

或者有一个明确的名字

^{pr2}$

或者

from comtypes import client, GUID
graph = client.CreateObject(some_CLSID)
graph.QueryInterface(...)

有人能帮我吗?在

我找到了另一个例子(dshow.py),但它有一些我找不到的依赖项(interfacesuuids)。在

来自Microsoft的This页将过程列出为

Call QueryInterface on the capture filter for the IAMVideoProcAmp interface.

或者

Query the capture filter for the IAMCameraControl.

并给出了一些C++代码:

// Query the capture filter for the IAMVideoProcAmp interface.
IAMVideoProcAmp *pProcAmp = 0;
hr = pCap->QueryInterface(IID_IAMVideoProcAmp, (void**)&pProcAmp);
hr = m_pProcAmp->GetRange(VideoProcAmp_Brightness, &Min, &Max, &Step,
    &Default, &Flags);

编辑: 我终于找到了一些目前看来不错的代码:

jaraco

它似乎完全符合我要写的东西,并使用了 DirectShow(see here):

from comtypes.gen.DirectShowLib import (FilterGraph, CaptureGraphBuilder2, ...)

在雅拉科视频声称是“纯Python中使用ctypes和comtypes的VideoCapture模块的端口。”

它使用^{}文件(不管是什么)来获取定义 进入comtypes

A type library (.tlb) is a binary file that stores information about a COM or DCOM object's properties and methods in a form that is accessible to other applications at runtime.


Tags: 模块theimportclientforherepropertiesfilter
2条回答

确定复制代码所需的值

再看一眼你文章末尾的代码节选,我意识到你只需要IID,而不需要IAMVideoProcAmp的CLSID来获取它的实例。在

查看this source of strmif.h的第8733行,它是接口所需的头,我发现IID_IAMVideoProcAmpC6E13360-30AC-11d0-A18C-00A0C9118956。在

在strmif.h的这一节之上,您可以确定哪些整数对应于tagVideoProcAmpProperty枚举中的哪些属性,例如0对应于{}。在strmif.h的这一节下面,您可以确定哪些整数对应于IAMVideoProcAmpVtblVTable中的哪些函数,例如3表示{}。我不熟悉如何在Python中与COM对象进行交互,但在爪哇中,您需要确定这些属性和函数索引,以便复制演示如何获取^ {CD10}}实例的C++代码摘录。在

获取IAMVideoProcAmp的实例

正如您可能注意到的,C++代码摘录在^ { < CD13>}上调用^ {< CD12>},并注意到您需要“查询IAMVIEW OPROAMP接口的捕获过滤器”。

To create a DirectShow capture filter for the device, call the IMoniker::BindToObject method to get an IBaseFilter pointer. Then call IFilterGraph::AddFilter to add the filter to the filter graph:

IBaseFilter *pCap = NULL;
hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pCap);
if (SUCCEEDED(hr))
{
    hr = m_pGraph->AddFilter(pCap, L"Capture Filter");
}

现在您已经知道了如何获取pCap,您注意到您需要一个名为pMoniker的东西,它在同一篇文章的前面已经定义。代码相当长,所以我在这里省略了它。在

在Python中完成所有这些

正如我之前提到的,我从来没有使用任何Python COM库,所以我不能轻易地举一个例子,但是你的目标应该是用Python复制C++实例中的函数调用来获取一个^ {CD1>}的实例,并根据需要修改它们。在

我终于找到了一些有用的示例库:

jaraco

它正是我想要达到的效果,并使用了 DirectShow(see here):

from comtypes.gen.DirectShowLib import (FilterGraph, CaptureGraphBuilder2, ...)

在雅拉科视频声称是“纯Python中使用ctypes和comtypes的VideoCapture模块的端口。”

它使用一个^{}文件(不管是什么)来获取定义 变成comtypes

A type library (.tlb) is a binary file that stores information about a COM or DCOM object's properties and methods in a form that is accessible to other applications at runtime.

导入是在__init__.py中自动生成的,可以轻松使用:

^{pr2}$

并且可以使用

def _get_camera_control(self):
    return self._get_graph_builder_interface(IAMCameraControl)

def get_camera_control_property(self, i):

    video_properties = self._get_camera_control()
    return video_properties.Get(i)

然后您可以将这些函数与文档中声明的enum结合使用,例如

# CameraControl_Exposure = 4
print(d.get_camera_control_property(4))

相关问题 更多 >

    热门问题