uEye camera with python on Windows

不打扰是莪最后的温柔 提交于 2019-12-30 03:24:21

问题


I need to get a uEye camera working with python on Windows in order to take pictures and operate on the live stream.
Since uEye cameras are widely spread industrial cameras I would have thought that there is a standard solution to this; however, I could not find any. The solution needs to run under python 2.7 on Windows XP or Windows 7.

I would appreciate anyone who has successfully used a uEye camera in python on Windows to share his knowledge on this issue or at least point me in the right direction. I also feel that there is really a need to find a generic soltion, since for sure I'm not the only one with this requirement.

What I've tried so far

(a) pyueye

There is a python driver available which works under Linux and - according to the documentation - "should work on windows".

I've tried that but installation failed:
python setup.py install gives me

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

I have no idea what cimported modules are and whether this should work at all. So it might be good to know if anyone has successfully installed this driver on a Windows system.

(b) openCV

OpenCV seems to be some kind of standard for image capturing and processing. It seems some people have used it to access a uEye camera, while there also seems to be some consensus that uEye cameras do not work with openCV. I haven't found any reportedly working example code.

Anyways I tried this (using openCV version 2.4.13) and I can access the camera and retrieve a picture from it. The resolution initially is 480 x 640, but I am able to change it to the sensor resoltion of 768 x 1024. However, I am not able to set the exposure time and the gain correctly, as can be seen in the following code I used.

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

It may well be that the camera is in some kind of auto-mode that automatically adjusts the parameters like exposure time and gain. But if this is the case, how would I set this auto-mode off.

(c) simpleCV

simpleCV seems to be an alternative to openCV. I also tried that and it gives the problem of only fetching a 480 x 640 pixel image and I couldn't find any way to set it differently, neither a way to set the exposure time.

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

(d) Writing own driver in C

One option might be to write a C code to access the camera via its SDK. A full documentation of the SDK is available and it seems, someone has successfully done it (here, or here) but I wouldn't even know where to start and how to get the live image into python.


回答1:


I had a similar project recently and found a couple of solutions that worked for me. I was also using python 2.7 (32-bit) and windows 7. I'm sure there are multiple other ways to control the camera but the two methods I found were (1) either using ctypes with the c++ API, or (2) using pythonnet (i.e. clr) with the dotNet libraries. Each method requires importing and calling functions from a separate dll file. I ultimately preferred the ctypes approach because it was easier to compile into an executable, but both methods worked equally well for controlling the camera.

1. uEye API using python ctypes:

The functions in the uEye API dll can be called in python using ctypes. Using ctypes is slightly cumbersome because passing variables between python and c requires constantly converting data types, but it works.

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. using pythonnet & uEye .NET interface

The syntax to call functions from the .NET dll is more straightforward than with ctypes, but installing the pythonnet (clr) package was difficult for me for some reason. Here's an example of acquiring a camera image with the .NET functions:

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



回答2:


I was able to get a ueye USB 3.0 camera working in Direct3D mode with the "IDS python libraries" Link to official project.

Also in use: wxpython 2.8, python 2.7. One key item to remember is to install the DirectX SDK. DXSDK_Jun10.exe worked for me.

This is working code, but it is not shutting down cleanly (it takes around 20 sec to close). It seems that just stop the capturing will allow it to close normally... an exercise for the 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


来源:https://stackoverflow.com/questions/40563139/ueye-camera-with-python-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!