問題描述
我需要一個(gè) uEye 相機(jī) 在 Windows 上使用 python 來拍照和操作直播.
由于 uEye 相機(jī)是廣泛使用的工業(yè)相機(jī),我認(rèn)為有一個(gè)標(biāo)準(zhǔn)的解決方案;但是,我找不到任何東西.解決方案需要在 Windows XP 或 Windows 7 上的 python 2.7 下運(yùn)行.
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 相機(jī),我將不勝感激,分享他在這個(gè)問題上的知識(shí),或者至少為我指明正確的方向.我也覺得確實(shí)有必要找到一個(gè)通用的解決方案,因?yàn)槲铱隙ú皇俏ㄒ灰粋€(gè)有這個(gè)要求的人.
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.
到目前為止我已經(jīng)嘗試過什么
What I've tried so far
有一個(gè) python 驅(qū)動(dòng)程序可用,它可以在 Linux 下運(yùn)行,并且 - 根據(jù)文檔 - 應(yīng)該在 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
模塊是什么以及這是否應(yīng)該工作.所以最好知道是否有人在 Windows 系統(tǒng)上成功安裝了這個(gè)驅(qū)動(dòng)程序.
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 似乎是圖像捕獲和處理的某種標(biāo)準(zhǔn).似乎有些人用它來訪問 uEye 相機(jī),而似乎也有一些共識(shí)認(rèn)為 uEye 相機(jī)不適用于 openCV.我沒有找到任何據(jù)報(bào)道有效的示例代碼.
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 版),我可以訪問相機(jī)并從中檢索圖片.最初的分辨率是 480 x 640
,但我可以將其更改為 768 x 1024
的傳感器分辨率.但是,我無法正確設(shè)置曝光時(shí)間和增益,如我使用的以下代碼所示.
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()
很可能相機(jī)處于某種自動(dòng)模式,自動(dòng)調(diào)整曝光時(shí)間和增益等參數(shù).但是如果是這種情況,我該如何設(shè)置這個(gè)自動(dòng)模式.
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
像素圖像的問題,我找不到任何不同的設(shè)置方法,也找不到設(shè)置曝光時(shí)間的方法.
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 編寫自己的驅(qū)動(dòng)程序
一種選擇可能是編寫 C 代碼以通過其 SDK 訪問相機(jī).提供完整的SDK 文檔看來,有人已經(jīng)成功地做到了(這里,或這里)但我什至不知道從哪里開始以及如何將實(shí)時(shí)圖像導(dǎo)入 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.
推薦答案
我最近有一個(gè)類似的項(xiàng)目,并找到了一些對(duì)我有用的解決方案.我還使用了python 2.7(32位)和windows 7.我確信還有多種其他方法可以控制相機(jī),但我發(fā)現(xiàn)的兩種方法是(1)使用ctypes和c++ API,或者(2)將 pythonnet(即 clr)與 dotNet 庫(kù)一起使用.每種方法都需要從單獨(dú)的 dll 文件中導(dǎo)入和調(diào)用函數(shù).我最終更喜歡 ctypes 方法,因?yàn)樗菀拙幾g成可執(zhí)行文件,但兩種方法在控制相機(jī)方面同樣適用.
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 中的函數(shù)可以在 python 中使用 ctypes 調(diào)用.使用 ctypes 有點(diǎn)麻煩,因?yàn)樵?python 和 c 之間傳遞變量需要不斷地轉(zhuǎn)換數(shù)據(jù)類型,但是它可以工作.
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)
<強(qiáng)>2.使用 pythonnet &uEye .NET 界面
從 .NET dll 調(diào)用函數(shù)的語法比使用 ctypes 更簡(jiǎn)單,但由于某種原因,安裝 pythonnet (clr) 包對(duì)我來說很困難.下面是一個(gè)使用 .NET 函數(shù)獲取相機(jī)圖像的示例:
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))
這篇關(guān)于在 Windows 上使用 python 的 uEye 相機(jī)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!