在 Windows 上使用 python 的 uEye 相机

uEye camera with python on Windows(在 Windows 上使用 python 的 uEye 相机)
本文介绍了在 Windows 上使用 python 的 uEye 相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要一个 uEye 相机 在 Windows 上使用 python 来拍照和操作直播.
由于 uEye 相机是广泛使用的工业相机,我认为有一个标准的解决方案;但是,我找不到任何东西.解决方案需要在 Windows XP 或 Windows 7 上的 python 2.7 下运行.

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.

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

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

有一个 python 驱动程序可用,它可以在 Linux 下运行,并且 - 根据文档 - 应该在 Windows 上工作".

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

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

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

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

我不知道 cimported 模块是什么以及这是否应该工作.所以最好知道是否有人在 Windows 系统上成功安装了这个驱动程序.

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.

OpenCV 似乎是图像捕获和处理的某种标准.似乎有些人用它来访问 uEye 相机,而似乎也有一些共识认为 uEye 相机不适用于 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.

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

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.

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

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) 用 C 编写自己的驱动程序

一种选择可能是编写 C 代码以通过其 SDK 访问相机.提供完整的SDK 文档看来,有人已经成功地做到了(这里,或这里)但我什至不知道从哪里开始以及如何将实时图像导入 python.

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

推荐答案

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

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.使用 python ctypes 的 uEye API:

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

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.使用 pythonnet &uEye .NET 界面

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

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 FilesIDSuEyeDevelopDotNet")  # 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))

这篇关于在 Windows 上使用 python 的 uEye 相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)