0

我正在尝试使用 pyueye 库来运行 ML 相机,并且遇到了 ctypes 的问题。一个函数需要一个类型为“ctypes instance”的参数,尽管尝试了所有可能的变体,但我无法弄清楚如何使用 ctypes 库生成它。这个库没有关于 python 的文档,但我尝试使用的函数的 C 文档是:

Syntax

INT is_SetAutoParameter (HIDS hCam, INT param, double* pval1, double* pval2)

Example 1

//Enable auto gain control:

double dEnable = 1;

int ret = is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_GAIN, &dEnable, 0);

我在 python 中收到的代码和后续错误是:

nRet = ueye.is_SetAutoParameter(hCam, ueye.IS_SET_ENABLE_AUTO_GAIN, ctypes.byref(ctypes.c_long(1)), ctypes.byref(ctypes.c_long(0)))

Error:
ret = _is_SetAutoParameter(_hCam, _param, ctypes.byref(pval1), ctypes.byref(pval2))
TypeError: byref() argument must be a ctypes instance, not 'CArgObject'

关于 ctypes 实例的任何建议?谢谢

编辑:最小的可重现示例

from pyueye import ueye
import ctypes

class Turfcam:
    def main(self):
        turfcam.take_photo()

    def take_photo(self):
        hCam = ueye.HIDS(0)
        pval1 = ctypes.c_double(1)
        pval2 = ctypes.c_double(0)
        nRet = ueye.is_SetAutoParameter(hCam, ueye.IS_SET_ENABLE_AUTO_GAIN, ctypes.byref(pval1), ctypes.byref(pval2))

        # Camera Init
        nRet = ueye.is_InitCamera(hCam, None)

if __name__ == "__main__":
    turfcam = Turfcam()
    turfcam.main()
4

1 回答 1

1

pyueye库有一些最少的文档:

_is_SetAutoParameter = _bind("is_SetAutoParameter", [ctypes.c_uint, ctypes.c_int, ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double)], ctypes.c_int)


def is_SetAutoParameter(hCam, param, pval1, pval2):
    """
    :param hCam: c_uint (aka c-type: HIDS)
    :param param: c_int (aka c-type: INT)
    :param pval1: c_double (aka c-type: double \*)
    :param pval2: c_double (aka c-type: double \*)
    :returns: success, or no success, that is the answer
    :raises NotImplementedError: if function could not be loaded
    """
    if _is_SetAutoParameter is None:
        raise NotImplementedError()

    _hCam = _value_cast(hCam, ctypes.c_uint)
    _param = _value_cast(param, ctypes.c_int)

    ret = _is_SetAutoParameter(_hCam, _param, ctypes.byref(pval1), ctypes.byref(pval2))

    return ret

包装器正在做byref,所以只ctypes需要传递一个对象。uEye文档说参数可以是输入或输出参数,因此始终创建c_double对象并根据传递函数的需要初始化它们。请注意,对于输出参数,您必须为对象分配一个名称,以便它在要查询的调用之后存在。输入参数可以ctypes.c_double(value)直接传递给函数,但为了保持一致性,我总是为对象分配名称:

例子:

pval1 = ctypes.c_double()
pval2 = ctypes.c_double() # not used for output on IS_GET_ENABLE_AUTO_GAIN 
nRet = ueye.is_SetAutoParameter(hCam, ueye.IS_GET_ENABLE_AUTO_GAIN, pval1, pval2)
print(pval1.value) # query the returned value
于 2020-09-09T03:18:47.657 回答