<p>我最近有一个类似的项目,找到了一些对我有用的解决方案。我还使用了python 2.7(32位)和windows 7。我确信还有其他多种方法可以控制相机,但我找到的两种方法是(1)使用c++API的ctypes,或(2)使用pythonnet(即clr)和dotNet库。每个方法都需要从单独的dll文件中导入和调用函数。我最终更喜欢ctypes方法,因为它更容易编译成可执行文件,但这两种方法在控制摄像机方面同样有效。</p>
<p><strong>1。使用python ctypes的uEye API:</strong></p>
<p>uEye API dll中的函数可以在python中使用ctypes调用。使用ctypes稍微有点麻烦,因为在python和c之间传递变量需要不断转换数据类型,但它是有效的。</p>
<pre><code>import ctypes
import numpy as np
uEyeDll = ctypes.cdll.LoadLibrary("ueye_api.dll") #include full path or copy dll into same folder as .py script
#connect camera
cam = ctypes.c_uint32(0)
hWnd = ctypes.c_voidp()
msg=uEyeDll.is_InitCamera(ctypes.byref(cam),hWnd)
ErrChk=uEyeDll.is_EnableAutoExit (cam, ctypes.c_uint(1))
if ~ErrChk:
print (' Camera Connected')
IS_CM_SENSOR_RAW8 =ctypes.c_int(11)
nRet = uEyeDll.is_SetColorMode(cam,IS_CM_SENSOR_RAW8)
IS_SET_TRIGGER_SOFTWARE = ctypes.c_uint(0x1000)
nRet = uEyeDll.is_SetExternalTrigger(cam, IS_SET_TRIGGER_SOFTWARE)
#allocate memory
width_py = 1600
height_py = 1200
pixels_py =8
width = ctypes.c_int(width_py) #convert python values into c++ integers
height = ctypes.c_int(height_py)
bitspixel=ctypes.c_int(pixels_py)
pcImgMem = ctypes.c_char_p() #create placeholder for image memory
pid=ctypes.c_int()
ErrChk=uEyeDll.is_AllocImageMem(cam, width, height, bitspixel, ctypes.byref(pcImgMem), ctypes.byref(pid))
if ~ErrChk:
print (' Success')
else:
print (' Memory allocation failed, no camera with value' +str(cam.value))
# Get image data
uEyeDll.is_SetImageMem(cam, pcImgMem, pid)
ImageData = np.ones((height_py,width_py),dtype=np.uint8)
#put these lines inside a while loop to return continous images to the array "ImageData"
uEyeDll.is_FreezeVideo (cam, ctypes.c_int(0x0000)) #IS_DONT_WAIT = 0x0000, or IS_GET_LIVE = 0x8000
uEyeDll.is_CopyImageMem (cam, pcImgMem, pid, ImageData.ctypes.data)
</code></pre>
<p><strong>2。使用pythonnet&uEye.NET接口</strong></p>
<p>从.NET dll调用函数的语法比使用ctypes更简单,但是由于某些原因,安装pythonnet(clr)包对我来说很困难。下面是使用.NET函数获取相机图像的示例:</p>
<pre><code>import numpy as np
import clr
import sys
import System
from System import Array, Double, IntPtr, Random
print System.Environment.Version
from CLR.System.Reflection import Assembly
from System.Collections.Generic import Dictionary
from System.Runtime.InteropServices import Marshal
true =bool(1)
false=bool(0)
#import .NET dll using clr (pythonnet)
sys.path.append(r"C:\Program Files\IDS\uEye\Develop\DotNet") # path of dll
clr.AddReference ('uEyeDotNet') # the dll
import uEye
# initialize camera
cam = uEye.Camera()
CAM_ID=1;
msg=cam.Init(CAM_ID)
print 'InitMessage ='+ str(msg)
# Change Camera settings
gain =1 #% gain
exposure = 0.2 #ms
ColorMode=cam.PixelFormat.Set(uEye.Defines.ColorMode.SensorRaw8)
errChk=cam.Trigger.Set(uEye.Defines.TriggerMode.Software)
errChk=cam.Gain.Hardware.GetSupported(1,1,1,1)
errChk,gainFactor=cam.Gain.Hardware.ConvertScaledToFactor.Master(gain, 1)
errChk=cam.Gain.Hardware.Factor.SetMaster(gainFactor)
errChk2,gain=cam.Gain.Hardware.Factor.GetMaster(gain)
errChk2,gainout=cam.Gain.Hardware.Scaled.GetMaster(1)
cam.Timing.Exposure.Set(1)
errChk,exposure_out=cam.Timing.Exposure.Get(exposure)
#allocate image memory
ErrChk, memout=cam.Memory.Allocate(1600,1200,8,true,1)
[ErrChk, Width, Height, Bits, Pitch] = cam.Memory.Inquire(memout,1,1,1,1);
# image aquisition
for n in range(1000):
ErrChk=cam.Acquisition.Freeze(true)
outarray = System.Array[System.Byte](())
[ErrChk, tmp] = cam.Memory.CopyToArray(memout, outarray)
#'Copy .Net Array using Marshal.Copy
imageData = np.empty(len(tmp),dtype=np.uint8)
Marshal.Copy(tmp, 0,IntPtr.__overloads__[int](imageData.__array_interface__['data'][0]), len(tmp))
</code></pre>