Windows 上 python 的 uEye 相机

uEye camera with python on Windows

我需要 uEye 相机 在 Windows 上与 python 配合使用才能拍照并对直播进行操作。
由于 uEye 相机是广泛使用的工业相机,我本以为对此有一个标准的解决方案;但是,我找不到任何东西。 解决方案需要在 Windows XP 或 Windows 7.python 2.7 下 运行。

如果有人在 Windows 上的 python 中成功使用过 uEye 相机,请分享他在这个问题上的知识,或者至少为我指明正确的方向,我将不胜感激。我也觉得确实需要找到一个通用的解决方案,因为可以肯定的是,我不是唯一有此要求的人。

到目前为止我尝试了什么

(a) pyueye

有一个 python driver available 可以在 Linux 下工作,并且 - 根据文档 - “应该在 windows 上工作”。

我试过了,但安装失败:
python setup.py install 给我

ueye\ueye.pyx: cannot find cimported module 'stdlib'
ueye\ueye.pyx: cannot find cimported module 'python_cobject'
Compiling ueye\ueye.pyx because it changed.
Compiling ueye\ueyeh.pyx because it changed.
[1/2] Cythonizing ueye\ueye.pyx

我不知道 cimported 模块是什么,也不知道它是否应该工作。因此,最好知道是否有人已在 Windows 系统上成功安装此驱动程序。

(b) openCV

OpenCV 似乎是某种图像捕获和处理标准。似乎有人用它来访问 uEye 相机,同时似乎也有一些共识认为 uEye 相机不能与 openCV 一起使用。我还没有找到任何据说有效的示例代码。

无论如何我试过了(使用 openCV 2.4.13 版),我可以访问相机并从中检索照片。分辨率最初是 480 x 640,但我可以将其更改为 768 x 1024 的传感器分辨率。 但是,我无法正确设置曝光时间和增益,正如我使用的以下代码所示。

cam = cv2.VideoCapture(0)

width = cam.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)     
height = cam.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) 
exposure = cam.get(cv2.cv.CV_CAP_PROP_EXPOSURE) 
print width, height, exposure # prints 640 480 -4.0
hr = cam.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 768)
wr = cam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1024) 
print "Setting resolution ", hr, wr  # prints  True True
cam.set(cv2.cv.CV_CAP_PROP_EXPOSURE, 0)  # or any other value, same for gain

width = cam.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)    
height = cam.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) 
exposure = cam.get(cv2.cv.CV_CAP_PROP_EXPOSURE) 
print width, height, exposure # 1024.0 768.0 -4.0

ret, buff = cam.read() 
cam.release()

可能是相机处于某种自动模式,自动调整曝光时间获得。但如果是这种情况,我将如何关闭此自动模式

(c) 简单CV

simpleCV 似乎是 openCV 的替代品。我也试过了,它给出了只获取 480 x 640 像素图像的问题,我找不到任何不同的方法来设置它,也没有设置曝光时间的方法。

from SimpleCV import Camera
cam = Camera(0)
img = cam.getImage() # img is a 480 x 640 pixel image

(d) 在 C 中编写自己的驱动程序

一种选择可能是编写 C 代码以通过其 SDK 访问相机。完整的 documentation of the SDK 可用 看来有人成功了 (here, or here) 但我什至不知道从哪里开始以及如何将实时图像导入 python。

我最近有一个类似的项目,并找到了几个适合我的解决方案。我还使用 python 2.7(32 位)和 windows 7。我确定还有多种其他方法可以控制相机,但我发现的两种方法是 (1) 使用 ctypes 和c++ API,或 (2) 使用 pythonnet(即 clr)和 dotNet 库。每种方法都需要从单独的 dll 文件中导入和调用函数。我最终更喜欢 ctypes 方法,因为它更容易编译成可执行文件,但两种方法在控制相机方面同样有效。

1. uEye API 使用 python ctypes:

uEye API dll 中的函数可以在 python 中使用 ctypes 调用。使用ctypes有点麻烦,因为在python和c之间传递变量需要不断转换数据类型,但它有效。

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) 

2。使用 pythonnet & uEye .NET 界面

从 .NET dll 调用函数的语法比使用 ctypes 更直接,但由于某些原因,安装 pythonnet (clr) 包对我来说很困难。以下是使用 .NET 函数获取相机图像的示例:

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))

