在Python中使用ueyeapi
我目前正在做一个项目,其中包括将IDS UEye XC摄像头连接到树莓Pi 3上以录制视频。在CythBee PI中,我使用C++、OpenCV和IDS API编写了一个脚本(脚本1)和Python中的另一个脚本(脚本2),使用GPIO按钮启动和停止脚本1。附件是脚本1和脚本2 脚本1:在Python中使用ueyeapi,python,c++,linux,opencv,camera,Python,C++,Linux,Opencv,Camera,我目前正在做一个项目,其中包括将IDS UEye XC摄像头连接到树莓Pi 3上以录制视频。在CythBee PI中,我使用C++、OpenCV和IDS API编写了一个脚本(脚本1)和Python中的另一个脚本(脚本2),使用GPIO按钮启动和停止脚本1。附件是脚本1和脚本2 脚本1: #include "uEye.h" #include "stdio.h" #include "stdlib.h" #include "iostream" #include "opencv2/highgui.hp
#include "uEye.h"
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/core.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
HIDS hCam = 1;
SENSORINFO sensor_info;
CAMINFO camera_info;
int nRet;
int Mode;
char strCamFileName[256];
int img_width=640;
int img_height=480;
int main ()
{
//initialize camera
nRet = is_InitCamera(&hCam, NULL);
cout << "Status Init: " << nRet << endl;
//get sensor info
//nRet = is_GetSensorInfo(hCam, &sensor_info);
//cout << "Sensor Color Mode: " << sensor_info.nColorMode << endl;
//cout << "Camera Model: " << sensor_info.strSensorName << endl;
//get camera info
//nRet = is_GetCameraInfo(hCam, &camera_info);
//cout << "Camera ID: " << camera_info.ID << endl;
//cout << "Camera SerNum: " << camera_info.SerNo << endl;
//cout << "Camera Version: " << camera_info.Version << endl;
//cout << "Camera Type: " << camera_info.Type << endl;
//color mode
Mode = IS_CM_RGB8_PACKED;
nRet = is_SetColorMode(hCam, Mode);
cout << "Color Mode: " << nRet << endl;
UINT formatID = 13;
nRet = is_ImageFormat(hCam, IMGFRMT_CMD_SET_FORMAT, &formatID, 4);
cout << "Status Image Format: " << nRet << endl;
char* pMem = NULL;
int memID = 0;
nRet = is_AllocImageMem(hCam, img_width, img_height, 24, &pMem, &memID);
nRet = is_SetImageMem(hCam, pMem, memID);
//set display mode
Mode = IS_SET_DM_DIB;
nRet = is_SetDisplayMode(hCam, Mode);
//zoom
double dZoomValue = 0;
nRet = is_Zoom(hCam, ZOOM_CMD_DIGITAL_SET_VALUE, (void*)&dZoomValue, sizeof(dZoomValue));
cout << "Zoom: " << dZoomValue << endl;
VideoWriter video("out.avi", CV_FOURCC('X','V','I','D'), 10, Size(img_width, img_height),true);
for(int ii=0; ii<600; ii++)
{
if(is_FreezeVideo(hCam, IS_WAIT) == IS_SUCCESS){
void *pMemVoid; //pointer to where the image is stored
is_GetImageMem (hCam, &pMemVoid);
Mat img=Mat(Size(img_width,img_height), CV_8UC3, pMemVoid);
video.write(img);
namedWindow( "Live Video", WINDOW_NORMAL);
resizeWindow("Live Video", 320,240);
imshow("Live Video", img);
waitKey(1);
}
}
//exit camera
is_ExitCamera(hCam);
return 0;
}
在我尝试将视频文件从树莓传送到Windows7笔记本电脑之前,我一直认为一切都很正常。我发现,如果让脚本1在整个持续时间内不间断地运行,它将在笔记本电脑上播放,但如果我使用脚本2中断脚本1,然后尝试在笔记本电脑上播放它,它将不会渲染
这让我相信,当我打断相机时,它并没有“清理”好。我的意思是,它没有到达是_ExitCamera()
,它可以干净地释放和关闭相机。我目前正在使用os.killpg()
终止该程序。有没有办法用<代码> iSyExtCimulaMo(< /代码>)替换它,即使一个是C++,另一个是Python?< /P>
提前谢谢 您可以通过调用所有带有“ctypes”的函数,在python中使用uEye API。我不确定你是否可以调用第二个脚本中的iSuxEclipse命令,或者是否需要将所有的第一个C++脚本重新编写为Python。p> python中使用ctypes的uEye API示例:
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))
# 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 continuous 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)
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))
# 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 continuous 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)