我能够使用 "IDS python libraries" Link to official project 使 ueye USB 3.0 相机在 Direct3D 模式下工作。

也在使用:wxpython 2.8,python 2.7。要记住的一项关键事项是安装 DirectX SDK。 DXSDK_Jun10.exe 对我有用。

这是工作代码,但它没有完全关闭(大约需要 20 秒才能关闭)。似乎只是停止捕获将允许它正常关闭...... reader.

的练习
import wx

def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1BCAPTURE, wxID_DIALOG1DIMAGE, 
] = [wx.NewId() for _init_ctrls in range(3)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(739, 274), size=wx.Size(888, 674),
              style=wx.DEFAULT_DIALOG_STYLE, title='Dialog1')
        self.SetClientSize(wx.Size(872, 636))

        self.Dimage = wx.Panel(id=wxID_DIALOG1DIMAGE, name=u'Dimage',
              parent=self, pos=wx.Point(24, 24), size=wx.Size(640, 480),
              style=wx.TAB_TRAVERSAL)
        self.Dimage.SetBackgroundColour(wx.Colour(233, 251, 230))

        self.BCapture = wx.Button(id=wxID_DIALOG1BCAPTURE, label=u'Capture',
              name=u'BCapture', parent=self, pos=wx.Point(136, 520),
              size=wx.Size(144, 71), style=0)
        self.BCapture.Bind(wx.EVT_BUTTON, self.OnBCaptureButton,
              id=wxID_DIALOG1BCAPTURE)

    def __init__(self, parent):
        self._init_ctrls(parent)

        # Video
        from pyueye import ueye
        import win32ui
        self.capture = None
        h_cam = ueye.HIDS(0)
        hwnd = ueye.HWND(self.Dimage.GetHandle())

        cam = ueye.is_InitCamera(h_cam,hwnd)
        if cam == 0:
            print 'camera was intintialized'
        else:
            print 'camera result',cam

        col = ueye.c_int(0)
        mod = ueye.c_int(0)
        ueye.is_GetColorDepth(h_cam, col, mod)
        nRet = ueye.is_SetColorMode (h_cam, mod)
        if nRet != ueye.IS_SUCCESS: print 2,nRet
        print 1,col,mod

        SensorInfo = ueye.SENSORINFO()
        nRet = ueye.is_GetSensorInfo(h_cam,SensorInfo)
        if nRet != ueye.IS_SUCCESS: print 55,nRet
        for i in SensorInfo._fields_:
            print i[0],eval('SensorInfo.%s'%i[0])

        imgw,imgh = self.Dimage.GetSizeTuple()
        imageSize = ueye.IS_SIZE_2D()
        imageSize.s32Width = imgw
        imageSize.s32Height = imgh
        nRet = ueye.is_AOI(h_cam, ueye.IS_AOI_IMAGE_SET_SIZE, imageSize, ueye.sizeof(imageSize))
        if nRet != ueye.IS_SUCCESS: print 77,nRet

        m_nDisplayMode = ueye.IS_SET_DM_DIRECT3D
        nRet = ueye.is_SetDisplayMode(h_cam, m_nDisplayMode)
        if nRet != ueye.IS_SUCCESS: print 88,nRet

        #ueye.is_DirectRenderer(h_cam,DR_GET_OVERLAY_KEY_COLOR
        #ueye.is_DirectRenderer(h_cam,DR_GET_MAX_OVERLAY_SIZE

        #ueye.is_DirectRenderer(h_cam,DR_GET_USER_SYNC_POSITION_RANGE
        ueye.is_DirectRenderer(h_cam, ueye.DR_SET_VSYNC_OFF, ueye.c_int(0), ueye.c_int(0))

        #ueye.is_DirectRenderer(h_cam, ueye.DR_SET_HWND, hwnd,ueye.sizeof(hwnd))
        #ueye.is_DirectRenderer(h_cam,ueye.DR_ENABLE_SCALING,None,None)

        nRet = ueye.is_CaptureVideo(h_cam, ueye.IS_WAIT) 
        if nRet != ueye.IS_SUCCESS: print 99,nRet

    def OnBCaptureButton(self, event):
        event.Skip()

if __name__ == '__main__':
    app = wx.App(0)
    parent = wx.Frame(None)
    parent.Show()
    dlg = create(parent)
    try:
        result = dlg.ShowModal()
    finally:
        dlg.Destroy()
    del dlg.capture